Linux Bible. Christopher Negus

Чтение книги онлайн.

Читать онлайн книгу Linux Bible - Christopher Negus страница 68

Linux Bible - Christopher Negus

Скачать книгу

which contained the following:

       #!/bin/bash # Script to echo out command-line arguments echo "The first argument is $1, the second is $2." echo "The command itself is called $0." echo "There are $# parameters on your command line" echo "Here are all the arguments: $@"

      Assuming that the script is executable and located in a directory in your $PATH, the following shows what would happen if you ran that command with foo and bar as arguments:

       $ chmod 755 /home/chris/bin/myscript $ myscript foo bar The first argument is foo, the second is bar. The command itself is called /home/chris/bin/myscript. There are 2 parameters on your command line Here are all the arguments: foo bar

      Another variable, $#, tells you how many parameters your script was given. In the example, $# would be 2. The $@ variable holds all of the arguments entered at the command line. Another particularly useful special shell variable is $?, which receives the exit status of the last command executed. Typically, a value of zero means that the command exited successfully, and anything other than zero indicates an error of some kind. For a complete list of special shell variables, refer to the bash man page.

      Reading in parameters

      Using the read command, you can prompt the user for information and store that information to use later in your script. Here's an example of a script that uses the read command:

       #!/bin/bash read -p "Type in an adjective, noun and verb (past tense): " adj1 noun1 verb1 echo "He sighed and $verb1 to the elixir. Then he ate the $adj1 $noun1."

      In this script, after the script prompts for an adjective, noun, and verb, the user is expected to enter words that are then assigned to the adj1, noun1, and verb1 variables. Those three variables are then included in a silly sentence, which is displayed on the screen. If the script were called sillyscript, here's an example of how it might run:

       $ chmod 755 /home/chris/bin/sillyscript $ sillyscript Type in an adjective, noun and verb (past tense): hairy football danced He sighed and danced to the elixir. Then he ate the hairy football.

      Parameter expansion in bash

      As mentioned earlier, if you want the value of a variable, you precede it with a $ (for example, $CITY). This is really just shorthand for the notation ${CITY}; curly braces are used when the value of the parameter needs to be placed next to other text without a space. Bash has special rules that allow you to expand the value of a variable in different ways. Going into all of the rules is probably overkill for a quick introduction to shell scripts, but the following list presents some common constructs you're likely to see in bash scripts that you find on your Linux system.

       ${var:-value}: If variable is unset or empty, expand this to value.

       ${var#pattern}: Chop the shortest match for pattern from the front of var's value.

       ${var##pattern}: Chop the longest match for pattern from the front of var's value.

       ${var%pattern}: Chop the shortest match for pattern from the end of var's value.

       ${var%%pattern}: Chop the longest match for pattern from the end of var's value.

       $ THIS="Example" $ THIS=${THIS:-"Not Set"} $ THAT=${THAT:-"Not Set"} $ echo $THIS Example $ echo $THAT Not Set

      In the examples here, the THIS variable is initially set to the word Example. In the next two lines, the THIS and THAT variables are set to their current values or to Not Set, if they are not currently set. Notice that because I just set THIS to the string Example, when I echo the value of THIS it appears as Example. However, because THAT was not set, it appears as Not Set.

      NOTE

      For the rest of this section, I show how variables and commands may appear in a shell script. To try out any of those examples, however, you can simply type them into a shell, as shown in the previous example.

      In the following example, MYFILENAME is set to /home/digby/myfile.txt. Next, the FILE variable is set to myfile.txt and DIR is set to /home/digby. In the NAME variable, the filename is cut down simply to myfile; then, in the EXTENSION variable, the file extension is set to txt. (To try these out, you can type them at a shell prompt as in the previous example and echo the value of each variable to see how it is set.) Type the code on the left. The material on the right side describes the action.

       MYFILENAME=/home/digby/myfile.txt: Sets the value of MYFILENAME

       FILE=${MYFILENAME##*/}: FILE becomes myfile.txt

       DIR=${MYFILENAME%/*}: DIR becomes /home/digby

       NAME=${FILE%.*}: NAME becomes myfile

       EXTENSION=${FILE##*.}: EXTENSION becomes txt

      Performing arithmetic in shell scripts

      Bash uses untyped variables, meaning it normally treats variables as strings of text, but you can change them on the fly if you want it to.

      Bash uses untyped variables, meaning that you are not required to specify whether a variable is text or numbers. It normally treats variables as strings of text, so unless you tell it otherwise with declare, your variables are just a bunch of letters to bash. However, when you start trying to do arithmetic with them, bash converts them to integers if it can. This makes it possible to do some fairly complex arithmetic in bash.

       BIGNUM=1024 let RESULT=$BIGNUM/16 RESULT=`expr $BIGNUM / 16` RESULT=`echo "$BIGNUM / 16" | bc` let foo=$RANDOM; echo $foo

      Another way to grow a variable incrementally is to use $(()) notation with ++I added to increment the value of I. Try typing the following:

       $ I=0 $ echo "The value of I after increment is $((++I))" The value of I after increment is 1 $ echo "The value of I before and after increment is $((I++)) and $I" The value

Скачать книгу