您好,我正在尝试为我的 shell 脚本创建参数,但遇到了问题。
例如,假设文件名为 test。
当我调用 ./test -parameter1 input_file.txt
我收到一条错误消息,提示“没有这样的文件或目录”。
这是我的代码示例。
#!/bin/sh
if [ "$1" == "parameter" ]
then
while read line
do
#echo "$line"
done <$1
else
echo "Not working"
fi
我的总体目标是逐行读取我正在处理的数字文件,然后计算行或列的平均值。这就是为什么我要尝试创建参数,以便用户必须指定 ./test -rows input_file.txt
或 ./test -columns input_file.txt
最佳答案
您正在使用字符串 -parameter
作为输入文件名。也许你想要:
#!/bin/sh
if [ "$1" = "-parameter" ]
then
while read line
do
#echo "$line"
done <$2 # Use $2 instead of $1 here. Or use shift
else
echo "Not working" >&2
fi
关于linux - 为命令 Linux shell 脚本创建参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29373835/