|
dreamsys software |
|
UNIX & Linux Shell Scripting Tutorial
BASH Shell
Variables
Variables are an important part of any program or script. A variable is a simple way to refer to a chunk of data in memory
that can be modified. A variable in a unix script can be assigned any type of value, such as a text string or a number. In unix
to create a variable, we simply put in our script:
VARIABLE_NAME=value
Note that we do not have to put the variable name in uppercase, but it is the standard way of naming variables in unix. The text
"VARIABLE_NAME" can be anything you want, as long as it only contains numbers, letters and/or an underscore "_". A variable name also
cannot start with a number. After the equal sign you put the value, there must be no space between the variable name and the equal sign.
To use the variable, we simply put a dollar sign "$" before the name of the variable in our script code. Let's revise our original script
to use the two words in two variables as such:
#!/bin/sh # This is my second script. VAR_1=Hello VAR_2=Unix echo "$VAR_1 $VAR_2"
This gives the same output as the original script but uses two variables. This is not a very exciting use of variable I will admit,
let's try something more interesting. Let's write a program that will open a file and read the head and tail of it. This can be useful
if you want to see the first items in your log and the last items in your log but you don't want to see the whole log file, which could be
very large. To write this program, we will make use of the unix commands head and tail. Our script will use a predefined varible
called $1. This variable will display the first item in the list of command line arguments to your script. You can also access any other command
line argument from $1 to $9. You can also use the variable $@ to access all of the command line arguments in a single variable.
#!/bin/sh # This program will print the head and tail of a file # passed in on the command line. echo "Printing head of $1..." head $1 echo "" #this prints an extra return... echo "Printing tail of $1..." tail $1
Give this program a name of ht.sh (for Head Tail shell script), be sure to give it execute permission and then run it with the following command:
./ht.sh WEB_LOG
The parameter WEB_LOG must point to an actual text file on your system, it will open the file and print the head and tail. Now let's assume
that we want the user to be able to input the file name after the script has been run. We can read input from the console by using the unix
read command. Let's modify the program and try it again: #!/bin/sh # This program will read the filename from user input. echo "Enter the file: " read FILENAME echo "Printing head of $FILENAME..." head $FILENAME echo "" #this prints an extra return... echo "Printing tail of $FILENAME..." tail $FILENAME
This way of printing a variable does have limitations, for instance you cannot print text without a space after the variable name.
Suppose that we want to create a script that will read the head and tail of the file as before, but we know that the user will always
be using files that end with "_LOG". If we tried to open the file $FILENAME_LOG, then it would look for a variable with that specific name
and not a file named $FILENAME + _LOG. We can fix this by using another way of displaying and using a variable. We use the format ${VARIABLE_NAME}.
Let's modify our program again to automatically append "_LOG" to the end of the filename for our users:
#!/bin/sh
# This program will read the filename from user input.
echo "Enter the file: "
read FILENAME
echo "Printing head of ${FILENAME}_LOG..."
head ${FILENAME}_LOG
echo "" #this prints an extra return...
echo "Printing tail of ${FILENAME}_LOG..."
tail ${FILENAME}_LOG
Now if you run the script with the command ./ht.sh WEB it will open the file "WEB_LOG" for the user. |
|