Learn How to Write a Generic Unix Shell Script in 4 Easy Steps with ltxprc.sh

  • Thread starter Thread starter Surrealist
  • Start date Start date
  • Tags Tags
    Shell Unix
Click For Summary

Discussion Overview

The discussion revolves around creating a generic Unix shell script named "ltxprc.sh" that executes a series of commands for processing a LaTeX file. Participants explore how to pass a filename as an argument to the script and clarify the purpose of the shebang line in shell scripts.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Homework-related

Main Points Raised

  • One participant seeks guidance on writing a shell script that runs four LaTeX commands sequentially for any given filename.
  • Another participant suggests modifying the script to accept command line arguments, specifically using "filename=$1" to capture the filename passed when executing the script.
  • A suggestion is made to make the script executable with "chmod" and to include a shebang line at the top of the script for proper execution.
  • One participant expresses curiosity about the functionality of the shebang line, questioning why "#!/bin/sh" works when "#" typically denotes a comment.
  • A later reply explains that the shebang line is a special case where the exec function recognizes it as an indication of which interpreter to use for executing the script.

Areas of Agreement / Disagreement

Participants generally agree on the technical aspects of modifying the script to accept command line arguments and the purpose of the shebang line, though there is some curiosity and clarification sought regarding its functionality.

Contextual Notes

Some assumptions about the user's familiarity with Unix commands and scripting may not be fully addressed, and the discussion does not resolve all potential questions about script execution nuances.

Who May Find This Useful

Individuals interested in learning about Unix shell scripting, particularly those looking to automate tasks related to LaTeX document processing.

Surrealist
Messages
47
Reaction score
0
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?
 
Technology news on Phys.org
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
 
Thanks for the help. That was probably the most useful response I have ever received from a message board.

I got to ask... why is it that the #!/bin/sh works? I thought that the # symbol meant "comment out" that which follows.
 
Surrealist said:
I got to 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" .
 
Last edited by a moderator:

Similar threads

  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
33
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
12K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 13 ·
Replies
13
Views
2K
Replies
1
Views
7K