shell script Learning Guide 2017-03-26 Turn on tracing
set -x
Turn off tracing
set +x
This abbreviation is also too bullish
i18n: internationalization l10n: localization
Regular expression (absolutely something you don't need to forget) Backward Reference awk (again)
sed, stream editor
sed -n '/Character string/p' file.txt sed -i '/Character string/d' file.txt
grep,cut,sort,uniq
grep -q: Quiet, not output to standard output -s: no error message is displayed
export: modify or print environment variables readonly: keep variables unmodified
unset: delete variable assignment
Expansion Operator
${varname:-word}:varname exists and is not null, returns its value, otherwise returns word ${varname:=world}:varname exists and returns its value if it is not null, otherwise it is set to word ${varname:?message}:varname exists and is not null, returns its value, otherwise displays varname:message and exits the command to catch errors caused by undefined variables ${varname:+word}:varname exists and returns word if it is not null, otherwise returns NULL to test the existence of a variable
pattern matching
${variable#Pattern}: at the beginning of the pattern matching variable value, the shortest part of the match is deleted and the rest is returned ${variable##Pattern}: at the beginning of the pattern matching variable value, delete the longest part of the match and return the rest
eg: [root@localhost shell]# ip0="/etc/sysconfig/network-scripts/ifcfg-eth0" [root@localhost shell]# echo $ip0 /etc/sysconfig/network-scripts/ifcfg-eth0 [root@localhost shell]# echo ${ip0#/*/} sysconfig/network-scripts/ifcfg-eth0 [root@localhost shell]# echo ${ip0##/*/} ifcfg-eth0 ${variable%pattern}: If the pattern matches at the end of the variable value, the shortest part of the match is deleted and the rest is returned ${variable%%pattern}: If the pattern matches at the end of the variable value, the longest part of the match is deleted and the rest is returned eg: [root@localhost shell]# echo ${ip0%%/*} [root@localhost shell]# echo ${ip0%/*} /etc/sysconfig/network-scripts
Location parameters $, $@: All command line parameters $: Treat all command line parameters as separate strings $@: Treat all command line parameters as separate individuals, separate strings shift command to truncate position parameters from list
eg: [root@localhost shell]# vim 1.sh #!/bin/bash # echo "$*" shift #Equivalent to shift 1 echo "$*" shift echo "$*" shift echo "$*" shift echo "$*" [root@localhost shell]# bash 1.sh 1 2 3 4 5 6 1 2 3 4 5 6 2 3 4 5 6 3 4 5 6 4 5 6 5 6
Special variables
#: Number of parameters @: Command Line Parameters *: Command Line Parameters -: Given in Reference shell Options for ?: Exit status $: shell Process Number 0: shell Program Name !: The process number of the last background command, which stores the process number by wait Command for later use ENV: HOME: IFS: Internal Field Separator LANG: current locale Default name of, other LC_*Variables will override LC_ALL: current locale The name of the LANG And others LC_*variable LC_COLLATE: Current used to sort characters locale Name LC_CTYPE: Current used to determine character categories during pattern matching locale Name of LC_MESSAGES: Name of the current language for output information LINEND: Line number of a line just passed in a script or function NLSPATH: stay $LC_MESSAGES(XSI)Location of information directory in given information language PATH: PPID: Process number of parent process PS1: Command prompt string, default is $ PS2: Prompt string for line continuation, default is> PS4: set -x Set the prompt string to execute the trace, default is+ PWD:
Exit status code (exit num (can be specified manually)
0: Exit successfully > 0: Failed during redirection or word expansion 1-125: Command unsuccessfully exited 126: Command found, file could not be executed 127: Command not found > 128: Command dies from receiving a signal
if statement
if then elif then else
test command
test "$?" -eq 0 Be equal to [ "$?" -eq 0 ]
case statement
case $1 in ) ;; ) ;; ) ;; *) ;; esac
for statement
for in do done
the while statement
while do done
until statement
until do done
break statement Jump out of the current loop, break
continue Statement The continue command is used to start the next repeating loop earlier
function
function name () { }
expr expr 10 + 20 30
Progress (not carefully watched) Capture, interrupt...