 |
 |
Shell Scripting trouble - help!
|
 |
|
 |
|
Mac Elite
Join Date: Jan 2001
Status:
Offline
|
|
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
|
|
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
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Jan 2001
Status:
Offline
|
|
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
|
| |
|
|
|
 |
|
 |
|
Moderator Emeritus 
Join Date: Mar 2001
Location: Austin, MN, USA
Status:
Offline
|
|
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
|
|
echo "#!/bin/sh" > foo && echo "ls -al" >> foo && chmod +x foo && ./foo
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Jan 2001
Status:
Offline
|
|
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.
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
|
| |
|
|
|
 |
|
 |
|
Moderator Emeritus 
Join Date: Mar 2001
Location: Austin, MN, USA
Status:
Offline
|
|
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
|
|
Hi,
for returning text :
echo Hello world
Simply
|
|
--
Francis Olof Garnier
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status:
Offline
|
|
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
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|