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

  • Thread starter Surrealist
  • Start date
  • Tags
    Shell Unix
In summary, the person is trying to learn how to write a generic Unix shell script to run four commands in a row. They want to name the script "ltxprc.sh" and make it work for any filename. They are advised to modify the script to access command line arguments and make it executable using a "shebang" line. The # symbol is explained as being used to indicate what program should be executed to run the script.
  • #1
Surrealist
48
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
  • #2
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
 
  • #3
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.
 
  • #4
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:

1. What is "ltxprc.sh" and what does it do?

"ltxprc.sh" is a generic Unix shell script that can be used to automate tasks in the Unix operating system. It is designed to be easily customized for specific needs and can be used to write a variety of different scripts.

2. Can you explain the 4 easy steps to writing a generic Unix shell script with ltxprc.sh?

Step 1: Download and install ltxprc.sh on your Unix system.Step 2: Create a new file and name it with a .sh extension.Step 3: Write your script using the basic structure provided in ltxprc.sh.Step 4: Save and run your script to test its functionality.

3. What are the benefits of using ltxprc.sh to write shell scripts?

Using ltxprc.sh can save time and effort when writing shell scripts, as it provides a basic structure and guidelines for creating a well-organized and functional script. It also allows for easy customization and can be used for a variety of tasks.

4. Do I need to have prior experience with Unix to use ltxprc.sh?

While prior experience with Unix can be helpful, it is not necessary to use ltxprc.sh. The script is designed to be user-friendly and can be used by beginners as well as experienced users.

5. Are there any limitations to what can be done with ltxprc.sh?

There are no inherent limitations to what can be done with ltxprc.sh, as it is a flexible and customizable script. However, it is important to have a basic understanding of Unix and shell scripting in order to use it effectively.

Similar threads

  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
33
Views
2K
  • Programming and Computer Science
Replies
9
Views
852
  • Programming and Computer Science
Replies
1
Views
639
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
13
Views
1K
Replies
16
Views
2K
  • Programming and Computer Science
Replies
6
Views
5K
Back
Top