`
xiaoer_1982
  • 浏览: 1820728 次
  • 性别: Icon_minigender_2
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

Unix® Shell Programming读书笔记2

阅读更多

21.Exit Status
The shell variable $? is automatically set by the shell to the exit status of the last command executed. 0---succeeded.1--failed

22.If the user is logged on, we'll print a message to that effect; otherwise we'll say nothing.
if who | grep "^$user " > /dev/null

then

echo "$user is logged on"

fi
the if command executes the pipeline----who | grep "$user".And tests the exit status returned by grep. If the exit status is zero, grep found user in who's output. In that case, the echo command that follows is executed. If the exit status is nonzero, the specified user is not logged on, and the echo command is skipped.

23.test
It's good programming practice to enclose shell variables that are arguments to test inside a pair of double quotes (to allow variable substitution). This ensures that test sees the argument in the case where its value is null.
In the "test",() must has a \ before it.

24.exit
exit n
where n is the exit status that you want returned. If none is specified, the exit status used is that of the last command executed before the exit.Be advised that executing the exit command directly from your terminal will log you off the system because it will have the effect of terminating execution of your login shell.


25.case
? can be used to specify any single character; * can be used to specify zero or more occurrences of any character; and [...] can be used to specify any single character enclosed between the bracket.
| has the effect of a logical OR when used between two patterns.


26.The -x Option for Debugging Shell Programs
You can trace the execution of any program by typing sh -x followed by the name of the program and its arguments. This starts up a new shell to execute the indicated program with the -x option enabled. In this mode, commands are printed at the terminal as they are executed, preceded by a plus sign.

27.The Null Command :
The format of this command is simply ----- :

28.&& and || and if-else
If condition A is succeeded,run the B, the if-else can be replaced with &&,otherwise with ||

29.for
f the file filelist contains a list of the files that you want to run through run, you can type
files=$(cat filelist)
for file in $files

do

run $file

done

go through all the arguments passed on
for arg in "$@"

do

echo $arg

done
It's equal to the following structure:
for arg

do

echo $arg

done

30.while and until
The while loop is often used in conjunction with the shift command to process a variable number of arguments typed on the command line.
while [ "$#" -ne 0 ]

do

echo "$1"

shift

done
The while command continues execution as long as the command listed after the while returns a zero exit status. The until command is similar to the while, only it continues execution as long as the command that follows the until returns a nonzero exit status.The until command is useful for writing programs that wait for a particular event to occur.
To just exit from the loop (and not from the program), you can use the break command.
If the break command is used in the form: break n ,the n innermost loops are immediately exited.
The continue command is similar to break, only it doesn't cause the loop to be exited, merely the remaining commands in the loop to be skipped. Execution of the loop then continues as normal. Like the break, an optional number can follow the continue, so continue n causes the commands in the innermost n loops to be skipped; but execution of the loops then continues as normal.
An entire loop can be sent to the background for execution simply by placing an ampersand after the done: done & .
I/O Redirection on a Loop: done > loopout:eg.echo's output is redirected to the terminal while the rest goes to the file output.
for file

do

echo "Processing file $file" > /dev/tty

...

done > output
A command's output can be piped into a loop, and the entire output from a loop can be piped into another command in the expected manner:done | wc -l




31.Death Loop
The Unix command true serves no purpose but to return an exit status of zero. The command false also does nothing but return a nonzero exit status. If you write

while true

do

...

done <=>

while :
do

...

done <=>
until false

do

...

done

32.getopts
The getopts command is designed to be executed inside a loop. Each time through the loop, getopts examines the next command line argument and determines whether it is a valid option.

#

# Wait until a specified user logs on -- version 3

#

# Set up default values

mailopt=FALSE

interval=60

# process command options
# If getopts doesn't find an argument after an option that requires one, it stores a question mark inside the specified # variable and writes an error message to standard error. Otherwise, it stores the actual argument inside a special # variable called OPTARG.

while getopts mt: option

do

case "$option"

in

m) mailopt=TRUE;;

t) interval=$OPTARG;;

\?) echo "Usage: mon [-m] [-t n] user"

echo " -m means to be informed by mail"

echo " -t means check every n secs."

exit 1;;

esac

done

# Make sure a user name was specified
# Another special variable called OPTIND is used by the command. This variable is initially set to one and is updated each # time getopts returns to reflect the number of the next command-line argument to be processed.

if [ "$OPTIND" -gt "$#" ]

then

echo "Missing user name!"

exit 2

fi

shiftcount=$((OPTIND - 1))

shift $shiftcount

user=$1

#

# Check for user logging on

#

until who | grep "^$user " > /dev/null

do

sleep $interval

done

#

# When we reach this point, the user has logged on

#

if [ "$mailopt" = FALSE]

then

echo "$user has logged on"

else

runner=$(who am i | cut -c1-8)

echo "$user has logged on" | mail $runner

fi
explanation:
When the line mon -m -t 600 ann & is executed, the following occurs inside the while loop in mon: getopts is executed, and it stores the character m inside the variable option, sets OPTIND to two, and returns a zero exit status. The case command is then executed to determine what was stored inside option. A match on the character m indicates that the "send mail" option was selected, so mailopt is set to TRUE. (Note that the ? inside the case is quoted. This is to remove its special meaning as a pattern-matching character from the shell.)
The second time getopts is executed, getopts stores the character t inside option, stores the next command-line argument (600) inside OPTARG, sets OPTIND to three, and returns a zero exit status. The case command then matches the character t stored inside option. The code associated with that case copies the value of 600 that was stored in OPTARG into the variable interval.
The third time getopts is executed, getopts returns a nonzero exit status, indicating the end of options. The program then checks the value of OPTIND against $# to make sure that the username was typed on the command line. If OPTIND is greater than $#, then no more arguments remain on the command line and the user forgot the username argument. Otherwise, the shift command is executed to move the username argument into $1. The actual number of places to shift is one less than the value of OPTIND.


33.Special echo Escape Characters
The echo command always automatically displays a terminating newline character after the last argument.
This can be suppressed if the last two characters given to echo are the special escape characters \c. This tells echo to leave the cursor right where it is after displaying the last argument and not to go to the next line.

34.Temporary files

When writing shell programs to be run by more than one person, make your temporary files unique. One way is to create the temporary file in the user's home directory, for example. Another way is to choose a temporary filename that will be unique for that particular process. To do this, you can use the special $$ shell variable, which contains the process id number (PID) of the current process.

35.The Exit Status from read
read always returns an exit status of zero unless an end of file condition is detected on the input.

lineno=1

cat $* |

while read -r line

do

echo "$lineno: $line"

lineno=$((lineno + 1))

done

The variable lineno--the line number count--is initially set to 1. Then the arguments typed to number are given to cat to be collectively written to standard output. If no arguments are supplied, $* will be null, and cat will be passed no arguments. This will cause it to read from standard input.The output from cat is piped into the while loop. For each line read by read, the line is echoed at the terminal, preceded by the value of lineno, whose value is then incremented by one.
Use the -r option to read to prevent it from interpreting the special character----backslash.

36.printf
printf "format" arg1 arg2 ...
format is : %[flags][width][.precision]type flags: -, justifies the value being printed(- means left aligned,right aligned default)
+, precede integers with a + or - sign
#, precede octal integers with 0 and hexadecimal integers with 0x or 0X for %#x or %#X,

Space character, precede positive integers with a space and negative integers with a - width: a positive number that specifies the minimum field width for printing an argument. The argument is right justified within this field unless the - flag is used.
.precision:a positive number that specifies a minimum number of digits to be displayed for %d, %u, %o, %x, and %X. This results in zero padding on the left of the value.
If a * is used in place of a number for width or precision, the argument preceding the value to be printed must be a number and will be used as the width or precision, respectively. If a * is used in place of both, two integer arguments must precede the value being printed and are used for the width and precision:

$ printf "%*s%*.*s\n" 12 "test one" 10 2 "test two"

test one te
12 is used as the width for the first string, 10 as the width for the second string, and 2 as the precision for the second string.

37. Subshell
There is a way to make the value of a variable known to a subshell, and that's by exporting it with the export command. The format of this command is simply:export variables.
1)Any variable that is not exported is a local variable whose existence will not be known to subshells.
2)Exported variables and their values are copied into a subshell's environment, where they may be accessed and changed. However, such changes have no effect on the variables in the parent shell.There is no way to change the value of a variable in a parent shell from within a subshell.
3)Exported variables retain this characteristic not only for directly spawned subshells, but also for subshells spawned by those subshells (and so on down the line).
4)A variable can be exported any time before or after it is assigned a value.

38.PS1 and PS2
PS1:The characters that the shell displays as your command prompt are stored in it.
PS2:The characters that used in the concatenation of two lines

39.$PATH
To have the current directory searched first, you put the period at the start of the path:.:/bin:/usr/bin
(can cause so-called Trojan horse problem)
Using a environment variable can be multi-used by many users.

40. There is no way to change the current directory of a parent shell from a subshell.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics