As was foretold, we've added advertisements to the forums! If you have questions, or if you encounter any bugs, please visit this thread: https://forums.penny-arcade.com/discussion/240191/forum-advertisement-faq-and-reports-thread/
Options

BASH Scripts for OSX

CalliusCallius Registered User regular
edited October 2007 in Help / Advice Forum
So, I'm trying to make a BASH script for an automated installation of a few tarballs in OSX. However, there is the slight problem of me not knowing how to make a script! HAH! Whatever, small hurdle...

Anywho, I've been to the OSX scripting site and it seemed relatively straight forward. So I created a little test script. Somehow, I get more lines of errors than I have lines of code. What the fuck?

Here is my code:
#! /bin/bash
$echo "Hello"

It's titled "script.sh" and I used chmod +x to make it executable. However, when I go into bash and type in ~/desktop/script.sh I get the following error:
./script.sh: line 1: {rtf1macansicpg10000cocoartf824cocoasubrtf420: command not found
./script.sh: line 2: syntax error near unexpected token `}'
./script.sh: line 2: `{\fonttbl\f0\fswiss\fcharset77 Helvetica;}'

I'm at a fucking loss... any ideas?

tonksigblack.png
Callius on

Posts

  • Options
    DrFrylockDrFrylock Registered User regular
    edited October 2007
    You edited the script in your word processor and saved it as an .rtf file. Scripts need to be plain text. The operating system doesn't need bold and italics and Comic Sans fonts.

    You need to use, like, a text editor. Like TextEdit or vi.

    Also it's been a while for me but I don't think you need the $ in front of 'echo'

    DrFrylock on
  • Options
    BarrakkethBarrakketh Registered User regular
    edited October 2007
    The shebang line shouldn't have any spaces in it.

    The '$' shouldn't be there before echo. Usually when you see a '$' at the beginning of a line it is because whatever example you are looking at is being entered directly into the terminal, so $ is the prompt.

    Barrakketh on
    Rollers are red, chargers are blue....omae wa mou shindeiru
  • Options
    CalliusCallius Registered User regular
    edited October 2007
    I added the $ there in frustration, it's since been deleted.

    Hrmm... I used TextEdit to create the script. Do I need to save it as HTML? The options I'm given are:

    RTF
    HTML
    Word Format
    Word XML Format

    Callius on
    tonksigblack.png
  • Options
    LewishamLewisham Registered User regular
    edited October 2007
    Callius wrote: »
    I added the $ there in frustration, it's since been deleted.

    Hrmm... I used TextEdit to create the script. Do I need to save it as HTML? The options I'm given are:

    RTF
    HTML
    Word Format
    Word XML Format

    Well, no, because HTML will save it with HTML markup, won't it?

    If you don't have a text editor like TextWrangler or TextMate or SubEthaEdit, you can just do it at the command-line with pico or emacs or vi.

    Lewisham on
  • Options
    DrFrylockDrFrylock Registered User regular
    edited October 2007
    Oops I mistakenly assumed that TextEdit was just the OS X version of the old SimpleText application. Apparently not so. I edit real text files on a mac using vi anyway.

    http://docs.info.apple.com/article.html?artnum=106212

    This explains the eight step procedure you have to use to get your Mac plain text editor to actually edit plain text. And people say that it's easier to use, eh? Sigh.

    DrFrylock on
  • Options
    CalliusCallius Registered User regular
    edited October 2007
    Okay, I managed to turn it into plain text and now it's working.

    Fucking right on!

    Thanks for your help everyone.

    Now I just need to figure out how to untar these things into directories instead of right where they're located, but that's a job for tomorrow.

    Callius on
    tonksigblack.png
  • Options
    SeñorAmorSeñorAmor !!! Registered User regular
    edited October 2007
    Callius wrote: »
    Now I just need to figure out how to untar these things into directories instead of right where they're located, but that's a job for tomorrow.
    tar xvf file.tar -C /path/to/directory
    

    v (for 'verbose') is optional, of course, and make sure you add the 'z' flag if your tarballs are compressed with gzip, too.

    SeñorAmor on
  • Options
    CalliusCallius Registered User regular
    edited October 2007
    Thanks everyone, this worked really well.

    I just have one more question. Is there any way to delete all the files in a folder except ones ending in a specific suffix? A la; when I'm done I want to delete all the files in a directory except those ending in .tar.z

    Any ideas?

    Callius on
    tonksigblack.png
  • Options
    JaninJanin Registered User regular
    edited October 2007
    Callius wrote: »
    Thanks everyone, this worked really well.

    I just have one more question. Is there any way to delete all the files in a folder except ones ending in a specific suffix? A la; when I'm done I want to delete all the files in a directory except those ending in .tar.z

    Any ideas?

    Use find.
    find . -not -name "*.tar.z" -delete
    

    Janin on
    [SIGPIC][/SIGPIC]
  • Options
    CalliusCallius Registered User regular
    edited October 2007
    Hrmm, that didn't work out so well. It ended up deleting everything. hrmm....

    Callius on
    tonksigblack.png
  • Options
    BarrakkethBarrakketh Registered User regular
    edited October 2007
    Callius wrote: »
    Hrmm, that didn't work out so well. It ended up deleting everything. hrmm....

    It's best to do a dry run by not specifying an action to be taken. That said, the example he gave should have worked. I did a similar command using both -name and -iregex and it worked as expected.
    find ./ -not -name '*.rar' -exec echo "{}" \;
    

    Which would print the name of the files that don't match the expression. Scanning the output with grep confirmed that it properly excluded all RAR archives.

    Barrakketh on
    Rollers are red, chargers are blue....omae wa mou shindeiru
  • Options
    CalliusCallius Registered User regular
    edited October 2007
    Oh awesome. It seems that my problem was with not capitalizing the Z in the script.

    I guess it's case sensitive. odd that some commands are and some aren't.

    Thank you so much, you have all been really helpful.

    Callius on
    tonksigblack.png
  • Options
    BarrakkethBarrakketh Registered User regular
    edited October 2007
    Callius wrote: »
    Oh awesome. It seems that my problem was with not capitalizing the Z in the script.

    I guess it's case sensitive. odd that some commands are and some aren't.

    If you want it case insensitive, use -iname instead of -name.

    Barrakketh on
    Rollers are red, chargers are blue....omae wa mou shindeiru
Sign In or Register to comment.