Making a Shell Script Executable

Joined
Aug 27, 2005
Messages
2,406
Reaction score
210
Points
63
Location
Fayetteville, AR
Your Mac's Specs
15" Powerbook G4 • 24" iMac • iPhone 3Gs
I made a simple script with pico, but I don't know how to make it executable from the Finder. I've tried changing the suffix to .command, but it just says "Terminal can't open this file, it's not executable".

I've looked a little at "chmod" but really don't understand how it works fully. So if anyone could help me out, please do.
Thanks.
 

rman


Retired Staff
Joined
Dec 24, 2002
Messages
12,637
Reaction score
168
Points
63
Location
Los Angeles, California
Your Mac's Specs
14in MacBook Pro M1 Max 32GB 2TB
Try chmod 755 script name
 
OP
surfwax95
Joined
Aug 27, 2005
Messages
2,406
Reaction score
210
Points
63
Location
Fayetteville, AR
Your Mac's Specs
15" Powerbook G4 • 24" iMac • iPhone 3Gs
Beeeautiful. That worked perfectly. :mac:

Now, could you explain WHY that worked perfectly? What does the 755 mean?

I "man"ed chmod, but didn't see the 755 code.
 

rman


Retired Staff
Joined
Dec 24, 2002
Messages
12,637
Reaction score
168
Points
63
Location
Los Angeles, California
Your Mac's Specs
14in MacBook Pro M1 Max 32GB 2TB
If you look at the chomd man pages, you see that you can change the permissions two ways. I find using the numbers to be easier.

Here is what it breaks down too.

read = 4; write = 2; execute = 1

There are three permission fields to each file; owner, group and other (world).
In that order. So if you get a directory listing (long) of a file you might see the following:

-rwxr-xr-x 5 rman user 200 13 Dec 2005 script

Looking at the string -rwxr-xr-x. you will see the first -, which is either l (link) or d (directory) and t (I think stick bit - no sure there). The remaining string you can break up in to three octets rwx (owner), r-x (group), and r-x (other).

If you look at the above numbers the string would equate to this 755. The ower have read, write and execute persmissions. Whereas the group and other only has read and execute permissions.
 
Joined
Dec 30, 2007
Messages
1
Reaction score
0
Points
1
You can also use chmod u+x script-name or just chmod x script-name, that will change the permissions to excitable (assuming your the owner of the file) if you want you can also view the file permissions using ls -l in case anyone was wondering about that as well. ;D
 
Joined
Aug 26, 2009
Messages
3
Reaction score
0
Points
1
how do I use chmod 755. I have a file that I want to make executable with chmod 755. I am new at this. Thanks.
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
You have to execute that in the Terminal (/Applications/Utilities/Terminal.app). So, if I wanted to make ~/test.sh executable, I could execute the following:
Code:
chmod 755 ~/test.sh
 
Joined
Aug 26, 2009
Messages
3
Reaction score
0
Points
1
Does the location of the file matter? I have it in OSX/Users/brittburns/ . Would I type "chmod 755 OSX/Users/brittburns/(file name)" ?
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
There should be no OSX at the front of that directory. / is the top level directory, much like C: on a Windows machine.

The only restriction is that you need permissions for that file. If it's in your user directory, you should be fine.
 
Joined
Aug 25, 2010
Messages
1
Reaction score
0
Points
1
A couple options

EDIT: I posted this in the wrong place, and cannot seem to remove it. But it's semi-relevant, so here it is.

One way to make a script an "Unix executable file" (with an icon that is the dark colored rectangular with '.exec' written on it) is to use 'cat' to get rid of the Apple file-type encoding. If you have a script called 'script.sh', and you rename it to 'script' with no extension, the file will continue to be identified as a '.sh' file, and thus will want to be opened with a text editor, unless of course you change the settings. But you can change the default application to Terminal by erasing the file-type info (with 'cat') and turning it into an executable file (with 'chmod'). The following script uses this method to change a file to an executable:
Code:
#!/bin/bash
# makeexec.sh
#
# changes the file type to "Unix executable file"
# by cat and chmod +x

if [ -z "$1" ]; then 
  echo usage: $0 directory
  exit
fi

for var in "$@"
do
if [ -f $var ]; then
	newname=${var%.*};
	[B]cat $var > $newname
	chmod +x $newname[/B]
fi
done

exit

The only problem with this method is that everytime you edit the executable, it wants to turn back into a .txt file. So, for this reason, I made this makeexec.sh script and put it in /usr/bin to speed up the process. I just do my edits, and then finish with a
Code:
makeexec.sh [I]file.sh[/I]
once I am done, which makes an executable file called file.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top