Вы находитесь на странице: 1из 30

The Linux Shells Creating a Script Part II

Operating Systems Laboratory Amir Saman Memaripour

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.

Built-In Versus Linux Commands


The original Bourne shell was designed in a way that anything that was not an essential part of the shell was implemented as some program outside of the shell itself. Even arithmetic, for example, had to be performed by an outside program. This design made the Bourne shell very flexible, but it also introduced a couple of drawbacks.

1. It was slow because programs were constantly loading and


restarting to perform even the simplest of tasks.

2. It made shell scripts difficult to port because there were


no guarantees that a command on one operating system was implemented the same way on another; such commands might not even have been available.

Built-In Versus Linux Commands


To deal with these problems, Bash has many of its fundamental commands built-in. But for basic compatibility with the older Bourne shell, Linux still implements its own version of Bashs commands. For instance, test is a built-in command in Bash, but Linux also provides its own program /usr/bin/test for shells that dont provide a built-in version of test. If you dont know whether a command is built-in, the Bash type command will tell you. If the command is a Linux command, it shows the path of the command (like the Linux whereis command).

Built-In Versus Linux Commands


The builtin command explicitly runs a built-in command. The command runs even if theres an alias with the same name. builtin pwd Likewise, command explicitly runs a Linux command, even if theres a built-in command or alias with the same name. command pwd

10

Built-In Versus Linux Commands


Although built-in and command are useful in testing and porting older scripts to Bash, well-structured scripts should not rely on them because they indicate ambiguity in a scripts design. The built-in enable command temporarily hides the shell built-in commands and reenables them later. The -n switch disables the command.

11

Built-In Versus Linux Commands


$ enable test $ type test test is a shell builtin $ enable -n test $ type test test is /usr/bin/test

12

Built-In Versus Linux Commands


With -p, all enabled shell built-ins are printed. You can combine -p with -n to list the disabled built-ins, or use -a to show them all. The -s switch restricts the listing to POSIX special built-ins. $ enable pn enable -n test

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

The Effect of Quotations


$ DISTRIBUTION_CENTERS=London ; Paris ; New York

$ printf %s $DISTRIBUTION_CENTERS London;Paris;NewYork

$ printf %s $DISTRIBUTION_CENTERS London ; Paris ; New York

19

The Effect of Quotations


$ DISTRIBUTION_CENTERS=London ; Paris ; New York $ printf %s $DISTRIBUTION_CENTERS London;Paris;NewYork

$ printf %s $DISTRIBUTION_CENTERS London ; Paris ; New York It is a safe practice to always enclose variable substitutions with quotes.

20

The Effect of Quotations


Besides space interpretation, another effect of quotation marks is that no pattern matching is done. Normally, for example, the asterisk (*) represents all the files in the current directory. Quotation marks prevent the asterisk from being replaced with a list of files. $ printf %s\n * orders.txt archive calc.sh $ printf %s\n * *

21

The Effect of Quotations


To print strings without interpreting the special characters inside, use single quotes. Double quotes do not prevent Bash from interpreting the special characters $, , and \, but single quotes leave all characters unchanged. $ printf %s $TAX_MESSAGE $TAX_MESSAGE In this case, the single quotes prevent Bash from interpreting the value as a variable substitution because the dollar sign is not treated specially. The backslash (\) acts like single quotes for one character, leaving the character unchanged.

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

The eval Command


Bash performs variable substitutions as variables are encountered in a command. Before a command is executed, Bash searches the command for all dollar signs and inserts the value of variables before the command is carried out. Bash performs this substitution once. If a variable contains a value with a dollar sign in it and the value is substituted into a command, the value with the dollar sign remains unchanged. $ declare rx COMPANY=Value Book Resellers $ declare rx TITLE=$COMPANY $ printf %s\n $TITLE $COMPANY

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.

The test command checks a wide variety of conditions


and indicates whether the condition is true or not. Test can compare files, strings, or numbers. Dont confuse it with the Linux test command.

The let command computes an expression and


assigns the results to a variable in a single command.

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

Вам также может понравиться