Shell Script Execution UNIX Shell Scripts

3.5.1 UNIX User Shell

UNIX user shell is an interface layer between the UNIX operating system and the user. It is presented in the Figure 3.1. Figure 3.1: The users shell layer. There are many different UNIX shell flavors: Bourne shell sh, Korn shell ksh, C shell csh, Bourne again shell bash, enhanced C shell tcsh, etc. Some shells are very similar — like ksh and bash, sh is the subset of ksh — but generally they are not mutually compatible at least in both directions. This is important to know when a shell script is invoked.

3.5.2 UNIX Shell Scripts

Shell scripts are programs that comply with the shell programming language. Shell scripts are not compiled programs; instead they are readable text files where each command line is read and processed by the shell command interpreter at the time the script is executed. Shell command interpreter processes a shell script until an erroneous command line is encountered or until it ends. A shell command line can contain: Any UNIX command or command sequence • Any shell−flavored command or statement • Any other program or shell script • A combination of previously listed items • Each shell has a number of its own commands and statements that actually make shell programming so powerful. Make sure that they are very shell−specific in every sense: syntax and action.

3.5.2.1 Shell Script Execution

A shell script as any other program in UNIX can be simply invoked by its name, but the read and execute permissions for the script are required. The following example illustrates this: sh cat tmpMyScript.sh to see content 80 echo Just a test of x permission sh ls −l tmpMyScript.sh to see permissions −rw−r−−r−− 1 root root 39 Aug 21 18:27tmpMyScript.sh sh tmpMyScript.sh to invoke shell script sh: tmptest4.sh: Permission denied The script can also be invoked with an explicitly specified shell. In that case the execute permission on the script is not mandatory. Some UNIX flavors will execute a shell script even without read permission granted. sh binsh tmpMyScript.sh Just a test of x permission When invoked directly, the shell script is executed in the environment of the current user shell. The current user shell is forked, and then each command line of the shell script is processed by the shell interpreter and executed already discussed fork−and−exec start of the program. If two shell flavors do not match the shell script and the parent shell — for example bash script is invoked in csh environment, most probably a number of errors will be encountered for basically correct shell script. The following examples present such situations. The arbitrary bash script named myscript.bash is invoked in the bash and csh environment: bash cat tmpmyscript.bash Define variables export TEXT1 = This is a bash script myscript.bash export TEXT2=Running the script myscript.bash Run the command echo TEXT1 echo TEXT2 bash tmpmyscript.bash This is a bash script myscript.bash Running the script myscript.bash bash bincsh Switch to csh csh tmpmyscript.bash export: Command not found. export: Command not found. TEXT1: Undefined variable. The previous problematic situation could be skipped in two ways. First, as we mentioned previously, the script can be invoked with explicitly specified shell: bash binbash tmpmyscript.bash Here shells match This is a bash script myscript.bash Running the script myscript.bash csh binbash tmpmyscript.bash Here shells dont match This is a bash script myscript.bash Running the script myscript.bash 81 bash cat tmpmyscript1.bash binbash Define variables export TEXT1=This is a bash script myscript1.bash export TEXT2=Running the script myscript1.bash Run the command echo TEXT1 echo TEXT2 bash tmpmyscript1.bash This is a bash script myscript1.bash Running the script myscript1.bash csh tmpmyscript1.bash This is a bash script myscript1.bash Running the script myscript1.bash In all the examples, the current shell spawns itself or another shell, making a parent–child relationship between two shells current users shell and the invoked shell script. However, a shell script can also be executed directly in the users shell environment. For this purpose the shell script must be sourced. A special shell command is used to source the script. source myscript.sh for csh and csh−like shells. .myscript.sh for ksh, bash, and Bourne shells To source a shell script means to skip the forking of the users shell and to execute the script directly in the users shell environment.

3.5.2.2 Shell Variables