Article directory
1. Shell Basic Grammar
file name substitution
The characters used for matching are called wildcards (Wildcard), such as: * ? [ ]
the details are as follows:
*
: matches 0 or more of any character
?
: matches any character
[ ]
: matches one occurrence of any character in the square brackets
The prerequisite for successful replacement is that the file must exist. If the file does not exist, it cannot be replaced.
Parameter expansion:
touch {1, 2, 3, 4} / touch{1..4}.txt
command substitution
Execute a certain command and store the content of the standard output of this command in a variable.
varname=
cmd arg1 arg2 ...
varname=$(date)
#!/bin/bash
DateTime=`date`
echo "DateTime is " $DateTime
Get the path where the current script is located:
#获取当前脚本所在路径 ,在这个路径touch一个1.txt
curPath=$(cd `dirname $0`;pwd)
touch $curPath/1.txt
arithmetic substitution
Do the most basic integer arithmetic
var=45
var2=2
echo $[var+3]
echo $((var+3))
echo $((var*var2))
echo $(($var*$var2))
# 将10以八进制来解析,最后得到的是10进制的8,最后再加11等于19
echo $[8#10+11]
escape character
Similar to the C language, it is used as an escape character\
in the Shell to remove the special meaning of the single character immediately following it (except carriage return), in other words, the character immediately following it takes the literal value. For example:
quotation marks
apostrophe:
Unlike the C language, the single quotes and double quotes in Shell scripts are string delimiters, not character delimiters. Single quotes are used to keep the literal values of all characters within the quotes, even \ and carriage returns inside the quotes, but single quotes cannot appear in the string. If the quotation marks are not matched, press Enter, and the Shell will give a continuation prompt, asking the user to match the quotation marks. For example:
Double quotes:
Content enclosed in double quotes will be treated as a single string. It prevents wildcard expansion, but allows variable expansion. This is handled differently than single quotes.
Before we use variables, if the variable is passed as a parameter, we should habitually add double quotes to prevent spaces in the variable.
Two, Shell script syntax
condition test
How to express true and false in the shell? Directly use the return status of a certain command to judge whether it is true or false - main函数的返回值
. We know that in the main function, 0 means normal return, and non-zero means abnormal return. It is the opposite of C language.
We can $?
get the return status of the previous command through .
Commands for conditional testing: test 表达式
/[ 表达式 ]
(
EXPRESSION)
tests whether the expression is true!
EXPRESSION- EXPRESSION1
-a
EXPRESSION2 logical AND- EXPRESSION1
-o
EXPRESSION2 logical OR-n
STRING judges that the string is not an empty string-z
STRING judges that the string length is 0- STRING1
=
STRING2 Determines string equality- STRING1
!=
STRING2 judges that the strings are not equal- INTEGER1
-eq
INTEGER2 Judge integer equality- INTEGER1
-ge
INTEGER2 Judgment integer 1>= integer 2- INTEGER1
-gt
INTEGER2 judge integer 1> integer 2- INTEGER1 -
le
INTEGER2 judge integer 1<= integer 2- INTEGER1
-lt
INTEGER2 judge integer 1< integer 2- INTEGER1
-ne
INTEGER2 Judgment integer 1 != integer 2- FILE1
-nt
FILE2 Judging that file 1 is newer than file 2 (refers to the last modification time)- FILE1
-ot
FILE2 Judging that file 1 is older than file 2-b
FILE block device-c
FILE character device-d
FILE judges whether it is a directory-e
FILE simply judges whether the file exists-f
FILE judges that the file is an ordinary file-h
FILE / -L FILE determine if it is a symbolic link-k
FILE Determines whether the sticky bit of the file is set-p
FILE Determines whether the file is a named pipe-r
FILE Determines whether the file has read permission-s
FILE judges that the file exists and its size is greater than 0 bytes-S
FILE Determines whether the file is a socket file-t
FD judges that a file descriptor is opened by the terminal-w
FILE determines whether there is write permission-x
FILE has execute permission
branch structure
Similar to the C language, use if、then、elif、else、fi
these commands to implement branch control in the Shell.
#!/bin/bash
if [ -f /bin/bash ] # 判断bash文件是不是一个普通文件
then
echo "/bin/bash is a file"
else
echo "/bin/bash is NOT a file"
fi
" :
" is a special command called empty command, this command does not do anything, but Exit Status is always true.
#!/bin/bash
if false
then
:
else
echo "always false"
fi
#!/bin/bash
echo "Is it morning? Please answer yes or no!"
#读取用户输入的一个变量
read YES_OR_NO
if [ "$YES_OR_NO" = "yes" ]
then
echo "Good morning"
elif [ "$YES_OR_NO" = "no" ] ; then
echo "Good afternoon"
else
echo "Not recognized"
:
fi
In addition, Shell also provides &&和||
syntax, which is similar to C language and has Short-circuit features.
Branch structure:
case 表达式 in
val1|pattern1)
xxxxxx
;;
val2|pattern2)
xxxxxx
;;
*)
xxxx
;;
esac #将case倒着写
#!/bin/bash
echo "Is it morning? Please answer yes or no!"
#读取用户输入的一个变量
read YES_OR_NO
case "$YES_OR_NO" in
yes|y|Yes|YES)
echo "Good morning"
;;
[nN][oO])
echo "Good afternoon"
;;
*)
echo "Not recognized"
;;
esac
cycle
for
The traversal mode of the loop:
for varname in 列表 ; do #do可以单独写一行,如果写在for这一样就要分号
.....
echo $varname
done
控制循环次数
for i in {1..100}
do
...
done
遍历目录
for i in `ls`
do
....
done
for FRUIT in apple banana pear
do
echo "I like $FRUIT"
done
# 计算从1加到100的值,使用for循环
sum=0
for i in {
1..100}
do
sum=$[$sum+$i]
done
echo $sum
# 遍历当前目录,看当前目录中的普通文件和目录
for f in `ls`
do
if [ -f "$f" ]
then
echo "$f 是一个文件"
elif [ -d "$f" ]
then
echo "$f 是一个目录"
else
echo "Not recognized"
fi
done
while
loop traversal
while 命令|条件测试
do
xxxx
done
break 和 continue 跟C中的一样
echo "Please input paswd"
sum=1
read try
while [ "$try" != "secret" ]
do
if [ "$sum" -ge 5 ]
then
echo"Error 5 times, exit"
break
fi
echo "Sorry, try again!"
read try
sum=$[$sum+1]
done
3. Summary
Shell is the glue of command-line tools. No language can combine a large number of command-line tools as easily as Shell. In principle, Shell can do anything, but obviously it is most suitable for automation, because you only need to copy all the commands you typed manually into a file. Well, see you next time!