1read keyboard command to read the values of variables
Usually variable values read from a keyboard in a shell script and user interaction where
the value of the order of several variables can be read once, the value of the input variables need separated by spaces
[root@meditation ~]# read a b
hello world
[root@meditation ~]# echo $a
hello
[root@meditation ~]# echo $b
world
[root@meditation ~]# read -p "input your passwd:" -s -t 20 passwd
- -p: display a message
- -s: Hide Password
- -t: set the time, off 20 seconds
- -n: length limit 12-n:. maximum of 12 characters
- -r: allows you to enter special characters
You can also mix and echo, to achieve the effect of a password
[root@meditation ~]# echo -n "input your passwd:";read passw
input your passwd:123456
Simple interactive input information
[root@meditation ~]# cat read.sh
#!/bin/bash
read -p "请输入姓名:" name
read -p "请输入年龄:" age
read -p "请输入性别:" sex
cat<<eof
******************
你的基本信息如下:
姓名: $name
年龄: $age
性别: $sex
******************
eof
[root@meditation ~]# bash ./read.sh
请输入姓名:苍老师
请输入年龄:34
请输入性别:女
******************
你的基本信息如下:
姓名: 苍老师
年龄: 34
性别: 女
******************
[root@meditation ~]#
2 flow control statements if
2.1 single-branch if statement
[root@meditation ~]# cat if1.sh
#!/bin/bash
if ls /mnt
then
echo "it is ok"
fi
[root@meditation ~]# bash if1.sh
it is ok
2.2 pairs of branch if statement
[root@meditation ~]# cat if2.sh
#!/bin/bash
if grep sss /etc/passwd
then
echo "it's ok"
else
echo "it's err"
fi
[root@meditation ~]# bash if2.sh
it's err
More than 2.3 branch if statement
Determining whether the user is present in the system, whether the home directory
[root@meditation ~]# cat if3.sh
#!/bin/bash
read -p "input a user:" users
if grep $users /etc/passwd
then
echo "the user $users exists on this system"
elif ls -d /home/$users
then
echo "$users has a home direcotry"
echo "the user $users not exists on this system"
else
echo "$users nor has a home direcotry"
echo "the user $users not exists on this system"
fi
[root@meditation ~]# bash if3.sh
input a user:sunlizhao31
sunlizhao31:x:1003:1003::/home/sunlizhao31:/bin/bash
the user sunlizhao31 exists on this system
3test test command
The test shell command is used to check if a condition is established, it can be numeric, character, file test three aspects
- If the result is right, also called the result is true, with $? = 0 indicates the contrary is false, non-0 means
3.1 numerical comparison
parameter | Explanation | Examples |
---|---|---|
-eq | True equals | "$a" -eq "$b" |
-born | It is not equal to True | "$a" -ne "$b" |
-gt | True greater than | "$a" -gt "$b" |
-give | True or greater | "$a" -ge "$b" |
-lt | Less than True | "$a" -lt "$b" |
-the | True or less | "$a" -le "$b" |
Both versions: test and []
[root@meditation ~]# cat test1.sh
#!/bin/bash
if test 2 -eq 1
then
echo "ok"
else
echo "err"
fi
[root@meditation ~]# bash test1.sh
err
[root@meditation ~]# cat test2.sh
#!/bin/bash
if [ 2 -le 10 ]
then
echo "ok"
else
echo "err"
fi
[root@meditation ~]# bash test2.sh
ok
The user inputs two numbers, to determine the size
Only use integer
[root@meditation ~]# cat test3.sh
#!/bin/bash
read -p "input var1: " var1
read -p "input var2: " var2
if [ $var1 -gt $var2 ]
then
echo "$var1 > $var2"
elif [ $var1 -lt $var2 ]
then
echo "$var1 < $var2"
else
echo "$var1 = $var2"
fi
[root@meditation ~]# bash test3.sh
input var1: 12
input var2: 21
12 < 21
[root@meditation ~]# bash test3.sh
input var1: 12
input var2: 12
12 = 12
3.2 String comparison
symbol | description |
---|---|
== | Equal to true |
!= | Not equal true |
-z string | 0 string length True |
-n string | The length of the string is not empty True |
str1>str2 | It is larger than the true str1 str2 |
str1<str2 | less than true str1 str2 |
When comparing strings
- The greater than and less than the number must escape, or else it will shell redirection symbols as
- Above and below, and their order is not the same sort test sort test, a sequence of an ascii, less than uppercase lowercase
File Compare 3.3
symbol | description |
---|---|
-e filename | If the file or directory exists True |
-r filename | If the file exists and is readable True |
-w file name | If the file exists and is writable True |
-x file name | If the file exists and is executable True |
-s filename | If the file exists and there is at least one character True |
-d filename | True if file exists and is a directory was |
-f filename | True if file exists and is a regular file |
-c filename | If the file exists and the file was true for the character |
-b filename | If the file exists and is a block special file True |
file1 file2 -nt | Check if file1 is newer than file2 |
file1 file2 -ot | Check if file1 is newer than file2 |
[root@localhost test]# cat test.sh
#!/bin/bash
if [ -e /etc/passwd ]
then
echo ok
else
echo err
fi
[root@localhost test]# bash test.sh
ok
[root@localhost test]# test -e /etc/passwd && echo ok || echo err
ok
4 complicated flow control process conditions and wildcards
4.1 Complex Conditions
Both conditions are true, or is true on the implementation of
Determining a first
if [条件判断一] &&(||) [条件判断二];then
命令一
elif [条件判断三] &&(||) [条件判断四];then
命令二
else
其他命令
fi
The second judge
if [[ 条件判断一 &&(||) 条件判断二 ]];then
命令一
elif [[ 条件判断三 &&(||) 条件判断四 ]];then
命令二
else
其他命令
fi
[] And the difference [[]] of
[[]] Is [] operator expansion; support * <> characters, do not need to escape
[root@localhost ~]# if [[ $USER == r* ]] ; then echo "hello,$USER" ; else echo $USER not; fi
hello,root
[root@localhost ~]# if [ $USER == r* ] ; then echo "hello,$USER" ; else echo $USER not; fi
root not
[] As a string will r *
- All characters and logical operators need to use "space" to separate, not connected together
- In [] expression, commonly> <need an escape character, for size comparison
- Logical operators &&, || comparison, if the [] symbols, with the outside, such as [] && [] || []
If the comparison logic, or with the in [], then use -a, -o be expressed as [[x -ay -oz]] [[]] Is [] operator expansion; support * <> characters, do not need to escape, which support the logical operators ||, is no longer used && -a, -o
- [[]] Can be extended arithmetic, and [] can not.
[[]] It can be used for advanced string processing, such as fuzzy matching
4.2 Wildcard
symbol | description |
---|---|
* | Matches zero or more characters a * b, a character can have any arbitrary length between a language B, may be not one, as ab, axybdb |
? | Matches any character. A? B, between a and b must be only one character can be any character as axb, abb |
[list] | Match any single character list, only a match between a [xyz] b, a and b are a character, but only x, y or z are as axb |
[!list] | At the match any single character in the list. A [! 0-9] ab between a character must only have, but are not Arabic numerals. As matched axb |
[a-z] | A single character to match any of z. [String1, string2, ...] string1 matches string2 or one or more of the string |
[root@meditation ~]# ls /etc/*.conf
/etc/asound.conf /etc/libaudit.conf /etc/pam_url.conf /etc/tcsd.conf
...
[root@meditation ~]# ls /etc/???.conf
/etc/ntp.conf /etc/sos.conf /etc/yum.conf
[root@meditation ~]# touch /opt/a{1,2,3}.txt
[root@meditation ~]# ls /opt/a[123].txt
/opt/a1.txt /opt/a2.txt /opt/a3.txt
[root@meditation ~]# ls /opt/a[1,2,3].txt
/opt/a1.txt /opt/a2.txt /opt/a3.txt
5 combat: three shell script combat
5.1 Inspection Service is running
[root@meditation ~]# cat status3.sh
#!/bin/bash
if [ $# -ge 1 ] ; then
systemctl status $1 > /dev/null
if [ $? -eq 0 ] ; then
echo "服务正在运行"
else
systemctl start $1
echo "正在打开"
fi
else
echo "格式错误"
fi
[root@meditation ~]# chmod +x status3.sh
[root@meditation ~]# ./status3.sh httpd
服务正在运行
5.2 According to the results interpretation grade
[root@meditation ~]# cat cjcx.sh
#!/bin/bash
read -p "请您输入成绩: " cj
if [ $cj -ge 0 ] && [ $cj -lt 60 ] ; then
echo "补考!"
elif [ $cj -ge 60 ] && [ $cj -lt 80 ] ; then
echo "良好"
elif [ $cj -ge 80 ] && [ $cj -lt 90 ] ; then
echo "优秀"
elif [ $cj -ge 90 ] && [ $cj -le 100 ] ; then
echo "你太棒了"
else
echo "请输入0-100之内的有效成绩"
fi
[root@meditation ~]# bash cjcx.sh
请您输入成绩: 1
补考!
[root@meditation ~]# bash cjcx.sh
请您输入成绩: 70
良好
[root@meditation ~]# bash cjcx.sh
请您输入成绩: 100
你太棒了
5.3 automatic backup a directory
[root@meditation ~]# cat etcbak.sh
#!/bin/bash
baknamefile=`date +%F`
bakdir=/etcbak
srcdir=/etc
[ -e $bakdir ] || mkdir $bakdir
tar -zcvf ${bakdir}/${baknamefile}-etc.tar.gz $srcdir
echo "==================="
ls -lh ${bakdir}/${baknamefile}-etc.tar.gz
[root@meditation ~]# bash etcbak.sh
...
/etc/cloud/templates/resolv.conf.tmpl
/etc/sysctl.conf
/etc/gshadow-
===================
-rw-r--r-- 1 root root 9.5M Aug 4 20:43 /etcbak/2019-08-04-etc.tar.gz