shell编程(三)| shell编程常用命令 test,tr

一,test命令

test命令通常做判断 test 命令和 [ ] 等同

注意 括号两边都有空格的!!!!

[ "$A" = "$B" ]           是否相等
[ "$A" != "$B" ]         是否不相等
[ "$A" -eq "$B" ]       是否相等
[ "$A" -ne "$B" ]        是否不相等
[ "$A" -le "$B" ]         是否小于
[ "$A" -lt "$B" ]           是否小于等于
[ "$A" -ge "$B" ]        是否大于
[ "$A" -gt "$B" ]          是否大于等于
[ "$A" -ne "$B" -a "$A" -gt "$B" ]    A 不等于 B 并且 A 大于等于B
[ "$A" -ne "$B" -o "$A" -gt "$B" ]    A不等于B 或者  A大于等于B
[ -z "$A" ]                   是否为空
[ -n "$A" ]                   是否不为空

test File1 –ef File2    两个文件是否为同一个文件,可用于硬连接。主要判断两个文件是否指向同一个inode!!!!
test File1 –nt File2    判断文件1是否比文件2新
test File1 –ot File2    判断文件1比是否文件2旧

-b<文件>:如果文件为一个块特殊文件,则为真;
-c<文件>:如果文件为一个字符特殊文件,则为真;
-d<文件>:如果文件为一个目录,则为真;
-e<文件>:如果文件存在,则为真;
-f<文件>:如果文件为一个普通文件,则为真;
-S<文件>:如果文件为一个套接字特殊文件,则为真;
–L File : 文件是否是一个符号链接

1,

