|
Shell Scripts or Programs are text files containing instructions / commands to do any specific task in a given shell. For HP-UX the default shell is POSIX. Other shells like Bourne, Korn and C are also supported.
To write a script, you will need to know how to use a text editor like vi. Also once you create the text file, you will need to change the permissions so that this file is executeable
$chmod u+x file_name
will change the permission on file file_name to executable. To execute the file, do the following:
$ ./file_name
First Script: Basic Structure
Use cat command or text editor to create the file
$cat > script01
#!/usr/bin/sh
# symbol # is used for comment (this will be ingored by interpreter)
echo "-------------------------------"
echo "This is my first script"
echo "-------------------------------"
Now save the file (use ^d if using cat). Make the file executable and run it:
$chmod u+x script01
$./script01
You will see the following output:
------------------------------- This is my first script -------------------------------
2nd Script: Using Variables
Variables can be set and used same way as on command line.
Environment variables can be used and changed inside the script but no changes can be seen outside the script during or after the execution.
Try the following script
cat > script02
#!/usr/bin/sh # example of use of variables echo "Variable use in Scripts" echo "-------------------------------" variable1=bird variable2=air echo "$variable1 flies in the $variable2"
echo "------------------------------"
Value of "bird" was assigned to variable variable1. To show the value of the variables, use $ sign, e.g., echo $variable1 returns bird. Run the script
$chmod u+x script02
$./script02
You will see the following resutls
Variable use in Scripts ------------------------------- bird flies in the air ------------------------------
|