shell编程基础(2)---&&与||
2024-08-24 12:34:25
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
版权声明:本文为博主原创文章,未经博主允许不得转载。
最新文章
- 关于js中的this
- SharePoint 2013 为用户组自定义EventReceiver
- iOS之UI--CAShapeLayer
- UVa1401 Remember the Word(DP+Trie树)
- Web前端开发面试题
- 对apply和call的理解
- 抓包分析TCP的三次握手和四次分手
- (转载)PHP 判断常量,变量和函数是否存在
- c - 输出 101 至 200之间的素数.
- tftp常用命令
- URL Scheme与openURL
- MySQL编程基础
- 信息安全技能树(SecWiki中Web安全工程师职位建议)
- C#:CeF遇到的问题
- Matlab-11:Gausssidel迭代法工具箱
- shiro 会话管理
- 1.3.2、CDH 搭建Hadoop在安装之前(端口---Cloudera Navigator加密使用的端口)
- XenServer DVSC
- js和css实现内容超过边框,就自动省略,自动添加title
- Android网络开发之HttpURLConnection