linux - 为命令 Linux shell 脚本创建参数

标签 linux shell

您好,我正在尝试为我的 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/

相关文章:

linux - 如何将 bash 脚本作为命令运行?

Linux - Bash - 在许多用户目录中应用 chown

linux - 使用 sed 从日志文件中提取目录

c - C 中子进程之间的 exec() 和 pipe()

linux - 使用curl解析时区

arrays - 正则表达式匹配给定数据中的最后一个值

bash - 为什么 shell 变量在给命令加上前缀时会变成环境变量?

linux - 在 Visual Studio for Linux 项目上从 jenkins 运行 devenv 时未定义的远程主机

regex - 使用正则表达式从行中提取子字符串并删除具有重复子字符串的行

linux - 解释 shell 脚本代码 "for loop"