1#!/bin/Bash2 ############################################3# @content Chapter8,9of Linux Command line and Shell Scripting Bible4 # @reader Gavin5# @date the/ A/ -6 ############################################7CHAPTER88 9+user varriable make the shell A-computer programTen 1when assigning a value to a user-defined variable, no spaces can appear One 2. Shell script automatically determine the data type A -+ The Backtick (') allows you-assign the output of a shell command to a vari- - Able the 1. The shell runs the command within the Backticks, and assigns the output to - The variable testing - (DO) -today= ' Date +%y%m%d ' + -+redirecting the input and output + 1. If the output file already exists, the redirectoperatorOverwrites the exi- ASting file with theNewFile Data at (DO): -ls >Test - 2. Instead of overwriting the file ' s contents, you may need to append output fr- - om a command to an existing file - (DO): -LS >>Test in 3. There ' s another method of input redirection, called inline input redirection. -This method allows your to specify the data forinput redirection on the command toLine instead ofinchA file. The inline input redirection symbol isTheDoubleles- +S-than symbol (<<). Besides Thissymbol,you must specify a text marker that -Delineates the beginning and end of the data used forinput. the (DO) *WC <<EOF $>HelloPanax Notoginseng>inline ->input the>redirection +>EOF A the EOF here can be replaced by any marker the ++Pipe - 1. The Linux system actually runs both commands at the same time, linking them $Together internallyinchThe system. $ 2. There ' s no limit to the number of pipes you can useincha command - -+Performing Math the 1the. Expr command can perform mathematical operations, but is more restrictive, such as to avoid wildcard matching - (DO)WuyiA=2 theb=5 - var= ' Expr $a \*$b ' WuEcho-n"A*b is:" -echo $var About 2. You can use $[] to perform mathematical operations $ (DO) -res=$[$a + $b-($a * $c)/($b + $c) +$d] - 3. The bash shell mathematical operators only support integer arithmetic. this - isA huge limitationifYou ' re trying to DoAny sort of real-world mathemat- Aical calculations. But there has been several solutions forOvercoming The +Bash integer limitation. The most popular solution uses the built-inchBash ca- the Lculator (called BC). -(TODO):p age - $ bash Calculater can be used here, but it is not available for the time being and will be made up later with the arrival the the+exiting the script the 1. Every command that runsinchThe shell uses an exit status to indicate to the theShell that it's done processing. Exit status exit, is a 0-255 of the shaping value. His - is passed to the shell when the executed command is completed) in 2.$?exit status after successful execution is 0 theLinux provides the $? Special variable that holds the exit status value from theThe last command, that executed. Must view or use the $? Variable immedi- Aboutately after the command you want to check. It changes values to the exit st- theATUs of the last command executed by the shell Code0Successful completion the of the command the (DO) + 1General Unknown Error - 2Misuse of shell command the 126The command can ' t executeBayi 127Command not found the -Invalid exit Argument the -+x Fatal error with Linux signal x - theCommand terminated with ctl-C - 255Exit status outof Range the the+Exit Command the 1. can change this toreturnyour own exit status code. The Exit command the allows you to specify a exit status when your script ends: - (DO) theres=$[$a +$b] theExit5 theWhen checking exit code for the above program $?=5, you can also use a variable as the value of exit94 the the ########################################### theCHAPTER998 About+if - if[]; Then101 Prog1102 Elif []; Then103 prog2104 Elif []; Then the Prog3106 fi107 108+The Test command109 1Directly judging the state of a command's speculationifcommand; the 2. You can useifTest command; Then You can also useif[command]; then; note111 the commands in the ${brackets must have spaces before and after them} the 3The . Test command can test Class 3 conditions113 (DO) the Numeric Comparisons the String Comparisons the File Comparisons117 118+Numeric Comparisons119N1-eq N2 CheckifN1 isequal to N2. -N1-ge N2 CheckifN1 isgreater than or equal to N2.121N1-GT N2 CheckifN1 isgreater than N2.122N1-le N2 CheckifN1 isLess than or equal to N2.123N1-lt N2 CheckifN1 isLess than N2.124N1-ne N2 CheckifN1 isNot equal to N2. the (DO)126 if[$a-GE $b]; Then127 Dosomething -elif [$a-LT $b]; Then129 DoSomethingElse the fi131 ${Numeric Comparisons cannot handle floating-point types} the 133
[Linux Command Line and Shell Scripting Bible] Basic Shell script