免费注册 查看新帖 |

Chinaunix

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

SHELL中究竟该怎么检测文件是否存在 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-11-04 12:25 |只看该作者 |倒序浏览
偶要检测该目录下文件是否存在,代码如下:
if [ -z "$FILENAME0" ]
then
   echo "input in_file name is NULL"
   exit;
elif [ !-f "$FILENAME0" ]
then
   echo "$FILENAME0 is not exist!\nPlease check it."
fi
[ -z "$FILENAME0" ]是检测输入文件名是否为空;
[ !-f "$FILENAME0" ]是检测文件是否存在,如果不存在,显示"$FILENAME0 is not exist!\nPlease check it."
为什么执行后显示提示信息如下:
+ [ -z ITEST.csv ]
+ [ !-f ITEST.csv ]
trfer: test: argument expected
为什么,错在哪里

论坛徽章:
0
2 [报告]
发表于 2004-11-04 12:28 |只看该作者

SHELL中究竟该怎么检测文件是否存在

elif [ !-f "$FILENAME0" ]
改为 elif [ ! -f "$FILENAME0" ] ,!与-f间加一个空格

论坛徽章:
0
3 [报告]
发表于 2004-11-04 18:01 |只看该作者

SHELL中究竟该怎么检测文件是否存在

[quote]原帖由 "nero_8888"]FILENAME0" ] ,!与-f间加一个空格[/quote 发表:

谢谢
但[ !-f "$FILENAME0" ]
到底需不需要加“”把变量包含啊

论坛徽章:
0
4 [报告]
发表于 2004-11-05 08:29 |只看该作者

SHELL中究竟该怎么检测文件是否存在

加了肯定没错,好象也可不加,但有时在判断时不加“”会有问题,我以前写shell时就碰到过。

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
5 [报告]
发表于 2004-11-05 10:59 |只看该作者

SHELL中究竟该怎么检测文件是否存在

推荐用[[]]代替[],
关于shell对文件的判断,请参阅:
http://blog.chinaunix.net/index.php?op=ViewArticle&articleId=571&blogId=126
中的详细解释

论坛徽章:
0
6 [报告]
发表于 2004-11-05 17:40 |只看该作者

SHELL中究竟该怎么检测文件是否存在

原帖由 "寂寞烈火" 发表:
于shell对文件的判断,请参阅:
http://blog.chinaunix.net/index.php?op=ViewArticle&articleId=571&blogId=126
中的详细解释

究竟该 [ -f [ filename ] ] ,还是该 [ [ -f filename ] ] 呢?

论坛徽章:
0
7 [报告]
发表于 2004-11-05 18:58 |只看该作者

SHELL中究竟该怎么检测文件是否存在

-b file
file exists and is a block device file

-c file
file exists and is a character device file

-d file
file exists and is a directory

-e file
file exists

-f file
file exists and is a regular file

-g file
file exists and has its setgid bit set

-G file
file exists and is owned by the effective group ID

-k file
file exists and has its sticky bit set

-L file
file exists and is a symbolic link

-O file
file exists and is owned by the effective user ID

-p file
file exists and is a pipe or named pipe (FIFO file)

-r file
file exists and is readable

-s file
file exists and is not empty

-S file
file exists and is a socket

-t N
File descriptor N points to a terminal

-u file
file exists and has its setuid bit set

-w file
file exists and is writeable

-x file
file exists and is executable, or file is a directory that can be searched
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP