Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Shell Scripting trouble - help!

Shell Scripting trouble - help!
Thread Tools
Mac Elite
Join Date: Jan 2001
Status: Offline
Reply With Quote
May 9, 2001, 06:54 PM
 

My attempts to write simple shell scripts in OSX have failed. They won't execute. I have properly chmod +x the files to execute the scripts, yet it doesn't work, even from a 'root' account.

I suspect the $PATH might be at fault. Is it necessary to include the file's PWD in the $PATH for shell scripts to execute?

I've used UNIX for years, but only with OSX have I had the chance to start learning more about these more advanced aspects.

tnx.
"Last time the French asked for more evidence, it rolled through France with a German flag." - David Letterman
     
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status: Offline
Reply With Quote
May 9, 2001, 08:59 PM
 
Assume that your script is named MyScript. Given that you permissions do not seem to be the problem, try the following:

./MyScript

This should execute your script. If you want to avoid the ./ in front of your scripts then you need to add the directory of interest to your PATH variable.

iMac 17" G4 800MHZ & 768 SDRAM
     
NeoMac  (op)
Mac Elite
Join Date: Jan 2001
Status: Offline
Reply With Quote
May 9, 2001, 10:01 PM
 

Thanks. That worked.

I'll have to learn more about the environment settings in UNIX.
"Last time the French asked for more evidence, it rolled through France with a German flag." - David Letterman
     
Xeo
Moderator Emeritus
Join Date: Mar 2001
Location: Austin, MN, USA
Status: Offline
Reply With Quote
May 9, 2001, 10:14 PM
 
How do you write shell scripts?

They are basically commands that call other commands right? like using "alias"? It's pretty easy, right?

For an example, can you show me how to write a shell script called "foo" that will call "ls -al"

That should be a pretty simple example to follow.

------------------
     
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
May 9, 2001, 11:13 PM
 
echo "#!/bin/sh" > foo && echo "ls -al" >> foo && chmod +x foo && ./foo
     
NeoMac  (op)
Mac Elite
Join Date: Jan 2001
Status: Offline
Reply With Quote
May 9, 2001, 11:18 PM
 

Basic shell scripts are really very simple. Basically, you are creating a file that holds a series of command line commands.

In the simplest form, a shell script looks like this.

Code:
!#/bin/tcsh ls -al
The first line has to be the absolute first thing in the document. It points to the location of the shell executable. Since different shells have different syntax and functions, this points the script to the proper shell to use.

The rest of the script is a simple 'ls -al' command, which lists the all contents of the directory in long form.

Set the script to the execute permission (chmod +x filename) and that's it. It's that simple.

The script can be a series of simple CLI commands, or it can be more complex and handle standard output and input from the system or user.

Great stuff really.

This is the kind of stuff that really makes OSX outshine NT and XP, IMHO.
"Last time the French asked for more evidence, it rolled through France with a German flag." - David Letterman
     
Xeo
Moderator Emeritus
Join Date: Mar 2001
Location: Austin, MN, USA
Status: Offline
Reply With Quote
May 10, 2001, 12:00 AM
 
Say I make a script like this:

Code:
#!/bin/tcsh ls -al ls -x
What does it return?

Also, is there a way to make a script named foo that will simply return text?

Something like this?
Code:
#!/bin/tcsh [command to display text] "Foo is a cool word!"
I don't know if there is a command or something special to do this.

[edit: I had the "#" and the "!" switched around. And thanks for the help with "echo." That worked great!
------------------


[This message has been edited by Xeo (edited 05-10-2001).]
     
Fresh-Faced Recruit
Join Date: Mar 2001
Location: Neuchâtel, Switzerland
Status: Offline
Reply With Quote
May 10, 2001, 01:51 AM
 
Hi,

for returning text :

echo Hello world

Simply

--
Francis Olof Garnier
     
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status: Offline
Reply With Quote
May 11, 2001, 12:13 AM
 
Here is a good O'Rielly book that should turn any novice into a guru in little time:

"Using csh & tcsh"
Paul DuBois


iMac 17" G4 800MHZ & 768 SDRAM
     
   
Thread Tools
Forum Links
Forum 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 On
Top
Privacy Policy
All times are GMT -5. The time now is 10:02 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2