Red Hat Linux 7 Unleashed

Red Hat Linux 7 Unleashed

By William Ball

Creating and Executing a Shell Program

Say you want to set up a number of aliases whenever you log on. Instead of typing all of the aliases every time you log on, you can put them in a file by using a text editor, such as vi, and then execute the file.

Here is what is contained in myenv, a sample file created for this purpose (for bash):

#!/bin/sh
alias ll='ls -l'
alias dir='ls'
alias copy='cp'

myenv can be executed in a variety of ways under Linux.

You can make myenv executable by using the chmod command, as follows, and then execute it as you would any other native Linux command:


   # chmod +x myenv

This turns on the executable permission of myenv. You need to ensure one more thing before you can execute myenv—it must be in the search path. You can get the search path by executing


   # echo $PATH

If the directory where the file myenv is located is not in the current search path, you must add the directory name in the search path.

Now you can execute the file myenv from the command line as if it were a Linux command:


   # myenv

A second way to execute myenv under a particular shell, such as pdksh, is as follows:


   # pdksh myenv

This invokes a new pdksh shell and passes the filename myenv as a parameter to execute the file.

You can also execute myenv from the command line as follows:

Command Line Environment
# . myenv pdksh and bash
# source myenv tcsh

The dot (.) is a way of telling the shell to execute the file myenv. In this case, you do not have to ensure that the execute permission of the file has been set. Under tcsh, you have to use the source command instead of the dot (.) command.

After you execute the command myenv, you should be able to use dir from the command line to get a list of files under the current directory and ll to get a list of files with various attributes displayed. However, the best way to use the new commands in myenv is to put them into your shell's login or profile file. For Red Hat Linux users, the default shell is bash, so make these commands available for everyone on your system by putting them in the /etc/profile.d directory. Copy in a tcsh version with the file extension .csh and the bash/pdksh version with a .sh file extension.

In some instances, you may need to modify how your shell scripts are executed. For example, the majority of shell scripts use a hash-bang line at the beginning, like this:

#!/bin/sh

One of the reasons for this is to control the type of shell used to run the script (in this case, an sh-incantation of bash). Other shells, such as ksh, may respond differently depending on how they're called from a script (hence the reason for symbolic links to different shells).

You may also find different or new environment variables available to your scripts by using different shells. For example, if you launch csh from the bash command line, you'll find at least several new variables, or variables with slightly occluded definitions, such as


   # env
...
VENDOR=intel
MACHTYPE=i386
HOSTTYPE=i386-linux
HOST=thinkpad.home.org

On the other hand, bash may provide these variables, or variables of the same name with a slightly different definition, such as


   # env
...
HOSTTYPE=i386
HOSTNAME=thinkpad.home.org

Although the behavior of a bang line is not defined by POSIX, variations of its incantation can be helpful when you're writing shell scripts. As described in the wish man page, you can use a shell to help execute programs called within a shell script without needing to hard-code pathnames of programs. This increases shell script portability.

For example, if you want to use the wish command (a windowing tcl interpreter), your first inclination may be to write

#!/usr/local/bin/wish

Although this will work on many other operating systems, the script will fail under Linux. However, if you use

#!/bin/sh
exec wish "$@"

the wish command (as a binary or itself a shell script) can be used. There are other advantages to using this approach. See the wish man page for more information.

Share ThisShare This

Informit Network