![]() | Once a shell script is created, there are several ways to execute it. However, before any Korn shell script be can executed, it must be assigned the proper permissions. The chmod command changes permissions on individual files, in this case giving execute permission to the simple file: $ chmod +x simple After that, you can execute the script by specifying the filename as an argument to the ksh command: $ ksh simple You can also execute scripts by just typing its name alone. However, for that method to work, the directory containing the script must be defined in your PATH variable. When looking at your .profile earlier in the course, you may have noticed that the PATH=$PATH:$HOME definition was already in place. This enables you to run scripts located in your home directory ($HOME) without using the ksh command. For instance, because of that pre-defined PATH variable, the simple script can be run from the command line like this: $ simple (For the purposes of this course, we'll simplify things by running all scripts by their script name only, not as an argument to the ksh command.) You can also invoke the script from your current shell by opening a background subprocess - or subshell - where the actual command processing will occur. You won't see it running, but it will free up your existing shell so you can continue working. This is really only necessary when running long, processing-intensive scripts that would otherwise take over your current shell until they complete. To run the script you created in the background, invoke it this way: $ simple & When the script completes, you'll see output similar to this in the current shell: [1] - Done (127) simple |
![]() | It is important to understand that Korn shell scripts run in a somewhat different way than they would in other shells. Specifically, variables defined in the Korn shell aren't understood outside of the defining - or parent - shell. They must be explicitly exported from the parent shell to work in a subsequent script or subshell. If you use the export or typeset -x commands to make the variable known outside the parent shell, any subshell will automatically inherit the values you defined for the parent shell. For example, here's a script named lookmeup that does nothing more than print a line to standard output using the myaddress (defined as 123 Anystreet USA) variable: $ cat lookmeup print "I live at $myaddress" If you open a new shell (using the ksh command) from the parent shell and run the script, you see that myaddress is undefined: $ ksh $ lookmeup I live at $ However, if you export myaddress from the parent shell: $ exit $ export myaddress and then open a new shell and run the lookmeup script again, the variable is now defined: $ ksh $ lookmeup I live at 123 Anystreet USA To illustrate further how the parent shell takes processing precedence, let's change the value of myaddress in the subshell: $ myaddress='Houston, Texas' $ print $myaddress Houston, Texas Now, if you exit the new shell and go back to the parent shell and type the same command: $ exit $ print $myaddress 123 Anystreet USA you see that the original value in the parent shell was not affected by what you did in the subshell. A way to export variables automatically is to use the set -o allexport command. This command cannot export variables to the parent shell from a subshell, but can export variables created in the parent shell to all subshells created after the command is run. Likewise, it can automatically export variables created in subshells to new subshells created after running the command. set -o allexport is a handy command to place in your .kshrc file. |
|
If a script is writing output to a file, you can interactively monitor the progress of the output file (and thereby the script) by using the tail -f outputfile command. To quit the tail command, use Ctrl-C. |
| * You are viewing a page from LiveFire Labs' sample online UNIX training course. Click here if you do not see a blue and green frame around this page, or view the detailed description for this course. LiveFire Labs - Online UNIX Training |