Register to reply

Unix Shell Script

by Surrealist
Tags: script, shell, unix
Share this thread:
Surrealist
#1
May24-07, 06:40 PM
P: 63
I have never written a shell script, but I am trying to learn. I want to make a generic Unix shell script that will allow me to run four commands in a row... something like this...

latex $filename
bibtex $filename
latex $filename
latex $filename

I would like to call the script "ltxprc.sh".

How would I make this file work for any filename?

For instance, if my file name were "paper", would I run a command like...

sh ltxprc.sh

where do I input the filename?
Phys.Org News Partner Science news on Phys.org
Mysterious source of ozone-depleting chemical baffles NASA
Water leads to chemical that gunks up biofuels production
How lizards regenerate their tails: Researchers discover genetic 'recipe'
D H
#2
May24-07, 06:55 PM
Mentor
P: 15,153
First, you need to modify your script so it accesses the command line arguments sent to the script:

filename=$1

You pass the filename on the command line:

sh ltxprc.sh file.tex

Even better is to make the shell script executable via chmod. You will need to add a "shebang" line to the very front of your script:

#!/bin/sh

When you do that, all you need to say is

ltxprc.sh file.tex
Surrealist
#3
May25-07, 02:26 PM
P: 63
Thanks for the help. That was probably the most useful response I have ever received from a message board.

I gotta ask... why is it that the #!/bin/sh works? I thought that the # symbol meant "comment out" that which follows.

D H
#4
May25-07, 03:09 PM
Mentor
P: 15,153
Unix Shell Script

Quote Quote by Surrealist View Post
I gotta ask... why is it that the #!/bin/sh works? I thought that the # symbol meant "comment out" that which follows.
The # symbol is indeed a comment to the shell. The shell, however, is not what runs runs programs (directly, that is). The exec family of functions are what run programs on a Unix machine. The first thing exec does when asked to run a program is read the first two bytes of the program. When those first two bytes are "#!", exec does something quite special: It uses the line to indicate what program should be executed to run the script file. For example, you used #!/bin/sh . The #! tells exec this is an executable script. The exec invokes /bin/sh to run the script. Now /bin/sh reads lines from the script. Now we get back to your question: the first line is just a comment; /bin/sh does nothing with it.

For more info, see http://homepages.cwi.nl/~aeb/std/hashexclam.html and http://en.wikipedia.org/wiki/Shebang_%28Unix%29.


Register to reply

Related Discussions
Shell scripting in C (unix) Programming & Computer Science 19
Script similar to [tex] General Discussion 2
Post script Computing & Technology 2