Shell Programming in Unix Operation, Variables in Unix, Environment Variables, read & readonly command, the .profile Command, Command line Arguments in Unix System
SHELL PROGRAMMING :
Shell Programming:
- When groups of command have to be executed regularly, they should be stored in a file, and the file itself executed as a shell script or a shell program by the user.
- A shell program runs in interpretive mode. It is not complied with a separate executable file as with a C program but each statement is loaded into memory when it is to be executed
- Hence shell scripts run slower than the programs written in high-level language. .sh is used as an extension for shell scripts.
- However the use of extension is not mandatory. Shell scripts are executed in a separate child shell process which may or may not be same as the login shell.
script.sh#! /bin/sh# script.sh: Sample Shell Scriptecho “Welcome to Shell Programming”echo “Today’s date : `date`”echo “This months calendar:”cal `date “+%m 20%y”`echo “My Shell :$ SHELL”
- The # character indicates the comments in the shell script and all the characters that follow the # symbol are ignored by the shell.
- However, this does not apply to the first line which beings with #. This because, it is an interpreter line which always begins with #! followed by the pathname of the shell to be used for running the script.
- In the above example the first line indicates that we are using a Bourne Shell.
To run the script we need to first make it executable. This is achieved by using the chmod command as shown below:
$ chmod +x script.sh
Then invoke the script name as:
Once this is done, we can see the following output :$ script.sh
As stated above the child shell reads and executes each statement in interpretive mode. We can also explicitly spawn a child of your choice with the script name as argument:
sh script.sh
Variables:
- variables are used to provide information to the programs you use. You can have both global environment and local shell variables.
- Global environment variables are set by your login shell and new programs and shells inherit the environment of their parent shell.
- Local shell variables are used only by that shell and are not passed on to other processes. A child process cannot pass a variable back to its parent process.
- To declare a local shell variable we use the form variable=value (no spaces around =) and its evaluation requires the $ as a prefix to the variable.
$ count=5$ echo$count5
- A variable can be removed with unset and protected from reassignment by readonly. Both are shell internal commands.
Note: In C shell, we use set statement to set variables. Here, there either has to be whitespace on both sides of the = or none at all.
$ set count=5
$ set size = 10
Uses of local shell variables :
1. Setting pathnames: If a pathname is used several times in a script, we can assign it to a variable and use it as an argument to any command.
2. Using command substitution: We can assign the result of execution of a command to a variable. The command to be executed must be enclosed in backquotes.
3. Concatenating variables and strings: Two variables can be concatenated to form a new variable.
Example:
$ base=foo ; ext=.c
$ file=$base$ext
$ echo $file
// prints foo.c
Environment Variables:
- The environment variables are managed by the shell. As opposed to regular shell variables, environment variables are inherited by any program you start, including another shell.
- New processes are assigned a copy of these variables, which they can read, modify and pass on in turn to their own child processes.
- The set statement display all variables available in the current shell, but env command displays only environment variables.
- Note than env is an external command and runs in a child process. There is nothing special about the environment variable names. The convention is to use uppercase letters for naming one.
The command search path (PATH):
- The PATH variable instructs the shell about the route it should follow to locate any executable command.
Your home directory (HOME):
- When you log in, UNIX normally places you in a directory named after your login name.
- This is called the home directory or login directory. The home directory for a user is set by the system administrator while creating users (using useradd command).
mailbox location and checking (MAIL and MAILCHECK):
- The incoming mails for a user are generally stored at /var/mail or /var/spool/mail and this location is available in the environment variable MAIL.
- MAILCHECK determines how often the shell checks the file for arrival of new mail.
The prompt strings (PS1, PS2):
- The prompt that you normally see (the $ prompt) is the shell’s primary prompt specified by PS1. PS2 specifies the secondary prompt (>).
- You can change the prompt by assigning a new value to these environment variables.
Shell used by the commands with shell escapes (SHELL):
- This environment variable specifies the login shell as well as the shell that interprets the command if preceded with a shell escape.
Variables used in Bash and Korn:
- The Bash and korn prompt can do much more than displaying such simple information as your user name, the name of your machine and some indication about the present working directory.
$ PS1=‘[PWD] ‘[/home/srm] cd progs[/home/srm/progs] _
Bash and Korn also support a history facility that treats a previous command as an event and associates it with a number.
This event number is represented as !.
$ PS1=‘[!] ‘ $ PS1=‘[! $PWD] ‘
[42] _ [42 /home/srm/progs] _
$ PS1=“\h> “ // Host name of the machine saturn> _
Read and Readonly : Making scripts interactive :
- The read statement is the shell’s internal tool for making scripts interactive (i.e. taking input from the user). It is used with one or more variables.
- Inputs supplied with the standard input are read into these variables. For instance, the use of statement like
read name
causes the script to pause at that point to take input from the keyboard. Whatever is entered by you will be stored in the variable name.
Example: A shell script that uses read to take a search string and filename from the terminal.
#! /bin/sh
# emp1.sh: Interactive version, uses read to accept two inputs
#
echo “Enter the pattern to be searched: \c”
read pname
echo “Enter the file to be used: \c”
read fname
echo “Searching for pattern $pname from the file $fname”
grep $pname $fname
echo “Selected records shown above”
The .Profile :
- When logging into an interactive login shell, login will do the authentication, set the environment and start your shell. In the case of bash, the next step is reading the general profile from /etc, if that file exists. bash then looks for ~/.bash_profile, ~/.bash_login and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.
- If none exists, /etc/bashrc is applied. When a login shell exits, bash reads and executes commands from the file, ~/.bash_logout, if it exists.
- The profile contains commands that are meant to be executed only once in a session. It can also be used to customize the operating environment to suit user requirements.
- Every time you change the profile file, you should either log out and log in again or You can execute it by using a special command (called dot).
$ . .profile
Command Line Arguments :
- Shell scripts also accept arguments from the command line. Therefore e they can be run non interactively and be used with redirection and pipelines.
- The arguments are assigned to special shell variables. Represented by $1, $2, etc; similar to C command arguments argv[0], argv[1], etc.