Bash Shell usage notes
The use of the shell is profound, I comb the basic knowledge, tidy up a simple usage, the specific requirements for self-study.
1. calculation # echo $ ((1+2)) 3# echo $ ((1+2+3)) 6# echo 3+2+5 |bc102. If usage if [ $i -eq 0 ]; then xxxelse xxxfi integers compare:-eq equals, such as:if [ "$a" -eq "$b" ] -ne not equal, such as: if [ "$a" -ne "$b" ] -gt greater than, such as:if [ "$a" -gt " $b " ] -ge greater than equals, such as:if [ " $a " -ge " $b " ] -lt Less than, such as:if [ "$a" -lt "$b" ] -le less than equals, such as:if [ "$a" -le "$b" ] < less (requires double brackets), such as: (("$a" < "$b") <= is less than or equal (requires double brackets), such as: (("$a" <= "$b") > is greater than (requires double brackets), such as: (("$a" > "$b") >= greater than or equal (requires double brackets), such as: (("$a" >= "$b")) stringComparison: This is usually done: if [ x "$test" = x "Test" ]= equals, such as:if [ "$a" = "$b" ] -d directory-e existence-f file-n non-empty string-z empty string 3. for usage array1= (" D.com " " e.com " " f.com ") len=${#array1 [@]}for ((i=0;i< $len; i++)) Do echo ${array1[$i]}donefor a in $ (seq 1 100);d o echo $a The usage of sleep 1sdone4. case case $1 in start|stop |reload) $1 ;; *) echo "usage: $0 [ Start|stop|reload] " ;; Usage of esac5. while while truedo echo "abc" read -p "Please enter: " abc ddd= "$abC " if [ ${#ddd} -ne 5 ]; then echo "Please re-enter!!!" " else break The fidone6. parameter ## $0 is the name of the bash file;# $? is the return value of the previous instruction;# $* All parameters that are called by the script;# [email protected] basically the same as the $* above. The difference is that:# $* returns a string separated by a space, and [email protected] Returns multiple strings;# $# is the number of all positional arguments; # #ab = ($ (LS)) echo "1: " ${ab[@]}echo "2: " ${#ab [@]} echo "3: " [email protected]echo "4: " $ #echo "5: " $?7. regular regular expression matching " =~ " [[ $XX =~ ^ $XXX ]] the =~ regular expression matching operator within a double brackets test expression.$ [[ "# Test2 " =~ ^# ]] && echo yes | | echo noyes8. Intercept string assumptions: f= "/a/b/c/d/e.name.ext" is:# basename $fe. name.ext# dirname $F &NBSP;&NBSP;&NBSP;&NBSP;/A/B/C/D Special usage: Use #,%,* in ${} to output the specified content 1) Remove the first/, and the left string # echo ${f#* /}A/B/C/D/E.NAME.EXT2) Remove the last 1/, and the left string # echo ${f##*/}e.name.ext3) and remove the last/, and the right string # echo ${ F%/*}&NBSP;/A/B/C/D4) Remove the first/, and the string on the right # echo ${f%%/*} (empty) above, is based on "/" To do the segmentation, you can also use "." To separate, may wish to try. # echo ${f#*.} name.ext# echo ${f##*.} ext# echo ${f%.*} /a/b/c/d/e.name# echo ${f%%.*}/a/b/c/d/e9. script into the background, output to log sh test.sh >1.log 2>&1 &sh test.sh >/dev/null 2>&1 & Here you need to understand the role of several small things:/dev/null understand the empty device, which is a special file, where the function is to discard the contents of the output 2>&1 0 input 1 output 2 error Here is 2 Redirect to 1& put test.sh into the background to do, think, and what other ways can you put the program in the background? 10. pipe through the "|" Import the output into another program's input for processing, such as:echo ' abc, def ' |cut -d ', ' -f 111. Command Trace debug sh -x test.sh12. shortcut ctrl + a switch to command line start Ctrl + b - move back a charCtrl + c terminate command ctrl + d exit shell,logoutctrl + e switch to the end of the command line ctrl + l clear the contents of the screen ctrl + k cut after the clear cursor ctrl + q Restore Brush screen ctrl + r in the history command find ctrl + s can be used to stay in the current ctrl + u before clearing the contents of the clipping cursor CTRL + y paste the characters just removed ctrl + z into the background to run!! Repeat the last command ↑ (ctrl+p) Show previous command ↓ (CTRL + N) Show next command!$ displays the last parameter of the system 13. The shell passes Chinese to Python with exception export lang= "en_US. UTF-8 ";/usr/local/bin/python3 xxx.py
Bash Shell usage notes