New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

Making a Shell Script Executable


Post Reply New Thread Subscribe

 
Thread Tools
surfwax95

 
surfwax95's Avatar
 
Member Since: Aug 27, 2005
Location: Fayetteville, AR
Posts: 2,410
surfwax95 has much to be proud ofsurfwax95 has much to be proud ofsurfwax95 has much to be proud ofsurfwax95 has much to be proud ofsurfwax95 has much to be proud ofsurfwax95 has much to be proud ofsurfwax95 has much to be proud ofsurfwax95 has much to be proud ofsurfwax95 has much to be proud of
Mac Specs: 15" Powerbook G4 24" iMac iPhone 3Gs

surfwax95 is offline
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.

I try to Command+Shift+/ when I can.
They're - Their - There | Two - Too - To | You're - Your | MAC - Mac
I was on the M-F honor roll for October, 2006.
QUOTE Thanks
rman

 
rman's Avatar
 
Member Since: Dec 24, 2002
Location: Los Angeles, California
Posts: 12,591
rman has much to be proud ofrman has much to be proud ofrman has much to be proud ofrman has much to be proud ofrman has much to be proud ofrman has much to be proud ofrman has much to be proud ofrman has much to be proud ofrman has much to be proud ofrman has much to be proud of
Mac Specs: 2 x 3.0GHz Quad-Core, 6GB OS X 10.6.8 | 15in MacBook Pro 2.2GHz OS X 10.6.8 | 64GB iPad 2 WiFi

rman is offline
Try chmod 755 script name

Life isn't about waiting for the storm to pass, It's about learning to dance in the rain!
QUOTE Thanks
surfwax95

 
surfwax95's Avatar
 
Member Since: Aug 27, 2005
Location: Fayetteville, AR
Posts: 2,410
surfwax95 has much to be proud ofsurfwax95 has much to be proud ofsurfwax95 has much to be proud ofsurfwax95 has much to be proud ofsurfwax95 has much to be proud ofsurfwax95 has much to be proud ofsurfwax95 has much to be proud ofsurfwax95 has much to be proud ofsurfwax95 has much to be proud of
Mac Specs: 15" Powerbook G4 24" iMac iPhone 3Gs

surfwax95 is offline
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.

I try to Command+Shift+/ when I can.
They're - Their - There | Two - Too - To | You're - Your | MAC - Mac
I was on the M-F honor roll for October, 2006.
QUOTE Thanks
rman

 
rman's Avatar
 
Member Since: Dec 24, 2002
Location: Los Angeles, California
Posts: 12,591
rman has much to be proud ofrman has much to be proud ofrman has much to be proud ofrman has much to be proud ofrman has much to be proud ofrman has much to be proud ofrman has much to be proud ofrman has much to be proud ofrman has much to be proud ofrman has much to be proud of
Mac Specs: 2 x 3.0GHz Quad-Core, 6GB OS X 10.6.8 | 15in MacBook Pro 2.2GHz OS X 10.6.8 | 64GB iPad 2 WiFi

rman is offline
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.

Life isn't about waiting for the storm to pass, It's about learning to dance in the rain!
QUOTE Thanks
falcon1620

 
Member Since: Dec 30, 2007
Posts: 1
falcon1620 is on a distinguished road

falcon1620 is offline
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.
QUOTE Thanks
brittburns05

 
Member Since: Aug 26, 2009
Posts: 3
brittburns05 is on a distinguished road

brittburns05 is offline
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.
QUOTE Thanks
vansmith

 
vansmith's Avatar
 
Member Since: Oct 19, 2008
Location: Toronto
Posts: 17,685
vansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond repute
Mac Specs: 2012 13" MBP (2.5 i5, 8GB)

vansmith is offline
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

Important Links: Community Guidelines : Use the reputation system if you've been helped.
M-F Blog :: Write for the blog
Personal Twitter
QUOTE Thanks
brittburns05

 
Member Since: Aug 26, 2009
Posts: 3
brittburns05 is on a distinguished road

brittburns05 is offline
thanks. I'll try that.
QUOTE Thanks
brittburns05

 
Member Since: Aug 26, 2009
Posts: 3
brittburns05 is on a distinguished road

brittburns05 is offline
Does the location of the file matter? I have it in OSX/Users/brittburns/ . Would I type "chmod 755 OSX/Users/brittburns/(file name)" ?
QUOTE Thanks
vansmith

 
vansmith's Avatar
 
Member Since: Oct 19, 2008
Location: Toronto
Posts: 17,685
vansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond repute
Mac Specs: 2012 13" MBP (2.5 i5, 8GB)

vansmith is offline
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.

Important Links: Community Guidelines : Use the reputation system if you've been helped.
M-F Blog :: Write for the blog
Personal Twitter
QUOTE Thanks
zarl1n0

 
Member Since: Aug 25, 2010
Posts: 1
zarl1n0 is on a distinguished road

zarl1n0 is offline
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%.*};
	cat $var > $newname
	chmod +x $newname
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 file.sh
once I am done, which makes an executable file called file.
QUOTE Thanks

Post Reply New Thread Subscribe


« Battery info on macbook | openmotif version? »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
MacAddict Backup Script sevenhelmet OS X - Development and Darwin 7 09-18-2005 03:56 PM
OS X Tiger shell vs RedHat Fedora Linux shell? AlexN OS X - Operating System 2 09-17-2005 07:08 PM
Shell script question Niwrad OS X - Development and Darwin 14 06-28-2005 04:58 PM
Shell Script on Desktop starrin OS X - Operating System 2 07-28-2004 11:25 AM
tsch script - nested loop dynamic variable reference lizardthefish OS X - Development and Darwin 0 03-04-2004 11:53 PM

All times are GMT -4. The time now is 10:02 PM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?