这是我的代码:
#!/bin/bash
if [ "$#" -ne 2 ] ; then
echo "$0: exactly 2 arguments expected"
exit 3
fi
if [$1 != "file" -a $1 != 'dir'] ; then
echo "$0: first argument must be string "file" or "dir""
exit 1
elif [-e $2 -a -r $2]; then
if ["$1" = "file" -a -f $2] ; then
echo YES
elif ["$1" = "dir" -a -d $2] ; then
echo YES
else
echo NO
fi
exit 0
else
echo "$0: $2 is not a readable entry"
exit 2
fi
如果我运行./lab4文件filename1,它将检查第一个参数是字符串“file”还是“dir”,如果第一个参数是“file”,filename1是一个文件,它将打印yes。相同的东西对于目录。
它不识别$ 1和$ 2。代码将输出:
./lab04Q2: line 7: [file: command not found
./lab04Q2: line 10: [-e: command not found
即使我在运行程序时放置了2个参数。