How can I make a command run in Terminal?

Discussion in 'macOS' started by Nogui, Sep 5, 2011.

  1. Nogui macrumors newbie

    Joined:
    Sep 5, 2011
    Location:
    Abu Dhabi
    #1
    I have a code, currently saved in RTF format, and I want to run it as a Terminal command without having to copy and paste it every time I want to use it.

    How can I configure a file so that it automatically runs in Terminal as soon as I double click it?
     
  2. r0k macrumors 68040

    r0k

    Joined:
    Mar 3, 2008
    Location:
    Detroit
    #2
    In Finder, you can rename the file something.sh and that should do it. Also you might need to add the following as the first line of the file:

    #! /bin/sh

    or

    #! /bin/bash

    if your script relies on the bash shell rather than the sh shell.

    Here is my script demo.sh that runs in a terminal and pauses so you can press a key otherwise it would be gone so quick you wouldn't be sure anything happened...
    Code:
    #! /bin/sh
    echo "this is a demo"
    read -p "done..."
    I created the script above and then did the following:

    In a shell, I executed the following command:
    chmod a+x demo.sh

    I then right-clicked on demo.sh and picked open with...
    I then changed to all applications and navigated to utilities and picked Terminal. I then checked "always open with" and selected open. If I didn't do this, on my Mac a .sh file always opens with xCode which is not what I wanted for the purpose of this demo. Another good thing to do if you are really going to use Textedit to edit shell scripts is change the default file type to txt instead of rtf in Textedit preferences.
     
  3. Nogui thread starter macrumors newbie

    Joined:
    Sep 5, 2011
    Location:
    Abu Dhabi
    #3
    Thanks. I put the command at the top and renamed it and it said I didn't have permission.

    I took a guess from previous experience and just did a command script chmod a+x and dragged the file across. Was this the right thing, or did I just screw it up? It worked fine for me.
     

Share This Page