Shell 脚本文件测试
Shell script file testing
我必须执行以下步骤:
一个。接受文件名作为命令行参数
b。检查是否有足够数量的命令行参数。
c。检查给定文件是否存在。
d.检查文件是否可读
e。使用语句 exit 0
成功退出程序
这是我到目前为止所做的,但是当我 运行 脚本时,我总是得到 "Invalid argument" 和 "File Does Not Exist" 作为输出。
#! /bin/bash
filename=
if [ $# -eq 1 ]
then
echo
else
echo "Invalid argument"
fi
if [ -e "" ]; then
echo "File Exists"
exit 1
else
echo "File Does not Exist"
exit 1
fi
if [ -r "" ]; then
echo "File is readable"
exit 1
else
echo "File is not readable"
exit 1
fi
好的 - 那么,您输入 bash myfilename.sh
。这意味着您正在 运行 编写您的脚本,但没有向 myfilename.sh
传递任何参数。请尝试以下操作:
echo "This is a test file" > testfile.dat
这将创建一个名为 testfile.dat
的文件,其中包含一些文本。 (文件中的实际文本无关紧要。如果您有时间,可以输入莎士比亚全集。但这不会改变您的 shell 脚本的功能)。接下来,运行 你的脚本
bash myfilename.sh testfile.dat
或者只是
myfilename.sh testfile.dat
此处您要让脚本检查 testfile.dat
.
是否存在
祝你好运。
我必须执行以下步骤:
一个。接受文件名作为命令行参数
b。检查是否有足够数量的命令行参数。
c。检查给定文件是否存在。
d.检查文件是否可读
e。使用语句 exit 0
成功退出程序这是我到目前为止所做的,但是当我 运行 脚本时,我总是得到 "Invalid argument" 和 "File Does Not Exist" 作为输出。
#! /bin/bash
filename=
if [ $# -eq 1 ]
then
echo
else
echo "Invalid argument"
fi
if [ -e "" ]; then
echo "File Exists"
exit 1
else
echo "File Does not Exist"
exit 1
fi
if [ -r "" ]; then
echo "File is readable"
exit 1
else
echo "File is not readable"
exit 1
fi
好的 - 那么,您输入 bash myfilename.sh
。这意味着您正在 运行 编写您的脚本,但没有向 myfilename.sh
传递任何参数。请尝试以下操作:
echo "This is a test file" > testfile.dat
这将创建一个名为 testfile.dat
的文件,其中包含一些文本。 (文件中的实际文本无关紧要。如果您有时间,可以输入莎士比亚全集。但这不会改变您的 shell 脚本的功能)。接下来,运行 你的脚本
bash myfilename.sh testfile.dat
或者只是
myfilename.sh testfile.dat
此处您要让脚本检查 testfile.dat
.
祝你好运。