脚本的条件语句
条件测试语句的格式
格式 | |
---|---|
test <条件> | |
[ <条件> ] | 一般情况下通用 |
[[ <条件> ]] | []升级版,支持正则表达式 |
(( <条件> )) |
不熟悉的话直接man test查看
文件测试语句
man test
大概有以下这些针对于文件的操作
FILE1 -ef FILE2
FILE1 and FILE2 have the same device and inode numbers
FILE1 -nt FILE2
FILE1 is newer (modification date) than FILE2
FILE1 -ot FILE2
FILE1 is older than FILE2
-b FILE
FILE exists and is block special
-c FILE
FILE exists and is character special
-d FILE
FILE exists and is a directory
-e FILE
FILE exists
-f FILE
FILE exists and is a regular file
-g FILE
FILE exists and is set-group-ID
-G FILE
FILE exists and is owned by the effective group ID
-h FILE
FILE exists and is a symbolic link (same as -L)
-k FILE
FILE exists and has its sticky bit set
-L FILE
FILE exists and is a symbolic link (same as -h)
-O FILE
FILE exists and is owned by the effective user ID
-p FILE
FILE exists and is a named pipe
-r FILE
FILE exists and read permission is granted
-s FILE
FILE exists and has a size greater than zero
-S FILE
FILE exists and is a socket
-t FD file descriptor FD is opened on a terminal
-u FILE
FILE exists and its set-user-ID bit is set
-w FILE
FILE exists and write permission is granted
-x FILE
FILE exists and execute (or search) permission is granted
# 测试文件权限
wxl@ubuntu:~/workspace/shellScripts$ [[ -w test222.sh ]] && echo ok || echo error
error
wxl@ubuntu:~/workspace/shellScripts$ chmod 444 test222.sh
wxl@ubuntu:~/workspace/shellScripts$ [[ -w test222.sh ]] && echo ok || echo error
error
wxl@ubuntu:~/workspace/shellScripts$ ls -al test222.sh
-r--r--r-- 1 wxl wxl 0 Apr 4 05:30 test222.sh
wxl@ubuntu:~/workspace/shellScripts$ [[ -r test222.sh ]] && echo ok || echo error
ok
常用格式
# 满足条件执行某命令
[ 条件 ] && 命令
# 满足条件执行多个命令
[ 条件 ] && {
cmd1
cmd2
...
}
# 条件不满足则执行另一个命令
[ 条件 ] || cmd
[ 条件 ] || {
cmd1
cmd2
...
}
案例
计算机的进阶版
#! /bin/bash
[ $# -eq 2 ] || {
echo "Usage: Please input 2 numbers!!!"
exit 1
}
x=$1
y=$2
# 判断当前的连个数字是否是数字
expr "$x" + "$y" + 666 &>/dev/null
[ $? -eq 0 ] || {
echo "Usage: $0 num1 num2"
exit 2
}
echo "result: "
awk -va=$x -vb=$y 'BEGIN{print a+b,a-b,a*b,a/b}'
wxl@ubuntu:~/workspace/shellScripts$ sh cal.sh 10 10
result:
20 0 100 1
wxl@ubuntu:~/workspace/shellScripts$ sh cal.sh
Usage: Please input 2 numbers!!!
wxl@ubuntu:~/workspace/shellScripts$ sh cal.sh 10 a
Usage: cal.sh num1 num2
字符串操作 需要加上双引号
表达式 | 含义 |
---|---|
-n | 判断字符串不为空,返回true |
-z | 判断字符串为空,返回true |
“str1” = “str2” | 两个字符串相等,返回true |
“str1” != “str2” | 两个字符串不相等,返回true |
代码示例:
# oldboy为空
wxl@ubuntu:~/workspace/shellScripts$ [ -n "$oldboy" ] && echo ok || echo fail
fail
# oldboy赋值
wxl@ubuntu:~/workspace/shellScripts$ oldboy=aaa
wxl@ubuntu:~/workspace/shellScripts$ [ -n "$oldboy" ] && echo ok || echo fail
ok
wxl@ubuntu:~/workspace/shellScripts$ [ -z "$oldboy" ] && echo ok || echo fail
fail
wxl@ubuntu:~/workspace/shellScripts$ [ -z "$oldboy11" ] && echo ok || echo fail
ok
# 判断字符串是否相等
wxl@ubuntu:~/workspace/shellScripts$ name=zzz
wxl@ubuntu:~/workspace/shellScripts$
wxl@ubuntu:~/workspace/shellScripts$ [ "$name" = "zzz" ] && echo ok || echo fail
ok
wxl@ubuntu:~/workspace/shellScripts$ [ "$name" = "zz" ] && echo ok || echo fail
fail
wxl@ubuntu:~/workspace/shellScripts$ echo $nae
wxl@ubuntu:~/workspace/shellScripts$ echo $name
zzz
wxl@ubuntu:~/workspace/shellScripts$ [ "$name" != "zz" ] && echo ok || echo fail
ok
注意
- 执行脚本的时候后面加上-x参数表示显示脚本的执行过程,在脚本出现错误的时候可以使用来排错
- 在shell编程中取变量的值有两种方法或者或者或者{}
- ${#aa} 统计变量aa中字符的个数 $# 统计脚本参数的个数
- $$ 脚本的pid
- | 表示管道
- $! 上一个脚本的pid
- #!/bin/bash 指定命令解释器
综合示例
- 去掉文件中的注释和空行
# 使用三种方法解决
1. egrep
wxl@ubuntu:~/workspace/shellScripts$ egrep -v '#|^$' sshd_config
ChallengeResponseAuthentication no
UsePAM yes
X11Forwarding yes
PrintMotd no
AcceptEnv LANG LC_*
Subsystem sftp /usr/lib/openssh/sftp-server
# 其中,^$对应的是正则表达式,^表示行的开头,比如^name 表示的就是已name为开头的行;$表示结尾,name$表示以name结尾的行;| 表示或
# 找出文件中大小写字母开头的行
wxl@ubuntu:~/workspace/shellScripts$ egrep '^[a-Z]' sshd_config
ChallengeResponseAuthentication no
UsePAM yes
X11Forwarding yes
PrintMotd no
AcceptEnv LANG LC_*
Subsystem sftp /usr/lib/openssh/sftp-server
2. sed
wxl@ubuntu:~/workspace/shellScripts$ sed -r '/#|^$/d' sshd_config
ChallengeResponseAuthentication no
UsePAM yes
X11Forwarding yes
PrintMotd no
AcceptEnv LANG LC_*
Subsystem sftp /usr/lib/openssh/sftp-server
# -r 表示支持扩展正则,|可以表示或
# //d 表示删除
3. awk
wxl@ubuntu:~/workspace/shellScripts$ awk '!/#|^$/' sshd_config
ChallengeResponseAuthentication no
UsePAM yes
X11Forwarding yes
PrintMotd no
AcceptEnv LANG LC_*
Subsystem sftp /usr/lib/openssh/sftp-server
- 获取“aaa:444:444:/home/user”中的home路径
wxl@ubuntu:~/workspace/shellScripts$ variable="aaa:444:444:/home/user"
wxl@ubuntu:~/workspace/shellScripts$ echo ${variable##*:} 使用##删除:之前的字符串
/home/user
wxl@ubuntu:~/workspace/shellScripts$ echo ${variable/*:/} 使用//替换:之前的字符串
/home/user
- 把文件后缀html批量修改为bak
wxl@ubuntu:~/workspace/shellScripts$ for name in `ls *.html`;do echo $name ${name/.html/.bak} ;done
1.html 1.bak
2.html 2.bak
3.html 3.bak
index.html index.bak
- 常见的命令解释器有哪些?
1. bash redhat、centos、ubuntu新版
2. dash ubuntu老版
2. csh tcsh unix系统
- 命令获取主机的hostname,ip,release版本,内核版本
wxl@ubuntu:~/workspace/shellScripts$ hostname
ubuntu
wxl@ubuntu:~/workspace/shellScripts$ hostname -I|awk '{print $1}'
192.168.1.110
wxl@ubuntu:~/workspace/shellScripts$ cat /etc/os-release
NAME="Ubuntu"
VERSION="18.04.6 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.6 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic
wxl@ubuntu:~/workspace/shellScripts$ uname -r
5.4.0-84-generic
- 命令获取当前的日期,时间,工作目录,用户名
wxl@ubuntu:~/workspace/shellScripts$ date +%F
2022-04-12
wxl@ubuntu:~/workspace/shellScripts$ date +%T
07:37:48
wxl@ubuntu:~/workspace/shellScripts$ whoami
wxl
wxl@ubuntu:~/workspace/shellScripts$ pwd
/home/wxl/workspace/shellScripts
- 将文件中的小写字母转换为大写
wxl@ubuntu:~/workspace/shellScripts$ tr 'a-z' 'A-Z' <test18.cpp
ABCD
DFGH
IJKL
MNOP
QRST
wxl@ubuntu:~/workspace/shellScripts$ cat test18.cpp
abcd
dfgh
ijkl
mnop
qrst
wxl@ubuntu:~/workspace/shellScripts$ awk '{print toupper($0)}' test18.cpp
ABCD
DFGH
IJKL
MNOP
QRST
wxl@ubuntu:~/workspace/shellScripts$ awk '{print tolower($0)}' test18.cpp
abcd
dfgh
ijkl
mnop
qrst
- 检查用户是否为root
wxl@ubuntu:~/workspace/shellScripts$ cat checkName.sh
#!/bin/bash
[ $UID -eq 0 ]||{
echo "need to be root"
exit 1
}
[ $UID -eq 0 ]&&{
echo "Now is root"
}
name=`whoami`
[ "$name" = "root" ]||{
echo "need to be root"
exit 1
}
[ "$name" = "root" ]&&{
echo "Now is root"
}
- 设计一个shell程序,每个月的第一天备份并压缩/workspace下面的所有内容,存放到/root/bak目录里,且文件名如下格式 yymmdd_bak, yy mm dd 代表年月日
#!/bin/bash
dir=/home/wxl/workspace
time=`date +%y%m%d`
[ -d $dir ] || [ mkdir -p $dir ]
tar zcf ${dir}/${time}_bak.tar.gz /home/wxl/workspace/csdn_code
# back /home/wxl/workspace/csdn_code at first day of every month
# 分 时 日 月 周 00 00 01 表示每个月的第一天的0点0分开始执行保存操作
00 00 01 * * /bin/sh /home/wxl/workspace/shellScripts/backFile.sh &>/home/wxl/workspace/log
- 统计目前登录计算机的用户
# 实际就是统计/etc/passwd下面的/bin/bash命令解释器的个数
wxl@ubuntu:~$ grep -c "/bin/bash" /etc/passwd
2
- 去掉字符串中所有的空格
wxl@ubuntu:~$ str='I am aaa'
wxl@ubuntu:~$ echo ${str// /}
Iamaaa
# 使用tr
wxl@ubuntu:~$ echo ${str}|tr -d ' '
Iamaaa
# 使用sed
echo $str|sed 's# ##g'
- 重定向标准输出和标准错误到log.txt
cmd &>/dev/log.txt
cmd >/dev/log.txt 2>&1
- val:−10和{val:-10}和val:−10和{val: -10}的区别
${val:-10} 表示如果val没有定义或者是空的话,就设置为10
${val: -10} 表示显示变量的最后10个字符