Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Nogui

macrumors newbie
Original poster
Sep 5, 2011
7
0
Abu Dhabi
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?
 
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?

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.
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.