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

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

最新文章

  1. 关于js中的this
  2. SharePoint 2013 为用户组自定义EventReceiver
  3. iOS之UI--CAShapeLayer
  4. UVa1401 Remember the Word(DP+Trie树)
  5. Web前端开发面试题
  6. 对apply和call的理解
  7. 抓包分析TCP的三次握手和四次分手
  8. (转载)PHP 判断常量,变量和函数是否存在
  9. c - 输出 101 至 200之间的素数.
  10. tftp常用命令
  11. URL Scheme与openURL
  12. MySQL编程基础
  13. 信息安全技能树(SecWiki中Web安全工程师职位建议)
  14. C#:CeF遇到的问题
  15. Matlab-11:Gausssidel迭代法工具箱
  16. shiro 会话管理
  17. 1.3.2、CDH 搭建Hadoop在安装之前(端口---Cloudera Navigator加密使用的端口)
  18. XenServer DVSC
  19. js和css实现内容超过边框,就自动省略,自动添加title
  20. Android网络开发之HttpURLConnection

热门文章

  1. XSS获取cookie
  2. 单例模式(Singleton)的6种实现
  3. Java HTML页面抓取实例
  4. iOS网络编程同步GET方法请求编程
  5. IIS本地服务器,设置IP地址问题
  6. php字符串函数和数组函数
  7. android 设置叠加父级响应点击事件
  8. 剑指offer--面试题6
  9. A const field of a reference type other than string can only be initialized with null Error [duplicate]
  10. linux源码阅读笔记 fork函数