ITS 2310 - Linux I


Shell Scripting Review

Objectives:

This lesson discusses shell scripting again. Objectives important to this lesson:

  1. Variables
  2. Shell operators
  3. Back quotes
  4. Command line arguments
Concepts:
The course lecture for this week brings us back to shell scripting.

A shell script is written in commands understood by your shell of choice (in our case, bash) and  interpreted by the shell at run time, rather than being compiled like a C program would be.  If you have tasks that you perform on a regular basis, or if you need a more complex set  of operations done, a shell script will allow you to save those customized sets of commands for repeated use. 

You will examine and analyze existing shell scripts to understand the basic building  blocks, then work to create simple scripts of your own.

Variables

A variable is like a container that holds an assigned value. Many variables already exist  and are created by the shell automatically to keep track of your information, such as your home directory, your default login shell, and your current directory. These are  environment and configuration variables. You can see these in the shell by using the env command or the printenv command to send them to a printer or a file. You can also create your own variables as you need them. These are known as shell variables. A shell variable is only available in the script or shell where it's currently defined unless, you use the export command to make it available to other shells. 

To set a variable in bash, the syntax is variable=value. So, to set a variable named V2 to  the value of "snow shoes", you would issue this command:  V2="snow shoes". To view the current value of any given variable, use the echo command and precede the  name of the variable with a $ sign: echo $V2  How is the shell variable useful? A common use is in shell scripts, where you can define  a variable once at the beginning of the script, and then use that variable throughout the  script. For instance, if you had a script that called for a log file to be updated numerous  times throughout the process, it might be helpful to set a variable right away, like:  logfile=/var/local/testusers/fall2012/userlogs.txt  It will be a lot easier to refer to $logfile ten times in the script than  /var/local/testusers/fall2012/userlogs.txt, and if the location of the log file ever changes, you only have to change it in one place in the script. 


Shell operators 

  • Defining operators give values to variables. Example: = . The equal sign is the assignment operator.
  • Evaluating operators let you view the value of a variable. Example: $ 
  • Arithmetic and relational operators allow you to perform arithmetic operations and  comparisons of values. Example: + 
  • Redirection operators let you control input and output when you don't want standard input or output. Example: > 

Back quotes 

The back quote (or accent grave): `. It looks like a left-leaning apostrophe and is on the same key as the tilde ~ on your keyboard. A command will execute inside the back quotes, and its results can be used as arguments to other commands, or to set the value of a  variable:
variable99=`ls -l | wc -l`

Programming logic 

Sequential logic works through a list of commands in order.  Decision logic executes commands based on conditions being met.  Looping logic repeats a routine until it is triggered to stop.  Case logic allows multiple choices to match multiple responses (often used to make  computer menus). 

Command-line arguments 

Sometimes you will need to give more information than just the name of the script at run time. The additional pieces of information can be command-line arguments. There are special variables for shell scripting that refer to those command-line arguments.

$0 refers to the name of the script itself, $1 refers to the first argument, $2 refers to the second argument, and so on. If you should need to use double digits, they must be  enclosed in brackets, like ${10}.  So, if your command line looks like this
myscript somefile.txt otherfile.txt 
myscript is $0 (the shell script you are running), somefile.txt is $1 (your first command-line argument), otherfile.txt is $2 (your second command-line argument).  Then the script only need refer to $1 for somefile.txt to be included in that part of the script, $2 for otherfile.txt, and so on. 

Login scripts

Sometimes you'll want to make changes to your environment that last beyond your current login session. You might have variables that you want set for you each time, or aliases for long commands. You can add these changes to your login scripts, which execute when you log in.  The file .bash_profile is executed during your initial login to the server. If you want your  changes to follow you if you launch additional shells on top of your login shell, you should also make your changes to the .bashrc file, which is executed each time you  launch a new instance of bash on top of your initial login shell.  Make sure you are confident of what you're doing when you make changes to your login  scripts. Making a backup copy before editing is never a bad idea.

Running scripts

In theory, you should be able to run a script simply by typing its name (and any  command-line arguments that might be required). However, the shell has places it knows to look for executables and scripts; if you're not in one of those places, the shell has no  idea where to look for your script, even if you're issuing the command from the same directory where the script is stored.  There are several ways to let the shell know where the script is. 

The simplest way is to run your script with a ./ (period, forward slash) to tell the  shell "The script is in my current directory."
./scriptname

You can add the current directory to your PATH variable temporarily (it will be  good for your current login session). Issue this command: PATH=$PATH:. 
Yes, you need the colon and the dot, or PATH wont remember the current directory as a new place.
This keeps all the values currently in your PATH variable, and adds the current  directory by adding the period at the end. You can also permanently alter your PATH variable in your .bash_profile login  script. Only do this if you are confident in making changes to your configuration  files, and always make a backup copy before editing configuration files.  Look in .bash_profile for a line reading export PATH and open a new line above it. On the new line, add  PATH=$PATH:.  and save the file. When you log back in, it will take effect. You can refer to the script with an absolute path.