[root@100 mnt]# a=1                                        >>>>>给1,2赋值
[root@100 mnt]# b=2
[root@100 mnt]# test "$a" = "$b" && echo yes || echo no    >>>>test判断,如果相等,打印yes,如果不相等,打印no
no
[root@100 mnt]# b=1
[root@100 mnt]# test "$a" = "$b" && echo yes || echo no 
yes
[root@100 mnt]# ["$a" = "$b"] && echo yes || echo no       >>>>!!!!!!!!!!!  [ ]内容的左右两侧有空格,否则会报错
bash: [1: command not found...
no
[root@100 mnt]# [ "$a" = "$b" ] && echo yes || echo no 
yes

2,

[root@100 mnt]# a=0
[root@100 mnt]# echo $a                  >>>>不是打印$a,而是打印变量a的值
0
[root@100 mnt]# echo $c                  >>>>没有给c赋值,所以值就是空

[root@100 mnt]# [ -z "$c" ] && echo yes || echo no               >>>-z 是否为空
yes
[root@100 mnt]# [ -n "$c" ] && echo yes || echo no               >>>-n 是否不为空 即是否有值
no
[root@100 mnt]# [ -z "$a" ] && echo yes || echo no               
no
[root@100 mnt]# [ -n "$a" ] && echo yes || echo no
yes

3,

[root@100 haha]# ls
[root@100 haha]# [ -e "file" ] && echo yes || echo no               >>>>file不存在
no
[root@100 haha]# touch file
[root@100 haha]# [ -e "file" ] && echo yes || echo no               >>>>file存在
yes
[root@100 haha]# [ -f "file" ] && echo yes || echo no               >>>>file是普通文件
yes
[root@100 haha]# [ -L "file" ] && echo yes || echo no               >>>>file不是软连接文件
no
[root@100 haha]# ln -s /etc/passwd passwd                           >>>>>建立软连接!!!passwd原来不存在的
[root@100 haha]# ll
total 0
-rw-r--r--. 1 root root  0 Jun 14 02:51 file
lrwxrwxrwx. 1 root root 11 Jun 14 02:53 passwd -> /etc/passwd
[root@100 haha]# [ -L "passwd" ] && echo yes || echo no              >>>>>passwd是软连接
yes
[root@100 haha]# [ -S "/run/rpcbind.sock" ] && echo yes || echo no   >>>>/run/rpcbind.sock是套接字
yes
[root@100 haha]# [ -b "/dev/vdb" ] && echo yes || echo no            >>>>/dev/vdb是块设备
yes
[root@100 dev]# [ -c "/dev/pts/0" ] && echo yes || echo no           >>>>/dev/pts/0是字符设备
yes

二,shell编程练习

1,

判断ip地址是否连通,并且判断是否给出ip

[root@100 mnt]# vim check_ip.sh 
[root@100 mnt]# cat check_ip.sh 
#################################
# Author:           Minz        #
# Version:                      #
# Mail:           [email protected] #
# Date:        2018-06-14       #
# Description                   #
#                               #
#################################

#!/bin/bash
[ -z "$1" ] && {                                            >>>>>>判断执行脚本时后面跟是不是跟了变量
	echo please give me a ipaddress !!
	exit 1                                              >>>>>如果是空的,打印信息,exit强行退出
}
ping -c1 -w1 $1 &> /dev/null && {
	echo "$1 is up"
}||{
	echo "$1 is down" }

测试

[root@100 mnt]# sh check_ip.sh                       >>>>>脚本后面没有跟变量,输出信息
please give me a ipaddress !!
[root@100 mnt]# sh check_ip.sh 172.25.254.256
172.25.254.256 is down
[root@100 mnt]# sh check_ip.sh 172.25.254.156
172.25.254.156 is up

2,

给一个数字,判断数字是不是在10以内

[root@100 mnt]# vim number_check.sh
[root@100 mnt]# cat number_check.sh 
#################################
# Author:           Minz        #
# Version:                      #
# Mail:           [email protected] #
# Date:        2018-06-14       #
# Description                   #
#                               #
#################################

#!/bin/bash
[ -z "$1" ] && {                              >>>>>是否给出数字判断
	echo "Please input a number "
	exit 1	
}	
[ "$1" -gt "0" -a "$1" -lt "10" ] && {        >>>>-gt 表示大于等于 -a 表示 并且 -lt 表示小于等于
	echo "$1 is between 1~10"
} || {
	echo "$1 is not between 1~10"
}

测试

[root@100 mnt]# sh number_check.sh             >>>>缺少变量
Please input a number 
[root@100 mnt]# sh number_check.sh 9           >>>>>成功!
9 is between 1~10
[root@100 mnt]# sh number_check.sh 11          >>>>>成功!
11 is not between 1~10

3,

判断一个文件的类型

[root@100 mnt]# vim file_check.sh
[root@100 mnt]# cat file_check.sh 
#################################
# Author:           Minz        #
# Version:                      #
# Mail:           [email protected] #
# Date:        2018-06-14       #
# Description                   #
#                               #
#################################

#!/bin/bash
[ -z "$1" ] && {
	echo "Please give a filename !!!"           >>>>>判断是否给出了变量
	exit 1
}
[ -e "$1" ] && {     
	echo $1 is exist !!!
} || {
	echo $1 is not exist !!!
	exit 0	
}

[ -f "$1" ] && { 
	echo $1 is a regular file
 } 
[ -L "$1" ] && { 
	echo $1 is a symbolic link
	exit 0 
} 
[ -S "$1" ] && { 
	echo $1 is a socket
	exit 0 	
} 
[ -b "$1" ] && { 
	echo $1 is a block special
	exit 0 	
} 
[ -d "$1" ] && { 
	echo $1 is a directory
	exit 0 	
} 
[ -c "$1" ] && { 
	echo $1 is a character special
	exit 0
}	 

注意:上面判断文件是否存在也可以写为

[ -e "$1" ] || {                                 >>>>如果不存在就打印信息退出,存在就不返回任何信息
    echo $1 is not exist !!!
    exit 0    
}

或者改为[ !-e "$1" ] && { ...           >>>>!表示非 不存在

测试

[root@100 mnt]# sh file_check.sh 
Please give a filename !!!
[root@100 mnt]# sh file_check.sh /mnt/
/mnt/ is exist !!!
/mnt/ is a directory
[root@100 mnt]# sh file_check.sh /mnt/haha/file 
/mnt/haha/file is exist !!!
/mnt/haha/file is a regular file
[root@100 mnt]# sh file_check.sh /mnt/haha/passwd 
/mnt/haha/passwd is exist !!!
/mnt/haha/passwd is a regular file
/mnt/haha/passwd is a symbolic link
[root@100 mnt]# sh file_check.sh /dev/pts/ptmx 
/dev/pts/ptmx is exist !!!
/dev/pts/ptmx is a character special
[root@100 mnt]# sh file_check.sh /dev/vdb
/dev/vdb is exist !!!
/dev/vdb is a block special
[root@100 mnt]# sh file_check.sh 1111111111
1111111111 is not exist !!!
成功!!!


三,tr命令

tr命令通常用于大小写转换

示例:判断输入是不是hello

[root@100 mnt]# vim hello.sh
[root@100 mnt]# cat hello.sh                             >>>>>判断输入是不是hello
#################################
# Author:           Minz        #
# Version:                      #
# Mail:           [email protected] #
# Date:        2018-06-14       #
# Description                   #
#                               #
#################################

#!/bin/bash
[ "$1" = "hello" ] && {
	echo yes
} || {
	echo no
}
[root@100 mnt]# sh hello.sh hello
yes
[root@100 mnt]# sh hello.sh HELLO                        >>>>>存在大小写的错误
no

小写字母转换为大写字母

[root@100 mnt]# tr 'a-z' 'A-Z' < hello.sh         >>>>> 注意书写格式  'a-z' 'A-Z' 是小写字母转大写字母
#################################
# AUTHOR:           MINZ        #
# VERSION:                      #
# MAIL:           [email protected] #
# DATE:        2018-06-14       #
# DESCRIPTION                   #
#                               #
#################################

#!/BIN/BASH
[ "$1" = "HELLO" ] && {
	ECHO YES
} || {
	ECHO NO
}

大写字母转换为小写字母

[root@100 mnt]# tr 'A-Z' 'a-z' < hello.sh               >>>>>>注意书写格式 'A-Z' 'a-z' 是大写转换为小写
#################################
# author:           minz        #
# version:                      #
# mail:           [email protected] #
# date:        2018-06-14       #
# description                   #
#                               #
#################################

#!/bin/bash
[ "$1" = "hello" ] && {
	echo yes
} || {
	echo no
}

修改脚本

[root@100 mnt]# vim hello.sh
[root@100 mnt]# cat hello.sh 
#################################
# Author:           Minz        #
# Version:                      #
# Mail:           [email protected] #
# Date:        2018-06-14       #
# Description                   #
#                               #
#################################

#!/bin/bash
WORD=$( echo $1 | tr 'A-Z' 'a-z' )       >>>>输入由大写转换为小写,并且作为一个变量存在,前面要加$符号
[ "$WORD" = "hello" ] && {               >>>>由于之前的$1已经被处理过了,所以这里写$1逻辑上是有问题的,应该改为我们自己的变量名称
	echo yes
} || {
	echo no
}

测试

[root@100 mnt]# sh hello.sh HELLO        >>>>现在也可以时别大写
yes
[root@100 mnt]# sh hello.sh hello
yes

四,shell编程练习

给出用户名和密码,如果用户存在,打印用户存在,如果不存在,创建用户

方法一

[root@100 mnt]# vim user_check.sh
[root@100 mnt]# cat user_check.sh 
#################################
# Author:           Minz        #
# Version:                      #
# Mail:           [email protected] #
# Date:        2018-06-14       #
# Description                   #
#                               #
#################################

#!/bin/bash
[ "$#" -eq "2" ] || {                                      >>>>>$#表示变量的个数,这里用户名和密码两个变量
	echo "Please input username and password !!!"
	exit 1
}

Check_User=`getent passwd $1`                              >>>>>查找用户的信息 ,这里用``先执行里面的内容,直接赋值给Check_User
[ -n "$Check_User" ] && {                                  >>>>>如果信息不为空,打印存在,然后退出
	echo $1 is exist !
	exit 1
}

useradd $1                                                 >>>>如果信息为空,执行useradd
echo $2 | passwd --stdin $1                                >>>>非交互式建立用户
[root@100 mnt]# getent passwd root             >>>> getent 用来察看系统的数据库中的相关记录,这里查看passwd库里面root的信息
root:x:0:0:root:/root:/bin/bash
测试:
[root@100 mnt]# sh user_check.sh hh 123            >>>>>自动创建
Changing password for user hh.
passwd: all authentication tokens updated successfully.
[root@100 mnt]# sh user_check.sh hh 123
hh is exist !

注意:上面的Check_User段也可以改为

Userinfo=$( getent passwd $1 )              >>>>>括号里面先执行,然后$赋值
[ -n "$Userinfo" ] && {
       echo $1 is exist !
       exit 1
}

也可以改为

Userinfo=$( grep "^$1\>" /etc/passwd )              >>>>>grep过滤 ^匹配行首 \>防止向后扩展 注意一定要""引起来!!!
[ -n "Userinfo" ] && {
        echo $1 is exist !
        exit 1
}

猜你喜欢

转载自blog.csdn.net/ha_weii/article/details/80694394