首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
  • 您找到你想要的搜索结果了吗?
    是的
    没有找到

    shell系列-shell第十四讲

    1、文件测试 1.1、-e -e判断符主要作用判断文件或者目录是否存在,如果存在条件为真,状态返回值为0,不存在条件为假,状态返回值为1,比如: bash [root@master1 ~]#touch /tmp/haha.txt [root@master1 ~]#[ -e /tmp/haha.txt ] && echo "yes,存在" || echo "no,不存在" yes,存在 [root@master1 ~]#[ -e /tmp ] && echo "yes,存在" || echo "no,不存在" yes,存在 [root@master1 ~]#[ -e /tmppp ] && echo "yes,存在" || echo "no,不存在" no,不存在 -e不仅可以判断文件存在性,还可以判断目录/tmp存在性,所以-e判断符还是很有帮助的。 1.2、-f -f判断符主要作用判断文件存在且为普通文件,条件为真,比如: bash ~]#[ -f /tmp/haha.txt ] && echo "yes,文件存在且为普通文件" || echo "no,文件不存在或者不是普通文件" yes,文件存在且为普通文件 ~]#[ -f /tmp ] && echo "yes,文件存在且为普通文件" || echo "no,文件不存在或者不是普通文件" no,文件不存在或者不是普通文件 /tmp是一个目录,所以这里测试肯定是不存在的。 1.3、-d -d判断符主要作用判断目录存在且为普通文件,条件为真,比如: bash [root@master1 ~]#[ -d /tmp ] && echo "yes,目录存在且为目录" || echo "no,目录不存在或者不是目录" yes,目录存在且为目录 [root@master1 ~]#[ -d /tmpp ] && echo "yes,目录存在且为目录" || echo "no,目录不存在或者不是目录" no,目录不存在或者不是目录 -d判断符在shell脚本中多用来判断目录是否存在,还是很常用的。 1.4、-r -r判断符用来判断文件或者目录是否存在且为可读,比如: bash [root@master1 ~]#[ -r /tmp/haha.txt ] && echo "yes,文件或目录存在且为可读" || echo "no,目录或者文件不存在或者不可读" yes,文件或目录存在且为可读 1.5、-w -w判断符用来判断文件或目录是否存在且为可写,比如: bash [jodan@master1 ~]$ll /tmp/haha.txt ----------. 1 root root 0 Mar 29 15:08 /tmp/haha.txt [jodan@master1 ~]$[ -w /tmp/haha.txt ] && echo "yes,文件或目录存在且为可写" || echo "no,目录或者文件不存在或者不可写" no,目录或者文件不存在或者不可写 1.5、-x -x判断文件或目录是否存在且有可执行权限,比如: bash [jodan@master1 ~]$ll /tmp/haha.txt ----------. 1 root root 0 Mar 29 15:08 /tmp/haha.txt [jodan@master1 ~]$[ -x /tmp/haha.txt ] && echo "yes,文件或目录存在且有执行权限" || echo "no,目录或者文件不存在或者没有执行权限" no,目录或者文件不存在或者没有执行权限 1.6、-s -s判断文件是否存在且大小大于0,比如: bash [jodan@master1 ~]$[ -s /tmp/haha.txt ] && echo "yes,文件存在且大小不为0" || echo "no,文件不存在或者大小为0" no,文件不存在或者大小为0 [jodan@master1 ~]$echo "1111" > /tmp/haha.txt [jodan@master1 ~]$[ -s /tmp/haha.txt ] && echo "yes,文件存在且大小不为0" || echo "no,文件不存在或者大小为0" yes,文件存在且大小不为0 -s是用来判断文件大小的,只有文件大小不为0,才为真。我们刚开始创建的/tmp/haha.txt只是一个空文件而已。

    04

    Day2 Linux登录和操作(个人理解简易版)

    ``` R pwd #显示当前路径 bio02@ecm-cefa:~$ pwd /home/bio02 mkdir #创建目录 bio02@ecm-cefa:~$ mkdir hello ls #显示列表 bio02@ecm-cefa:~$ ls biosoft hello project src tmp #除了四个已有目录,新增一个hello目录 rm #删文件 rmdir #删空目录 rm -r #删非空目录(删除统一展示了,如下,因为不会随意切换目录,导致删除要挨个进入目录删) bio02@ecm-cefa:~$ rm -r tmp bio02@ecm-cefa:~$ mkdir tmp bio02@ecm-cefa:~$ cd tmp bio02@ecm-cefa:~/tmp$ mkdir rm_test bio02@ecm-cefa:~/tmp$ cd rm_test bio02@ecm-cefa:~/tmp/rm_test$ mkdir huahua bio02@ecm-cefa:~/tmp/rm_test$ cd huahua bio02@ecm-cefa:~/tmp/rm_test/huahua$ touch doodle.txt bio02@ecm-cefa:~/tmp/rm_test/huahua$ rm doodle.txt bio02@ecm-cefa:~/tmp/rm_test/huahua$ cd bio02@ecm-cefa:~$ cd tmp bio02@ecm-cefa:~/tmp$ cd rm_test bio02@ecm-cefa:~/tmp/rm_test$ rmdir huahua bio02@ecm-cefa:~/tmp/rm_test$ cd bio02@ecm-cefa:~$ cd tmp bio02@ecm-cefa:~/tmp$ rmdir rm_test bio02@ecm-cefa:~/tmp$ cd #进入目录 bio02@ecm-cefa:~$ cd tmp vi #建脚本或文档 bio02@ecm-cefa:~/tmp/new$ vi hello_world.txt cat #查看文档并展示到屏幕 bio02@ecm-cefa:~/tmp/new$ cat hello_world.txt i today is a good day,i meet my boyfriend,he is older than me,he finished his work then meet me.we have a good time. head #输出前十行(然而我只发挥了一行) bio02@ecm-cefa:~/tmp/new$ head hello_world.txt i today is a good day,i meet my boyfriend,he is older than me,he finished his work then meet me.we have a good time. tail #输出后十行(如上括号所言) bio02@ecm-cefa:~/tmp/new$ tail hello_world.txt i today is a good day,i meet my boyfriend,he is older than me,he finished his work then meet me.we have a good time. head -n #数字 自定义输出几行 bio02@ecm-cefa:~/tmp/new$ head -n 2 hello_world.txt i today is a good day,i meet my boyfriend,he is older than me,he finished his work then meet me.we have a good time. cp #复制 bio02@ecm-cefa:~/tmp/new$ cp hello_world.txt hello_boy mv #移动 bio02@ecm-cefa:~/tmp/new$ mv hello_world.txt tmp bio02@ecm-cefa:~/tmp/new$ ```

    02
    领券