set | more # check the variables including shell-local variables(including shell functions) env | less # check the variables except shell-local variables(including shell functions)
Parameters
Here is a cheetsheet about parameters of shell.
Parameters
Comments
$0
script name
$<n>
the <n>th parameter
$#
nums of parameters
$@
All parameters, could be a list
$*
All parameters, but behave as a whole
$$
PID of the current progress
$?
return of last cmd
$!
PID of the last background cmd
Expression
Here is some expression cheatsheet.
Expression
Comments
Integer
-eq
equal to
-ne
not equal to
-gt
greater than
-lt
less than
-ge
greater than or equal to
-le
less than or equal to
String
-z “$str1”
str1 is empty or not
-n “$str1”
str1 is not empty or not
“$str1” = “$str2”
str1 equals to str2 or not
“$str1” != “$str2”
str1 doesn’t equals to str2 or not
“$str1” =~ “$str2”
str1 includes str2 or not
File
-f $filename
is file
-e $filename
does file exist
-d $filename
is directory
-s $filename
is not empty file
! -s $filename
is empty file
Logical
-o, ||, or
or
-a, &&, and
and
man bash
Syntax
Here is some usages of syntax and things.
Array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
IPS=(192.168.1.1 192.168.1.2 192.168.1.3) # all values: echo"${IPS[@]}"
# nums ${#IPS[@]}
# The first ${#IPS[0]} $IPS
# get subscript a=('a''b''c'): for i in${!a[@]}; doecho$i; done 0 1 2
\ # reverse ; # ls;cat a.txt ''# Does not parse variable, likes '$var' ""# Parsing the variable and replace with its value ` ` ,$() # execute command, but '$()' is better \n\r\t \$\" \\ () (()) ${}# for variable
# If 'exit 0' exists in func, this won't quit the current script. exit 0 # Nomal exit; Non-zero abnormal exit. exit# Return the return value of the previous command $? # Get the return value
return 0 # recomanded in function
test
1 2 3 4 5 6 7 8
# man test
# test equals to [] test -f file # equals to [ -e file]
[ 5 -gt 4 ]
Variable
Here is four ways to use variable better.
Prefer local variables in function
Make global variables readonly
Always refferred a variable with “${var}”
Env for ${ALL_CAPS}, local variables for ${lower_case}
for i in"$*"; do [ -d "/proc/$i" ] && return 0 # return is recommanded in function, exit 0 works well too done
return 1 }
export var unset var
# readonly for the static variable that could be modified readonly MY_PATH=/home/lee/bin
# If $1 is empty, take init_value as the initial value name=${1:-init_value}
Singnal and trap
1 2 3 4 5 6 7 8 9 10 11
# kill -l to list all signals while : do # capture signal 15 (default) and send 'echo signal 15' trap"echo signal 15" 15 sleep 1 # capture signal 2 and send 'echo signal C-C' trap"echo signal C-C" 2 sleep 1 echo $$ # print the current PID done
# Meta character: '.' - Single char except linebreak '*' - The char before it appears any times '[]' - Any char in [] '^' - Start of line '$' - End of line '\' - Reverse
# Extended Meta character: '+' - The expr(char) before it appeard more than one times '?' - The expr before it appeard 0 or 1 time '|' - or
`find -regex pattern`
sed -i "s/oldstring/newstring/g" `grep oldstring test/ -rl` grep '^lee'# start with lee grep 'bash$' /etc/passwd # end with bash grep '^root|bash$' passwd
touch /tm/{1..9}.txt
find *txt -execrm -v {} \;
# Cut with space and get the first string grep <pattern> <path> | cut -d " " -f 1
When the letter that follows is not space, line break, enter, = , (, it means substitution, likes the pointer of c language.
1 2 3 4 5 6 7 8 9 10 11
# like the pointer of c language, ${!C} = "Hello" B="Hello";C="B";value=${!C};echo result: $value # or B="Hello";C=B;value=${!C};echo result: $value
!n # the <n>th command of 'history' !-n # the <n>th command from the bottom of 'history' !! # equals to !-1, a alias, 'sudo !!, !!<missing char>' !$ # parameters of last command !str # the latest command that starts with str !?str # the latest command that includes str
Template
Pay attention to accumulation and form your own set of templates.
# Use = instead of == for String Comparisons value1="tecmint.com" value2="fossmint.com" if [ "$value1" = "$value2" ]
# Use Double Quotes to Reference Variables for name in"$names"; do echo"$name" done
eval# execute arguments as a shell command(multiple commands)
cp | mv test.sh{,.bk} cp | mv test.sh.bk{,}
echo {file1,file2}\ :{\ A," B",' C'} file1 : A file1 : B file1 : C file2 : A file2 : B file2 : C # Space needs to appear with \, ", '.
`set -e` # Add at the beginning, exit if any error occurs # Despite error, still continue to execute some commands <cmd> || true # or set +e ; <cmd>
# Add at the beginning, print the excute process # bash -x myscript.sh # If you only want debug output in a specific section of the script, put set -x before and set +x after the section. set -x # Display undefined variable set -u # don't hide errors within pipes, stop the whole pipes when anyone fails set -o pipefail # abort on nonzero exitstatus set -o errexit # abort on unbound variable set -o nounset
nohup foo | cat & # if foo must be started from a terminal and run in the background.
shellcheck
Always check for syntax errors by running the script with bash -n myscript.sh, or use shellcheck.
1 2 3
apt-get install shellcheck
shellcheck xxx.sh
whiptail
Display dialog boxes from shell scripts.
1 2 3 4 5 6 7 8 9
#!/bin/bash whiptail --yesno "would you like to continue?" 10 40 RESULT=$? if [ $RESULT = 0 ]; then echo"you clicked yes" else echo"you clicked no" fi