11 Replies Latest reply: Feb 19, 2012 2:02 PM by X423424X
juan manuelfromcf Level 1 Level 1

I need to execute a file with terminal commands without open the terminal.

 

I just want to double click the file and get the terminal open and the commands executed like when you run a CMD file in Windows.

 

I created a file.command but it did not work.

 

Thanks !


iMac, Mac OS X (10.7.2)
Solved by X423424X on Feb 19, 2012 12:01 PM Solved

I think there's a pair of buttons on your posts. 

Reply by X423424X on Feb 19, 2012 3:57 AM Helpful

For running scripts in the shell you don't even need a .command extension.  For example, here's a line,

 

echo "hello world"

 

Save it in a file, call the file "hello", with whatever you are using as a text editor, and not a .rtf (rich text file).  Then in terminal do,

 

chmod +x "hello"

 

and then type,

 

hello

 

You will see "hello world " on your terminal.  I'm assuming file "hello" is in the current directory.

 

Just in case you missed the lines I added to the above post I repeat what I added here:

 

I recommend you use a better test editor, for example BBEdit, TextWrangler, and a whole host of other text editors better suited for the purpose.

All replies

  • SeeYa32 Level 1 Level 1

    It's probably a permissions problem. If you get the error message "Terminal can't open this file, it's not executable", try opening Terminal, navigating to the file and "chmod 755 <file name>". Hope that helps!

     

    [via mac-forums]

  • juan manuelfromcf Level 1 Level 1

    Thanks for your quick response.

     

    That was the first issue I had but I solved it.

     

    Now the Terminal open and give me this message

     

    Last login: Sat Feb 18 10:00:53 on ttys003

    Juans-iMac:~ jmg$ /Users/jmg/Documents/Backup.command ; exit;

    /Users/jmg/Documents/Backup.command: line 1: {rtf1ansiansicpg1252cocoartf1138cocoasubrtf230: command not found

    /Users/jmg/Documents/Backup.command: line 2: syntax error near unexpected token `}'

    /Users/jmg/Documents/Backup.command: line 2: `{\fonttbl\f0\fswiss\fcharset0 Helvetica;}'

    logout

     

    [Process completed]

     

    I got this message, no matter which .command file I try to open.

     

    Thanks again !

  • X423424X Level 6 Level 6

    I would suggest that before you work on seeing if can be launched from the finder you make sure the script itself works simply running it from the shell.  It's a lot easier to debug that way.  Add set -x and set +x around interesting parts to trace what's happening (assuming you are using bash).

  • juan from Buenos aires Level 1 Level 1

    Yes, I did that.

     

    I am testing one .command file with only one command

     

    Clear

     

    and get that message.

     

    Should I add something else to the file to make it work ?

  • X423424X Level 6 Level 6

    It would be nice to see that file so that we can see the context.  Hope it isn't too long.

     

    Note, if I understand what I am seeing above with your error message line 1 of you script looks like???

     

    {rtf1ansiansicpg1252cocoartf1138cocoasubrtf230

     

    If that is correct of course that's an error since that isn't a shell command line. Similarly line 2.

     

    By any chance did you create that script as a .rtf file?  That's what those lines look like.  It should be a plain text file. 

     

    If you insist on using something like TextEdit then set it to "Make Plain Text" as opposed to "Make Rich Test" in TextEdit's Format menu.

     

    I recommend you use a better test editor, for example BBEdit, TextWrangler, and a whole host of other text editers better suited for the purpose.

  • juan from Buenos aires Level 1 Level 1

    Just created a text and wrote one line with the CLEAR command and I changed the extention to command.

     

    I don´t know if that is right way to execute Terminal commands without open it.

  • X423424X Level 6 Level 6

    For running scripts in the shell you don't even need a .command extension.  For example, here's a line,

     

    echo "hello world"

     

    Save it in a file, call the file "hello", with whatever you are using as a text editor, and not a .rtf (rich text file).  Then in terminal do,

     

    chmod +x "hello"

     

    and then type,

     

    hello

     

    You will see "hello world " on your terminal.  I'm assuming file "hello" is in the current directory.

     

    Just in case you missed the lines I added to the above post I repeat what I added here:

     

    I recommend you use a better test editor, for example BBEdit, TextWrangler, and a whole host of other text editors better suited for the purpose.

  • juan from Buenos aires Level 1 Level 1

    Thanks a lot ! I used TextWrangler and it worked.

  • juan from Buenos aires Level 1 Level 1

    I want to give you 10 points and don´t know how

  • X423424X Level 6 Level 6

    I think there's a pair of buttons on your posts. 

  • X423424X Level 6 Level 6

    Thanks.