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

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

  • 相关阅读:
    Just Do It:战胜拖延症(终极大法!)
    拖延是一种什么病
    数学之美系列二十三 谈谈香农第一定律
    深度挖掘
    当机器统治世界
    算法是如何秘密左右我们的行为方式
    比人类更强:为什么机器人终将也必将接手我们的工作?
    Google移动的未来在哪里?Google Now
    数学之美系列十九 马尔可夫链的扩展 贝叶斯网络 (Bayesian Networks)
    Matlab与COM应用(一)——COM技术概述
  • 原文地址:https://www.cnblogs.com/ironstark/p/4892642.html
Copyright © 2011-2022 走看看