shell中连接符(并且、和、或者)
2017-06-14 17:03
85 查看
&&(并且) 与 ||(或者)与;(和)
在上面刚刚提到了分号,用于多条命令间的分隔符。另外还有两个可以用于多条命令中间的特殊符号,那就是 “&&” 和 “||” 下面把这几种情况全列出:
command1 ; command2 和
command1 && command2 并且
command1 || command2 或者
&&:左边命令执行成功才会执行右边的命令。
|| :左边命令执行不成功,才会执行右边的命令。
;(分号):左边命令执行成功与否,后边的命令都会执行。
使用 ”;” 时,不管command1是否执行成功都会执行command2;
使用 “&&” 时,只有command1执行成功后,command2才会执行,否则command2不执行;
使用 “||” 时,command1执行成功后command2 不执行,否则去执行command2,总之command1和command2总有一条命令会执行。
在做实验前,想把所有的 test* 删除掉,可是删除的时候,却提示说权限不够,下面是排除问题的过程:
[root@localhost ~]# rm -rf test*
rm: 无法删除"test2/test1": 权限不够
rm: 无法删除"test2/test3": 权限不够
rm: 无法删除"test2/test4": 权限不够
[root@localhost ~]# ls test*
test1 test3 test4
[root@localhost ~]# lsattr test*
-----a-------e- test2/test1
----i--------e- test2/test3
-------------e- test2/test4
[root@localhost ~]# chattr -a test2/test1
[root@localhost ~]# chattr -i test2/test3
[root@localhost ~]# rm -rf test*
rm: 无法删除"test2/test1": 权限不够
rm: 无法删除"test2/test3": 权限不够
rm: 无法删除"test2/test4": 权限不够
[root@localhost ~]# ls test*
test1 test3 test4
[root@localhost ~]# ls -ld test*
drwxrwxr-x 2 root root 4096 5月 10 10:12 test2
[root@localhost ~]# ls -l test2/*
-rw-r--r-- 1 root root 6 5月 10 10:20 test2/test1
-rw-r--r-- 1 root root 0 5月 10 10:11 test2/test3
-rw-r--r-- 1 root root 0 5月 10 10:12 test2/test4
[root@localhost ~]# lsattr test2/*
-------------e- test2/test1
-------------e- test2/test3
-------------e- test2/test4
[root@localhost ~]# lsattr test2
-------------e- test2/test1
-------------e- test2/test3
-------------e- test2/test4
[root@localhost ~]# lsattr -d test2
----i--------e- test2
[root@localhost ~]# chattr -i test2/
[root@localhost ~]# rm -rf test2/
如果你之前跟着做过同样的实验,相信你也会出现同样的问题的。接下来要通过做实验来说明 “&&” 与 “||” 这两个特殊符号的作用:
[root@localhost ~]# touch test1 test3
[root@localhost ~]# ls test2 && touch test2
ls: 无法访问test2: 没有那个文件或目录
[root@localhost ~]# ls test2
ls: 无法访问test2: 没有那个文件或目录
[root@localhost ~]# ls test2 || touch test2
ls: 无法访问test2: 没有那个文件或目录
[root@localhost ~]# ls test*
test1 test2 test3
在上面刚刚提到了分号,用于多条命令间的分隔符。另外还有两个可以用于多条命令中间的特殊符号,那就是 “&&” 和 “||” 下面把这几种情况全列出:
command1 ; command2 和
command1 && command2 并且
command1 || command2 或者
&&:左边命令执行成功才会执行右边的命令。
|| :左边命令执行不成功,才会执行右边的命令。
;(分号):左边命令执行成功与否,后边的命令都会执行。
使用 ”;” 时,不管command1是否执行成功都会执行command2;
使用 “&&” 时,只有command1执行成功后,command2才会执行,否则command2不执行;
使用 “||” 时,command1执行成功后command2 不执行,否则去执行command2,总之command1和command2总有一条命令会执行。
在做实验前,想把所有的 test* 删除掉,可是删除的时候,却提示说权限不够,下面是排除问题的过程:
[root@localhost ~]# rm -rf test*
rm: 无法删除"test2/test1": 权限不够
rm: 无法删除"test2/test3": 权限不够
rm: 无法删除"test2/test4": 权限不够
[root@localhost ~]# ls test*
test1 test3 test4
[root@localhost ~]# lsattr test*
-----a-------e- test2/test1
----i--------e- test2/test3
-------------e- test2/test4
[root@localhost ~]# chattr -a test2/test1
[root@localhost ~]# chattr -i test2/test3
[root@localhost ~]# rm -rf test*
rm: 无法删除"test2/test1": 权限不够
rm: 无法删除"test2/test3": 权限不够
rm: 无法删除"test2/test4": 权限不够
[root@localhost ~]# ls test*
test1 test3 test4
[root@localhost ~]# ls -ld test*
drwxrwxr-x 2 root root 4096 5月 10 10:12 test2
[root@localhost ~]# ls -l test2/*
-rw-r--r-- 1 root root 6 5月 10 10:20 test2/test1
-rw-r--r-- 1 root root 0 5月 10 10:11 test2/test3
-rw-r--r-- 1 root root 0 5月 10 10:12 test2/test4
[root@localhost ~]# lsattr test2/*
-------------e- test2/test1
-------------e- test2/test3
-------------e- test2/test4
[root@localhost ~]# lsattr test2
-------------e- test2/test1
-------------e- test2/test3
-------------e- test2/test4
[root@localhost ~]# lsattr -d test2
----i--------e- test2
[root@localhost ~]# chattr -i test2/
[root@localhost ~]# rm -rf test2/
如果你之前跟着做过同样的实验,相信你也会出现同样的问题的。接下来要通过做实验来说明 “&&” 与 “||” 这两个特殊符号的作用:
[root@localhost ~]# touch test1 test3
[root@localhost ~]# ls test2 && touch test2
ls: 无法访问test2: 没有那个文件或目录
[root@localhost ~]# ls test2
ls: 无法访问test2: 没有那个文件或目录
[root@localhost ~]# ls test2 || touch test2
ls: 无法访问test2: 没有那个文件或目录
[root@localhost ~]# ls test*
test1 test2 test3
相关文章推荐
- shell中连接符(并且、和、或者)
- shell 脚本 更新或者添加host ,并且增加hostname映射到hosts (修改)
- shell入门-连接符(并且、和、或者)
- 能连接打印服务器,并且能看到服务器上的共享打印机,但是不能连接打印机,有提示信息:“操作无法完成,键入的打印机名不正确,或者指定的打印机没有连接到服务器上,有关详细信息,请单击帮助”。
- shell中的并且、和、或者
- shell 脚本 更新或者添加host ,并且增加hostname映射到hosts (修改)
- shell 中的条件判断 “并且” “或者”
- wp7模拟器开启不了,显示“被调用的对象已与其客户端断开连接”或者“接口”。并且提示有文件损坏的处理方法
- 配置win7 iis后 本地连接网址 打不开网站或者一直在加载 网页加载不出来并且 提示下列错误
- shell判断文件,目录是否存在或者具有权限
- shell判断文件,目录是否存在或者具有权限
- 解决Installer重新安装补丁错误-"无法打开此修补程序包。请确认该修补程序包存在,并且您可以访问它,或者与应用程序供应商联系,以确认这是否是一个有效的 Windows Installer 修补程序包。"
- 连接浏览器SHELL窗口的代码
- 超时时间已到。超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。
- 使用SHELL命令来停止或者运行服务---service
- 解决大量TCPIP连接后出现“因为系统缺乏足够缓冲区空间或者因为队列已满无法执行套接字上操作”的问题
- Analysis service的manager无法连接,不能连接服务器(xxxxx)注册表,或者还不是olap Administrator组成员
- ASP.NET中数据库的操作初步----连接并且打开数据库
- ASP.NET中数据库的操作初步----连接并且打开数据库
- 工具推荐:如何部署使用“远程桌面Web连接”,并且扩展它,可以连接非3389端口的远程桌面,(服务器管理员必备)