存在文件的ksh脚本

时间:2012-11-30 09:51:39

标签: windows cygwin ksh

总是我得到“源文件不可读或存在,请检查文件”以获取以下代码。我在我的windows机器上使用cygwin来运行脚本。即使文件存在于该位置,我也收到相同的消息。如何获得有关文件无法读取的详细信息。

#!/bin/ksh
#
# Scanning source file for existance and readable
file_loc="abc.xml"
if [ -f "$file_loc" -a -r "$file_loc"]
then
    print "Source file read.\n"
else
    print "Source file not readable or exists, please check the file $file_loc.\n"
fi

2 个答案:

答案 0 :(得分:1)

需要结束括号前的空格:

if [ -f "$file_loc" -a -r "$file_loc" ]

答案 1 :(得分:0)

file_loc="abc.xml" && (< ${file_loc:?})

是一种替代检查。一个优点是它为不同的错误返回不同的消息;我们让shell完成所有工作。

这是如何构建的?这一行可以触发至少三个不同的消息:

${ varname :?可选消息}语法将检查未定义的变量, 这可以用变量名称表示拼写错误。

(< ${file_lo:?} )

-bash: file_lo: parameter null or not set

(< filename )只会尝试打开和关闭文件。

$ (< nonesuch ) && echo ok

-bash: nonesuch: No such file or directory

$ (< /etc/shadow ) && echo ok

-bash: /etc/shadow: Permission denied

相关问题