zoukankan      html  css  js  c++  java
  • shell编程基础(2)---&&与||

    shell 编程重要的应用就是管理系统,对于管理系统中成千上万的程序而言,查询某个文件名是否存在,并且获取该文件名所指代文件基本信息是系统管理员的基本任务。shell命令可以很轻松的完成这项任务。

    #program this is a example for ################
    #########     command test     ################
    read -p "type in the filename:        " filename  
    test -z $filename &&echo "you must put in a legal name"&&exit 0

    //连续的&&表示命令是顺序执行的,前一个执行成功才能执行后一个中间有任何一个环节错误,则返回报错信息

    test ! -e $filename &&echo "not exist" &&exit 0
    test -e $filename &&echo "exit"

    这里想实现实现的功能是,如果文件不存在则退出script,本来想写作

    test -e $filename&&echo "exit"||echo"not exit"&&exit 0

    但是发现这样无论filename是否存在,程序都会在这里退出
    因为如果文件存在,则test传回一个0值,||判断后执行echo "exit",然后又传回一个0值,&&判断后执行exit 0
    如果test 传回一个非0值,则||判断后执行echo "not exit",传回0值,&&判断后还是执行exit 0;
    如果写成

    test -e $filename ||echo "not exit"&&echo "exit"

    如果存在,test返回非0,执行echo "exit"
    如果不存在,则test返回0,执行echo "not exit"
    echo "not exit" 又返回0,再执行echo "exit"
    所以没办法在一条语句中判断并推出。

    test -f $filename &&filetype="file"
    test -d $filename &&filetype="dictory"
    test -r $filename &&perm=" readable"
    test -w $filename &&perm=${perm}" writable"
    test -x $filename &&perm=${perm}" execuable"

    /perm=${变量}”__”表示在变量后补充___

    echo "file type is $filetype and the mod is $perm"
    exit 2

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    获得ip地理信息的几种方法
    设置 phoneGap/Cordova 3.4 应用程序启动动画闪屏 SplashScreen
    backbone实例01
    即使世界明天毁灭,我今天也要种下葡萄树
    真言真语
    美文美句
    25个实用的jQuery技巧和解决方案
    突袭HTML5之WebGL 3D概述
    敏捷开发之Scrum扫盲篇
    javascript中undefined和null的区别详解
  • 原文地址:https://www.cnblogs.com/ironstark/p/4892642.html
Copyright © 2011-2022 走看看