Академический Документы
Профессиональный Документы
Культура Документы
Standard Output
Linux assumes that all output is going to some kind of file. To Linux, the screen is a file called /dev/tty. printf Sales are up > /dev/tty # display on the screen Bash uses the symbol &1 to refer to standard output, and you can explicitly redirect messages to it.
Standard Output
printf Sales are up > results.txt # sent to a file on disk printf Sales are up > /dev/tty # send explicitly to the screen printf Sales are up # sent to screen via standard output printf Sales are up >&1 # same as the last one printf Sales are up >/dev/stdout # same as the last one /dev/stdout is another name for the standard output file. The last three examples are identical. Make sure &1 is directly beside the redirect symbol, with no intervening spaces. 3
Standard Output
$ bash listorders.sh > listing.txt Inside the script, standard output no longer refers to the screen. Instead, standard output refers to the file youve redirected the output to, in this case listing.txt. ls -l incoming/orders # listing saved in listing.txt
ls -l incoming/orders 1>&1 # listing saved in listing.txt ls -l incoming/orders > /dev/tty # listing displayed on screen
Standard Error
Linux defines a second file especially for messages intended for the user called standard error. This file represents the destination for all error messages. The symbol for standard error is &2. /dev/stderr can also be used. The default destination, like standard output, is the screen. For example, printf $SCRIPT:$LINENO: processing >&2 No files available for
Standard Error
Because standard error, like standard output, is a kind of renaming of another destination, standard error can likewise be redirected. The redirection symbols for standard error are the same as standard output except they begin with the number 2. $ bash listorders.sh 2> listorders_errors.txt In this example, all the error messages from the listorders.sh script are saved in the file listorders_errors.txt.
Standard Input
Linux treats all input as if it was being read from a file. This special file is called standard input, and uses the symbol &0. /dev/stdin can also be used for standard input. When commands are joined together with the | symbol, the standard input of the second command becomes the standard output of the first command.
10
11
12
13
Variables
The results of commands can be written to a file or saved in variables. Because variables are saved in memory, they tend to be faster to examine than files. Bash doesnt put an upper limit on the size of a variable. They are large enough to contain anything you will ever need to hold. Variables are declared using the Bash declare command. To declare a variable named COST, use this: $ declare COST
14
Variables
Although variables can be in upper- or lowercase, tradition dictates variables are named in uppercase so as not to be confused with shell commands, which are almost always in lowercase. TOTAL, ORDERS_EUROPE, and _W3C are all legitimate variable names. There are no reserved words, which are words that are reserved for a specific purpose in the shell. Variables are assigned new values with an equals sign (=). To assign an empty string to a variable, dont supply any value. $ COST= Otherwise, include some text to be assigned. $ COST=0
15
Variables
Because declare is a command, variables are created only when the declare command is executed. They remain in existence until the script ends or until the variable is destroyed with the built-in unset command. $ unset COST
16
Variables
The results of a command can also be assigned to a variable. If a command is contained in backquotes (), everything written to standard output is stored in the variable being assigned instead. $ declare NUMBER_OF_FILES $ NUMBER_OF_FILES=ls -1 | wc -l $ printf %d $NUMBER_OF_FILES 14
17
Variables
Bash has more than 50 predefined variables. These variables, created when Bash is first started, provide information about the Bash session and can be used to control some of the shells features. Some of these variables have special properties that might be lost if you unset the variable and then create a new one with the same name. For example, the variable RANDOM contains a random number. If you delete RANDOM with unset and declare a new variable called RANDOM, this new variable is a normal shell variable and does not contain random numbers. Therefore, its best to avoid creating variables with the same name as the predefined variables.
18
19
$ printf %s $DISTRIBUTION_CENTERS London ; Paris ; New York It is a safe practice to always enclose variable substitutions with quotes.
20
21
22
Variable Attributes
If a variable is declared with the -i (integer) switch, Bash turns on the integer attribute for that variable. The shell will remember that the string should be treated as an integer value. If a non-numeric value is assigned to an integer variable, Bash does not report an error but instead assigns a value of zero. $ declare -i NUMBER_ACCOUNTS=15 $ printf %d\n $NUMBER_ACCOUNTS 15 $ NUMBER_ACCOUNTS=Smith # mistake $ printf %d\n $NUMBER_ACCOUNTS 0
23
Arrays
Arrays are lists of values that are created with the -a (array) attribute. A number called an index refers to the position item in the array. Bash arrays differ from arrays in other computer languages because they are openended. Arrays can be any length and are initially filled with empty strings for items. $ declare -a PRODUCTS
24
Arrays
New items are assigned to the array using square brackets to indicate the position in the list. The first position is position zero (not one). If an initial value is specified, it is assigned to the first position. Assigning one value is not particularly useful but is included for compatibility with other shells. Alternatively, the initial values can be assigned to specific positions by including a position in square brackets. $ declare -a DEPT[0]=accounting DEPT [1]=shipping \ DEPT [2]=customer service
25
Exporting Variables
Shell variables exist in the script or interactive sessions only where they were declared. In order to make shell variables available outside of their place of origin, they have to be declared as exportable. Variables are marked as exportable with the export attribute using the declare -x (export) switch. The export attribute reminds the shell that you want to export, or provide the variable, to all programs run by the script. For example, the program CVS requires a variable called CVSROOT to exist for all its programs. $ declare -x CVSROOT=/home/cvs/cvsroot
26
27
Expressions
An expression is a formula that calculates a value. Bash has several built-in commands and functions to compute expressions, and not all have the same syntax or features. In some cases there is more than one way to calculate the same expression. There are also many specialized features for use in rare cases. As a result, few Bash programmers have all the nuances memorized.
28
Expressions
There are two common built-in commands that interpret expressions.
29
Assignment (5 points)
Write a shell script to provide the following output. ~/tmp not found Deadline: December 4th, 10:00 AM How to deliver?! Via email to memarypour@gmail.com This assignment is mandatory!
30