首先是:
if -then
格式:if command
then
commands
fi
-------------
根据if 后的命令的退出状态码来确定是否执行then后的语句。
如果命令的退出状态码为0,则执行。表示,命令运行成功。
[oh@localhost shell]$ ./s6
Mon Mar 26 14:14:22 CST 2018
that's ok
[oh@localhost shell]$ cat s6
#!/bin/bash
if date
thenecho "that's ok"
fi[oh@localhost shell]$ vim s6
[oh@localhost shell]$ ./s6
Mon Mar 26 14:15:45 CST 2018
that's ok
0
./s6: line 7: skjfdkjfskf: command not found
0
[oh@localhost shell]$ cat -n s61 #!/bin/bash2 if date 3 then4 echo "that's ok"5 fi6 echo $?7 if skjfdkjfskf8 then echo "ooo"9 fi10 echo $?
虽然出错了,但是shell会依旧执行后面的语句,只是有个报错语句。
而且then后可以有多个命令
还有一种if格式:
if command;then
commands
fi
---------
[oh@localhost shell]$ ./s7
oh:x:500:500:oh:/home/oh:/bin/bash
the bash file for user oh are:
/home/oh/.bash_history /home/oh/.bash_logout /home/oh/.bash_profile /home/oh/.bashrc
[oh@localhost shell]$ cat s7
#!/bin/bash
testuser=oh
if grep $testuser /etc/passwd
thenecho the bash file for user $testuser are:ls -a /home/$testuser/.b*
fi
-----
if-then-else
if command
then
commands
else
commands
fi
还有就是嵌套if
if command1
then
commands
elif command2
then
commands
fi
当然都是根据退出状态码是否为0来确定是否执行
---------------
test命令:放在if条件中 test condition
这样就可以放除了命令之外的东西在if条件里了
test condition
---
if test condition
then
commands
fi
当然如果你不想写test
可以用[ condition ]注意空格
if [ condition ]
then
commans
fi
---
test里可以判断三种条件:
数值的比较
字符串。。
文件。。
( EXPRESSION )EXPRESSION is true! EXPRESSIONEXPRESSION is falseEXPRESSION1 -a EXPRESSION2both EXPRESSION1 and EXPRESSION2 are trueEXPRESSION1 -o EXPRESSION2either EXPRESSION1 or EXPRESSION2 is true-n STRINGthe length of STRING is nonzeroSTRING equivalent to -n STRING-z STRINGthe length of STRING is zeroSTRING1 = STRING2the strings are equalSTRING1 != STRING2the strings are not equalINTEGER1 -eq INTEGER2INTEGER1 is equal to INTEGER2INTEGER1 -ge INTEGER2INTEGER1 is greater than or equal to INTEGER2INTEGER1 -gt INTEGER2INTEGER1 is greater than INTEGER2INTEGER1 -le INTEGER2INTEGER1 is less than or equal to INTEGER2INTEGER1 -lt INTEGER2INTEGER1 is less than INTEGER2INTEGER1 -ne INTEGER2INTEGER1 is not equal to INTEGER2FILE1 -ef FILE2FILE1 and FILE2 have the same device and inode numbersFILE1 -nt FILE2FILE1 is newer (modification date) than FILE2FILE1 -ot FILE2FILE1 is older than FILE2-b FILEFILE exists and is block special-c FILEFILE exists and is character special-d FILEFILE exists and is a directory-e FILEFILE exists-f FILEFILE exists and is a regular file-g FILEFILE exists and is set-group-ID-G FILEFILE exists and is owned by the effective group ID-h FILEFILE exists and is a symbolic link (same as -L)-k FILEFILE exists and has its sticky bit set-L FILEFILE exists and is a symbolic link (same as -h)-O FILEFILE exists and is owned by the effective user ID-p FILEFILE exists and is a named pipe-r FILEFILE exists and read permission is granted-s FILEFILE exists and has a size greater than zero-S FILEFILE exists and is a socket-t FD file descriptor FD is opened on a terminal-u FILEFILE exists and its set-user-ID bit is set-w FILEFILE exists and write permission is granted-x FILEFILE exists and execute (or search) permission is grantedExcept for -h and -L, all FILE-related tests dereference symboliclinks. Beware that parentheses need to be escaped (e.g., by back-slashes) for shells. INTEGER may also be -l STRING, which evaluates tothe length of STRING.
首先是数值比较:equal to ,greater than , less than , not equal to
但是,test无法处理浮点数,只能是整数
但可以用echo显示浮点数
所以注意变量的类型
[ $v -eq 3 ]
注意空格
n1 -eq n2
-ge
-gt
-le
-lt
-ne
-----
字符串的比较:
str1 = str2
!=
<
>
-n str1 检查str1的长度是否为非0 是的话就有数据,继续执行
-z str1 检查str1的长度是否为0 使得话就没数据 继续执行
----
大于> 小于<符号必须转义否则会被理解为重定向
而且,test命令使用标准的ASCII数值决定排序顺序,sort命令使用系统的本地化语言设置中定义的顺序排序。对英语,本地化设置指定了顺序排序中小写字母出现在大写字母前。
空的和非初始化的变量对脚本来说是个灾难。
-------------
文件的比较:
-d file1 是否存在并且是个目录
-e file file是否存在
-f file file是否存在并且是个文件
-r file 存在并可读
-s file 存在并非空
-w file 存在并可写
-x file 存在并可执行
-O file 存在并是否属当前用户所有
-G file 存在并属当前用户组
file1 -nt file2 file1 new than file2
file1 -ot file2 old than
----
如果你想使用数学上的高级运算符,可以使用(())双括号
在if条件或是脚本里的赋值给变量
(( r= 2 ** 3 ))才是对的
[oh@localhost shell]$ cat -n s81 #!/bin/bash2 (( v= 2 ** 3 ))3 echo v= $v4 v1=(( 3 ** 3 ))5 echo v1= $v16 v2=<< 2 ** 4 >>7 echo v2=$v28 << v3 = 3 ** 3 >>9 echo v3=$v310
[oh@localhost shell]$ ./s8
v= 8
./s8: line 4: syntax error near unexpected token `('
./s8: line 4: `v1=(( 3 ** 3 ))'
v1=
./s8: line 10: warning: here-document at line 6 delimited by end-of-file (wanted `2')
./s8: line 10: syntax error near unexpected token `newline'
./s8: line 10: `v2=<< 2 ** 4 >>'
所以双(())内还可用:
val++
val--
--val
++val
!
~ 位求反
**
<< 位移 2倍的关系
>> 右移
& 位布尔和
| 位布尔或
&&
||
-------
而对字符串的操作
[[ $u == r* ]]
与正则表达式配合使用
---------
[oh@localhost shell]$ ./s7
your HOME is exits
. .gitconfig .pulse
.. .gnome2 .pulse-cookie
.abrt .gnome2_private .python_history
.adobe .gnote readme.txt
.bash_history .gnupg .ssh
.bash_logout .gstreamer-0.10 .thumbnails
.bash_profile .gtk-bookmarks usr
.bashrc .gvfs .viminfo
bin .ICEauthority .xinputrc
.cache .imsettings.log .xsession-errors
chromium-el6.repo .lesshst .xsession-errors.old
.config LGPL 下载
.dbus libflashplayer.so 公共的
.dmrc license.pdf 图片
.esd_auth linux 文档
firefox-45.0.1-1.el6.centos.i686.rpm .local 桌面
flash_player_npapi_linux.i386.tar.gz .macromedia 模板
.fontconfig .mozilla 视频
.gconf .nautilus 音乐
.gconfd .pki
[oh@localhost shell]$ cat s7
#!/bin/bash
#testuser=oh
#if grep $testuser /etc/passwd
#then
# echo the bash file for user $testuser are:
# ls -a /home/$testuser/.b*
#fi#look before you leap
if [ -d $HOME ]
then echo "your HOME is exits"cd $HOMEls -a
elseecho "there is no the HOME"
fi[oh@localhost shell]$
------
[ ]中可以直接放目录 /,,/
------
复合条件测试符
[ condition ]&&[ condition ]
[ condition ] || [ condition ]
case命令;
case variable in
parttern 1 | partten2 ) command1;;
pattern3) command2;;
*) commands;;
esac
[oh@localhost shell]$ ./s9
welcome,oh
please enjoy your visit
yeah use ;; to end
[oh@localhost shell]$ cat s9
#!/bin/bash
#use case commandcase $USER in
rich | oh )echo "welcome,$USER"echo "please enjoy your visit"echo "yeah use ;; to end";;
testing)echo "special user testing";;
jessica)echo "do not forget to log off when your'e done";;
*)echo "sorry,you are not allowed here"
esac