加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 检索第一个非选项命令行参数

发布时间:2020-12-15 22:16:37 所属栏目:安全 来源:网络整理
导读:我正在尝试编写一个包装器 shell脚本,每次调用命令时都会缓存信息.它只需要存储第一个非选项参数.例如,在 $mycommand -o option1 -f another --spec more arg1 arg2 我想要检索“arg1”. 怎么能在bash中完成? 解决方法 使用 getopt可能是要走的路. 如果你想
我正在尝试编写一个包装器 shell脚本,每次调用命令时都会缓存信息.它只需要存储第一个非选项参数.例如,在

$mycommand -o option1 -f another --spec more arg1 arg2

我想要检索“arg1”.

怎么能在bash中完成?

解决方法

使用 getopt可能是要走的路.

如果你想在bash中看到参数扫描代码,那么非getopt方式??是:

realargs="$@"
while [ $# -gt 0 ]; do
    case "$1" in
      -x | -y | -z)
        echo recognized one argument option $1 with arg $2
        shift
        ;;
      -a | -b | -c)
        echo recognized zero argument option $1,no extra shift
        ;;
      *)
        saveme=$1
        break 2
        ;;
    esac
    shift
done
set -- $realargs
echo saved word: $saveme
echo run real command: "$@"

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读