1. Variables and Replacements
#!/bin/bash# variable Substitution# In addition, there are many other syntax for variable substitution# For example, b=${a/23/bb} replaces 23 with BB and so on, and then uses the time to findA=375hello= $a# Note Spaces# If there are spaces in the variable, then the variable needs to be enclosed in double quotes.hello="A b c D"Echo $hello# output A b c DEcho ${hello}# output A b c DEcho"$hello" # output A b c D# -------------------------------------------------# variable Assignment# When the variable is "naked" there is no variable symbol, and when it is assigned,#+ instead of being quoted.A=123# Use let to assign valuesLet A=16+5# It is best to write the Let form in the following format, because such a format will show that the assignment statement is a wholeLet"A=16+5"Echo $a# results are displayed# If you don't use let, direct a=16+5, then echo $a is 16+5# variable assignment in a for loop forAinch7 8 9 10do Echo-n"$a"Doneechoecho# Special variable assignment, assigning the result of the command to the variableA= ' Ls-l ' echo $aecho"$a"# The same as the meaning abovea=$ (uname-m) echo $aexit 0
2. Test structure of the individual recommendation
#!/bin/bash # [] Single bracket if -ne 0]then "This is not root." Fi# [[]] double brackets if"integer" ]]then "test"fi# Note, use [[]] instead of [], Be able to block many logic errors in the script, for example, although an error will be given in [],#+ However, the &&,| |,<> operation can still work in a [[]]. # Arithmetic test using (()) ((0)) # return 1 ((1)) # return 0, this is true ((5 > 4)) # return 0, this is true # Compare here are a lot of key parameters, such as-e,-eq, etc., Time to confirm # and or non-bottom equivalent notation [[Condition1 && Condition2]] if "$exp 1" "$exp 2" ] Fiexit 0
3. An arithmetic expression for personal recommendation
#!/bin/bash "n++" # 101Echo $n ((n++) # 102Echo $nexit 0
4. Clear the files in the directory
#!/bin/bash# Clean up version 1# ---------------------------------------------------------------------------------Cd/tmp/logcat/dev/null > Testfileecho"Log clean up."# Clean up version 2# ---------------------------------------------------------------------------------LOG_DIR=/TMP/LOGCD $LOG _dircat/dev/null > Testfileecho"Log clean up."Exit#clean up version 3# ---------------------------------------------------------------------------------log_dir=/tmp/logroot_uid=0lines=10e_xcd=66e_notroot=67if["$UID" -ne "$ROOT _uid"]then Echo"must is root to run this script."Exit $E _notrootElseEcho"Yes, you are the god root."Fiif[-N"$"]then lines=$1Elselines= $LINESfi# e_wrongagrs=65# case "$" in# "") lines=50;;# *[!0-9]*) echo "Usage: ' basename $ ' file-to-cleanup"; Exit $E _wrongagrs;;# *) lines=$1;CD $LOG _dirif[' pwd '! ="$LOG _dir"]then:echo"Can ' t change to $LOG _dir."Exit $E _XCDFI# More efficient implementation of the above content.# Cd/tmp/log | | {# echo "Cannot change to necessary directory." >&2# exit $E _xcd;# }tail-$lines testfile > MESG.TEMPMV mesg.temp messageecho"Logs cleaned up."Exit 0
5. The logic of personal referrals and, or
#!/bin/bash # logic with if [Condition1] && [condition2]fiif [[Condition1 && Condition2]]FI
# logic or
if [Condition1] | | [Condition2]fi
if [condition1 | | condition2]]fi
6. Greatest common Divisor
#!/bin/bash # greatest common divisor args=2e_badargs=65< Span class= "REM" ># parameter check if [$#-ne "$ARGS"] then echo
"Usage: ' basename $ ' first-number second-number" exit $E _badargsfigcd () {dividend=$1 D Ivsor=$2 remainder=1 until [
"$remainder"
-eq 0] Do Let
"remainder= $dividend% $divsor" dividend= $divsor divsor= $remainder Done}
# in addition, about the function call
#+ defined a function, and then the bottom call, the argument is passed in the following way GCD $ $2echo; echo
"GCD of $ and $ = $dividend" ; Echoexit 0
Some of the better shellscript scripts