随笔 - 56, 文章 - 0, 评论 - 0, 引用 - 0
数据加载中……

《LInux与Unix Shell编程指南》笔记之命令执行顺序

                                                               命令执行顺序
                                                            点击打开本章pdf文档
&&和||可以完成这样的任务,相应的命令可以为系统命令和shell脚本。
    
    使用&&
    格式为:
    命令1 && 命令2
    在命令1不出现错误即为真的情况下,命令2才能执行,比如:
    [root@localhost test1]# cp myfile ./bin/etc/myfile  && echo "if you are seeing this 
    then cp was OK"
    cp: 无法创建一般文件“./bin/etc/myfile”: 没有那个文件或目录
    因为当前目录为test1,所以拷贝失败。下面在当前目录下拷贝,成功。
    [root@localhost test1]# cp myfile myfile1 && echo "if you are seeing this then cp was 
    OK"
    if you are seeing this then cp ws OK
    
    使用||
    格式为:
    命令1 || 命令2
    说明:如果||左边的命令1未执行成功,那么执行命令2;如果命令1执行成功,那么命令2被忽略。也就是说两个命令中只
有一个被执行,如果两个命令都正确,则只执行命令1。比如:
    [root@localhost test1]# ls 
    :  
1-soft.txt  2-hard.txt  f_desc  file1  myfile  myfile1  stock.txt  stork.txt
    [root@localhost test1]# rm myfile2 
|| echo "rm error!"
    rm myfile2 
|| echo "rm error"
    rm: lstat “myfile2” 失败: 没有那个文件或目录
    rm error
    [root@localhost test1]# rm myfile1 
|| echo "rm ok"
    rm:是否删除 一般文件 “myfile1”
? y
    [root@localhost test1]# 
    
    使用()和{}将命令结合在一起
    在当前shell中执行一组命令,可以用命令分隔符;隔开每一个命令,并把所有命令用()括起来。一般形式为:
    (命令1;命令2;...)
    比如:
    [root@localhost test1]# cp myfile myfile1 && (echo "OK";echo "OK,again")
    OK
    OK,again 

posted on 2011-02-20 11:17 八路 阅读(356) 评论(0)  编辑 收藏 引用 所属分类: 嵌入式linux笔记


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理