Reading Data into Variables

team bbl


You can store data into a variable from one of the following sources:

  • Assigning a value in the script using the equal sign (=). This method is discussed in the previous section.

  • Storing the output of a command using back ticks (`).

  • Passing the information on the command line when starting the script.

  • Prompting the user to enter data with the read command.

This section describes how to read data into variables using these methods.

The previous section discussed assigning simple values using the equal sign. You can also store arithmetic calculations in a variable using the let command, as follows:

 let sum=3+4 let total=total+1 let sum=$n1+$n2-3 

You can add (+), subtract (-), multiply (*), or divide (/). You can use modulus (%), which returns the remainder of a division (8%5 = 3).

You can store the output of a command using back ticks, as follows:

 today=`date` 

The shell executes the date command, storing the output in today, as shown below:

 echo $today Sun Jan   9 15:37:45 PST 2005 

You can pass variable values on the command line when you start the script, as follows:

 ./dir2file 21 33 44 

The values are stored in built-in shell script variables that are numbered, starting with $0. The above command results in the following variables:

 echo $0 dir2file echo $1 21 echo $2 33 echo $3 44 

Notice that the first information item read from the command line is the name of the script, which is stored in $0.

The simple script from the previous section is modified below to use information input at the command line:

 #!/bin/bash # Script Name: dir2file - A simple shell script that saves a directory listing in a file ls -l > dirlist echo $1 

To pass in the value for the variable $1, run the script as follows:

 ./dir2file "Directory is saved" Directory is saved 

The same script is modified below to request information from the user:

 #!/bin/bash # Script Name: dir2file - A simple shell script that saves a directory listing in a file echo -n "Enter text: " read status ls -l > dirlist echo $status 

When the script reaches the third line (echo -n), it echoes the text. When it reaches the next line (read), it waits for input from the keyboard. When the user presses <Enter>, the remaining two lines are executed. The session looks as follows:

 ./dir2file Enter text: Directory is saved Directory is saved 

The bold text is typed by the person using the script. The nonbold text is displayed by the script.

Shell scripts also provide built-in variables that you can use. One useful variable is HOME that contains the path to the home directory of the account that is running the script. Another variable is PWD, which contains the path to the current directory. To see a description of all the built-in variables, look at the man page for builtin (man builtins). You can use built-in variables the same way you use your own created variables:

 echo $HOME 

    team bbl



    Spring Into Linux
    Spring Into Linux
    ISBN: 0131853546
    EAN: 2147483647
    Year: 2005
    Pages: 362
    Authors: Janet Valade

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net