shell 脚本2

fs:

mountd nfs rpc-bind 或者 将客户端加入trousted

iSCSI:

端口3260

test  ##判断条件是否成立

 1.test参数

 test 等同于 [ ]

字符类型数据

 [ "$a" = "$b" ]     ##$a和$b是否一致

 [ "$a" != "$b" ]    ##$a和$b是否不一致

 [ ! "$a" = "$b" ]   ##$a和$b是否一致,且该条件不成立时

整型数据

 [ "$a" -eq "$b" ]   ##$a和$b是否 =

 [ "$a" -ne "$b" ]   ##$a和$b是否 !=

 [ "$a" -lt "$b" ]   ##$a是否 < $b

 [ "$a" -le "$b" ]   ##$a是否 <= $b

 [ "$a" -gt "$b" ]   ##$a是否 > $b

 [ "$a" -ge "$b" ]   ##$a是否 >= $b

 [ "$a" = "$b" -o "$a" -lt 100 ]  ##$a是否等于$b,或者$a小于100

 [ "$a" = "$b" -a "$a" -gt 100 ]  ##$a是否等于$b,并且$a大于100

 [ -z "$a" ]  ##$a是否为空

 [ -n "$a" ]  ##$a是否不为空

文件

 [ "file1" -ef "file2" ]  ##节点是否一致,即文件是否一样

建立文件,查看节点,创建硬链接

[root@foundation77 ost]# touch test

[root@foundation77 ost]# ls -i /ost/test

134975442 /ost/test

[root@foundation77 ost]# ln /ost/test /ost/test123

[root@foundation77 ost]#

[root@foundation77 ost]# ls -i test test123

134975442 test  134975442 test123

 [ "file1" -nt "file2" ]  ##file1是否比file2时间new

 [ "file1" -ot "file2" ]  ##file1是否比file2时间out

 [ -e "file" ]  ##文件是否存在

 [ -f "file" ]  ##是否是普通文件

 [ -L "file" ]  ##是否是软链接文件

 [ -S "file" ]  ##是否是套接字

 [ -b "file" ]  ##是否是块设备

 [ -d "file" ]  ##是否是目录

 [ -c "file" ]  ##是否是字符文件

[root@foundation77 ost]# [ -b "/dev/sdc1" ] && echo yes || echo no

yes

[root@foundation77 ost]#  [ -d "/ost" ] && echo yes || echo no

yes

[root@foundation77 ost]# [ -c "/dev/pts/0" ] && echo yes || echo no

yes

[root@foundation77 ost]# ln -s test test456

[root@foundation77 ost]# [ -L "/ost/test456" ] && echo yes || echo no

yes

 

 2.编写脚本check_num.sh判断数字是否在0-10之间

[root@foundation77 ost]# vim check_num.sh

[root@foundation77 ost]# cat check_num.sh

#!/bin/bash

[ $1 -gt "0" -a $1 -lt "10" ] && echo yes || echo no

[root@foundation77 ost]# /ost/check_num.sh 7

yes

[root@foundation77 ost]# /ost/check_num.sh 8

yes

[root@foundation77 ost]# /ost/check_num.sh 11

no

 

 3.编写脚本clean_log.sh,当超级用户时,情况日志,当普通用户,提示请切换用户

#!/bin/bash

User_Id=`id -u`

[ "$User_Id" -eq "0" ] && `> /var/log/messages` || echo please change root!!

 4.编写脚本check_file.sh判断文件类型

#!/bin/bash

[ -z "$1" ] && {

        echo please give a filename

        exit 1

}

[ -e "$1" ] || echo $1 is not exit

[ -L "$1" ] && {

        echo $1 is a link file

        exit 1

}

[ -f "$1" ] && echo $1 is a common file

[ -S "$1" ] && echo $1 is a sock file

[ -b "$1" ] && echo $1 is a bock

[ -d "$1" ] && echo $1 is a directory

[ -c "$1" ] && echo $1 is a char file

 

[root@foundation77 ost]# /ost/check_file.sh

please give a filename

[root@foundation77 ost]# /ost/check_file.sh test

test is a common file

[root@foundation77 ost]# /ost/check_file.sh test123

test123 is a common file

[root@foundation77 ost]# /ost/check_file.sh test456

test456 is a link file

[root@foundation77 ost]# /ost/check_file.sh /ost

/ost is a directory

[root@foundation77 ost]# /ost/check_file.sh /dev/sdc1

/dev/sdc1 is a bock

 

tr   ##转换字符

[root@foundation77 ost]# vim sing

[root@foundation77 ost]# vim sing

[root@foundation77 ost]# tr  'a-f' 'A-F' < /ost/sing

123

hEllo worlD123

[root@foundation77 ost]#  tr '1-2' '#' < /ost/sing

##3

hello world##3

 

 

find  ##查找文件

 1.find参数

 -group   ##查找组

 -type    ##查找类型

 -user    ##查找用户

 -not -group  ##查找不是该组的文件

 -a | -o  ## and | or

 -perm 777  ##精确匹配权限777的文件

 -perm -644 ##匹配u=6、g=4、o=4的文件

 -perm /666 ##匹配u=6或g=6或o=6的文件

 -size 20k  ##查找文件大小为20k的文件

 -size -20k ##查找文件大小为小于20k的文件

 -size +20k ##查找文件大小为大于20k的文件

 -maxdepth 1 -name passwd  ##查找目录的深度为1层

 -mindepth 2 -maxdepth 2 -name passwd  ##只查找目录的第2层

 ########## 注意:最大深度为2层  ######################

猜你喜欢

转载自blog.csdn.net/period000/article/details/80713722