免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 4222 | 回复: 24
打印 上一主题 下一主题

这个要怎么写? [复制链接]

论坛徽章:
1
巨蟹座
日期:2014-06-04 13:33:30
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-01-16 22:47 |只看该作者 |倒序浏览
read input
if [ $input != $(ls -l $input|grep '^d') ]
echo "failed"

if这行是想判断输入的文件名,如果不是文件夹就echo failed

但是怎么让if 里面的执行对比,而不是直接执行命令呢?

如果read input我输入abc (假如abc这个文件存在)

运行后就会出现
: abc  command not found

shell 把input给执行进去了

论坛徽章:
0
2 [报告]
发表于 2009-01-16 22:51 |只看该作者
test不是有检查文件的参数吗?
为什么要这么麻烦?

论坛徽章:
1
巨蟹座
日期:2014-06-04 13:33:30
3 [报告]
发表于 2009-01-16 22:52 |只看该作者
haimming 请直言,在下新手,怕是不能理解您的意思,洗耳恭听哦
在线等

论坛徽章:
0
4 [报告]
发表于 2009-01-16 22:56 |只看该作者
man test
-d选项

论坛徽章:
0
5 [报告]
发表于 2009-01-16 22:57 |只看该作者
-a file
file exists

-d file
file exists and is a directory

-e file
file exists; same as -a

-f file
file exists and is a regular file (i.e., not a directory or other special type of file)

-r file
You have read permission on file

-s file
file exists and is not empty

-w file
You have write permission on file

-x file
You have execute permission on file, or directory search permission if it is a directory

-N file
file was modified since it was last read

-O file
You own file

-G file
file's group ID matches yours (or one of yours, if you are in multiple groups)

file1 -nt file2
file1 is newer than file2 [6]

file1 -ot file2
file1 is older than file2

论坛徽章:
0
6 [报告]
发表于 2009-01-16 22:58 |只看该作者

  1. [rockfall@localhost atmp]$ ll
  2. total 8
  3. -rw-rw-r-- 1 rockfall rockfall    2 2009-01-15 23:03 a
  4. drwxrwxr-x 2 rockfall rockfall 4096 2009-01-16 22:56 b
  5. [rockfall@localhost atmp]$ a=a
  6. [rockfall@localhost atmp]$ if [ -d $a ] ;then echo y ;else echo n;fi
  7. n
  8. [rockfall@localhost atmp]$ a=b
  9. [rockfall@localhost atmp]$ if [ -d $a ] ;then echo y ;else echo n;fi
  10. y
复制代码

论坛徽章:
1
巨蟹座
日期:2014-06-04 13:33:30
7 [报告]
发表于 2009-01-16 23:05 |只看该作者
hi haimming
我想问你,如果输入的是1个文件名,比如aaa
但是aaa不是我要的文件,如何判断?
例如命令行里

ls -ahl 文件名|grep '^a'

文件名不匹配,不是以a开头的 那这样执行命令,在新的一行就是空的

论坛徽章:
0
8 [报告]
发表于 2009-01-16 23:07 |只看该作者
read input
if [ ! -d $input ]
then
        echo "failed"
fi

if [ ! -e $input ]
then
        echo "abc  command not found"
fi

论坛徽章:
1
巨蟹座
日期:2014-06-04 13:33:30
9 [报告]
发表于 2009-01-16 23:08 |只看该作者
谢谢楼上的解答,我没看到,谢谢


那我说的那样怎么解决呢?

论坛徽章:
1
巨蟹座
日期:2014-06-04 13:33:30
10 [报告]
发表于 2009-01-16 23:10 |只看该作者
原帖由 todayhero 于 2009-1-16 23:07 发表
read input
if [ ! -d $input ]
then
        echo "failed"
fi

if [ ! -e $input ]
then
        echo "abc  command not found"
fi



-e FILE
              FILE exists

如果文件存在,echo "abc  command not found"  ??
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP