比较运算符
运算符
描述
示例
-e filename
如果 filename 存在,则为真
[ -e /var/log/syslog ]
-d filename
如果 filename 为目录,则为真
[ -d /tmp/mydir ]
-f filename
如果 filename 为常规文件,则为真
-f /usr/bin/grep ]
-L filename
如果 filename 为符号链接,则为真
[ -L /usr/bin/grep ]
-r filename
如果 filename 可读,则为真
[ -r /var/log/syslog ]
-w filename
如果 filename 可写,则为真
[ -w /var/mytmp.txt ]
-x filename
如果 filename 可执行,则为真
[ -L /usr/bin/grep ]
-s filename
如果文件存在且至少有一个字符则为真
-c filename
如果文件存在且为字符型特殊文件则为真
-b filename
如果文件存在且为块特殊文件则为真
filename1 -nt filename2
如果 filename1 比 filename2 新,则为真
[ /tmp/install/etc/services -nt /etc/services ]
filename1 -ot filename2
如果 filename1 比 filename2 旧,则为真
[ /boot/bzImage -ot arch/i386/boot/bzImage ]
字符串比较运算符 (请注意引号的使用,这是防止空格扰乱代码的好方法
运算符
描述
示例
-z string 如果 string
长度为零,则为真
[ -z “$myvar” ]
-n string 如果 string
长度非零,则为真
[ -n “$myvar” ]
string1 = string2
如果 string1 与 string2 相同,则为真
[ “$myvar” = “one two three” ]
string1 != string2
如果 string1 与 string2 不同,则为真
[ “$myvar” != “one two three” ]
运算符
描述
示例
num1 -eq num2
等于
[ 3 -eq $mynum ]
num1 -ne num2
不等于
[ 3 -ne $mynum ]
num1 -lt num2
小于
[ 3 -lt $mynum ]
num1 -le num2
小于或等于
[ 3 -le $mynum ]
num1 -gt num2
大于
[ 3 -gt $mynum ]
num1 -ge num2
大于或等于
[ 3 -ge $mynum ]
shell里面的赋值方法有两种,格式为
arg=`(命令)
arg=$(命令)
如果想要把某一文件的总行数赋值给变量nlines,可以表达为
1 2 3 nlines=`(awk 'END{print NR}' filename)` 或者 nlines=$(awk 'END{print NR}' filename)
shell判断文件是否存在 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 1. shell判断文件,目录是否存在或者具有权限 2. #!/bin/sh 3. 4. myPath="/var/log/httpd/" 5. myFile="/var /log/httpd/access.log" 6. 7. # 这里的-x 参数判断$myPath是否存在并且是否具有可执行权限 8. if [ ! -x "$myPath"]; then 9. mkdir "$myPath" 10. fi 11. 12. # 这里的-d 参数判断$myPath是否存在 13. if [ ! -d "$myPath"]; then 14. mkdir "$myPath" 15. fi 16. 17. # 这里的-f参数判断$myFile是否存在 18. if [ ! -f "$myFile" ]; then 19. touch "$myFile" 20. fi 21. 22. # 其他参数还有-n,-n是判断一个变量是否是否有值 23. if [ ! -n "$myVar" ]; then 24. echo "$myVar is empty" 25. exit 0 26. fi 27. 28. # 两个变量判断是否相等 29. if [ "$var1" = "$var2" ]; then 30. echo '$var1 eq $var2' 31. else 32. echo '$var1 not eq $var2' 33. fi
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 -f 和-e的区别 Conditional Logic on Files -a file exists. -b file exists and is a block special file. -c file exists and is a character special file. -d file exists and is a directory. -e file exists (just the same as -a). -f file exists and is a regular file. -g file exists and has its setgid(2) bit set. -G file exists and has the same group ID as this process. -k file exists and has its sticky bit set. -L file exists and is a symbolic link. -n string length is not zero. -o Named option is set on. -O file exists and is owned by the user ID of this process. -p file exists and is a first in, first out (FIFO) special file or named pipe. -r file exists and is readable by the current process. -s file exists and has a size greater than zero. -S file exists and is a socket. -t file descriptor number fildes is open and associated with a terminal device. -u file exists and has its setuid(2) bit set. -w file exists and is writable by the current process. -x file exists and is executable by the current process. -z string length is zero.
shell 脚本中$$,$#,$?分别代表什么意思? 1 2 3 4 5 6 7 8 9 10 11 12 $0 这个程式的执行名字 $n 这个程式的第n个参数值,n=1..9 $* 这个程式的所有参数,此选项参数可超过9个。 $# 这个程式的参数个数 $$ 这个程式的PID(脚本运行的当前进程ID号) $! 执行上一个背景指令的PID(后台运行的最后一个进程的进程ID号) $? 执行上一个指令的返回值 (显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误) $- 显示shell使用的当前选项,与set命令功能相同 $@ 跟$*类似,但是可以当作数组用 sh -x 显示执行该脚本执行所有变量的值 sh -n 返回所有语法错误信息
shell 脚本中$$,$#,$?分别代表什么意思?
shell中设置文字输出的颜色及字体格式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 #下面是字体输出颜色及终端格式控制 #字体色范围:30-37 echo -e "\033[30m 黑色字 \033[0m" echo -e "\033[31m 红色字 \033[0m" echo -e "\033[32m 绿色字 \033[0m" echo -e "\033[33m ×××字 \033[0m" echo -e "\033[34m 蓝色字 \033[0m" echo -e "\033[35m 紫色字 \033[0m" echo -e "\033[36m 天蓝字 \033[0m" echo -e "\033[37m 白色字 \033[0m" #字背景颜色范围:40-47 echo -e "\033[40;37m 黑底白字 \033[0m" echo -e "\033[41;30m 红底黑字 \033[0m" echo -e "\033[42;34m 绿底蓝字 \033[0m" echo -e "\033[43;34m 黄底蓝字 \033[0m" echo -e "\033[44;30m 蓝底黑字 \033[0m" echo -e "\033[45;30m 紫底黑字 \033[0m" echo -e "\033[46;30m 天蓝底黑字 \033[0m" echo -e "\033[47;34m 白底蓝字 \033[0m" #控制选项说明 #\033[0m 关闭所有属性 #\033[1m 设置高亮度 #\033[4m 下划线 echo -e "\033[4;31m 下划线红字 \033[0m" #闪烁 echo -e "\033[5;34m 红字在闪烁 \033[0m" #反影 echo -e "\033[8m 消隐 \033[0m " #\033[30m-\033[37m 设置前景色 #\033[40m-\033[47m 设置背景色 #\033[nA光标上移n行 #\033[nB光标下移n行 echo -e "\033[4A 光标上移4行 \033[0m" #\033[nC光标右移n行 #\033[nD光标左移n行 #\033[y;xH设置光标位置 #\033[2J清屏 #\033[K清除从光标到行尾的内容 echo -e "\033[K 清除光标到行尾的内容 \033[0m" #\033[s 保存光标位置 #\033[u 恢复光标位置 #\033[?25| 隐藏光标 #\033[?25h 显示光标 echo -e "\033[?25l 隐藏光标 \033[0m" echo -e "\033[?25h 显示光标 \033[0m"
shell中设置文字输出的颜色及字体格式
判断输入参数的个数(命令行) 1 2 3 4 5 6 7 8 9 10 11 12 if [ $# != 3 ] ; then echo "USAGE: $0 from to" echo " e.g.: $0 ~/oucaijun/from ~/oucaijun/to" exit 1; fi ———————————————————————— $#代表了命令行的参数数量 位置参数 $1, $2,..., $N,$#代表了命令行的参数数量, $0代表了脚本的名字 -ne 不等于
linux脚本:shell, 判断输入参数的个数(命令行)
软连接
linux 创建连接命令 ln -s 软链接
ossec OSSEC安装