请问如何给 shell 脚本传递长参数? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
Moris
V2EX    Linux

请问如何给 shell 脚本传递长参数?

  •  
  •   Moris 2022-08-24 16:24:34 +08:00 2926 次点击
    这是一个创建于 1214 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我有一个多功能脚本 test.sh

    我想实现输入./test.sh --help 时输出帮助内容

    输入./test.sh --install 时执行安装程序,

    请教一下,shell 脚本对应代码该如何实现?

    第 1 条附言    2022-08-24 17:05:04 +08:00

    以下是我参考网上的教程写的部分代码,但是不能实现功能,请教一下问题出在哪?

    #!/bin/bash cd /mnt/c/Data/program/library/auto_software/backup_program ARGS= `getopt -a -o aswl --long all,server,windows,local -- "$@"` if [ $? != 0 ]; then echo "Terminating..." exit 1 fi eval set -- "${ARGS}" while true do case "$1" in -a|--all) bash locsyc_r bash sbak_r bash wbak_r;; -s|--server) bash sbak_r;; -w|--windows) bash wbak_r;; -l|--local) bash locsyc_r;; \?) echo "no args" exit 1;; esac done 
    第 2 条附言    2022-08-24 17:06:05 +08:00
    代码复制错了,应该是这个
    ```
    #!/bin/bash
    ARGS= `getopt -a -o ah --long install,help -- "$@"`
    if [ $? != 0 ]; then
    echo "Terminating..."
    exit 1
    fi
    eval set -- "${ARGS}"
    while true
    do
    case "$1" in
    -i|--install)
    ./install.sh;;
    -h|--help)
    ./help.sh;;
    \?)
    echo "no args"
    exit 1;;
    esac
    done
    ```
    第 3 条附言    2022-08-24 17:06:39 +08:00

    代码复制错了,应该是这个

    #!/bin/bash ARGS= `getopt -a -o ah --long install,help -- "$@"` if [ $? != 0 ]; then echo "Terminating..." exit 1 fi eval set -- "${ARGS}" while true do case "$1" in -i|--install) ./install.sh;; -h|--help) ./help.sh;; \?) echo "no args" exit 1;; esac done 
    第 4 条附言    2022-08-24 17:07:08 +08:00
    代码复制错了,应该是这个
    ```
    #!/bin/bash
    ARGS= `getopt -a -o ah --long install,help -- "$@"`
    if [ $? != 0 ]; then
    echo "Terminating..."
    exit 1
    fi
    eval set -- "${ARGS}"
    while true
    do
    case "$1" in
    -i|--install)
    ./install.sh;;
    -h|--help)
    ./help.sh;;
    \?)
    echo "no args"
    exit 1;;
    esac
    done

    ```
    第 5 条附言    2022-08-24 17:07:47 +08:00

    代码复制错了,应该是这个

    #!/bin/bash ARGS= `getopt -a -o ah --long install,help -- "$@"` if [ $? != 0 ]; then echo "Terminating..." exit 1 fi eval set -- "${ARGS}" while true do case "$1" in -i|--install) ./install.sh;; -h|--help) ./help.sh;; \?) echo "no args" exit 1;; esac done 
    19 条回复    2022-08-24 18:03:36 +08:00
    AirCrusher
        1
    AirCrusher  
       2022-08-24 16:29:34 +08:00
    getopt
    vtwoextb
        2
    vtwoextb  
       2022-08-24 16:35:26 +08:00
    getopts
    circle33
        3
    circle33  
       2022-08-24 16:43:08 +08:00
    获取命令行参数再执行对应的逻辑
    $1 可以获取第一个命令行参数,分支选择可以用 case xxx in xxx
    Moris
        4
    Moris  
    OP
       2022-08-24 16:48:39 +08:00
    @circle33 我写了一些代码如下。但是无法实现功能执行,我不是很懂 getopt

    #!/bin/bash
    ARGS= `getopt -a -o ah --long install,help -- "$@"`
    if [ $? != 0 ]; then
    echo "Terminating..."
    exit 1
    fi
    eval set -- "${ARGS}"
    while true
    do
    case "$1" in
    -i|--install)
    ./install.sh;;
    -h|--help)
    ./help.sh;;
    \?)
    echo "no args"
    exit 1;;
    esac
    done
    Moris
        5
    Moris  
    OP
       2022-08-24 16:53:34 +08:00
    @vtwoextb 请问能具体说一下吗?我在网上找到的 getopt 教程都没有涉及长参数的
    Moris
        6
    Moris  
    OP
       2022-08-24 16:54:00 +08:00
    @AirCrusher 请问能具体说一下吗?
    arch9999
        7
    arch9999  
       2022-08-24 16:58:08 +08:00 via iPhone
    打开 acme.sh

    看看别人怎么写的
    circle33
        8
    circle33  
       2022-08-24 17:17:47 +08:00
    @Moris 无法实现是指?
    Moris
        9
    Moris  
    OP
       2022-08-24 17:20:41 +08:00
    @circle33 报错,显示没有定义-i 或--install 参数
    circle33
        10
    circle33  
       2022-08-24 17:24:31 +08:00
    @Moris 去掉 ARGS= 右侧的空格呢
    Moris
        11
    Moris  
    OP
       2022-08-24 17:28:24 +08:00
    @circle33 我已经找到解决方案了,我参考了一下别人的案例,直接去掉
    ARGS= `getopt -a -o ah --long install,help -- "$@"`
    if [ $? != 0 ]; then
    echo "Terminating..."
    exit 1
    fi
    eval set -- "${ARGS}"
    再更改一下 while 的条件就行
    bearice
        12
    bearice  
       2022-08-24 17:30:22 +08:00
    觉得 getopt 不好用的话 https://github.com/fumieval/clap4shell 可以试试这个
    kaiger
        13
    kaiger  
       2022-08-24 17:33:13 +08:00
    我看你这脚本目录名字: cd /mnt/c/Data/program/library/auto_software/backup_program

    是不是想备份一些自己平时用的软件安装命令?

    navi 了解一下?



    可以定制任何命令



    语法也很简单
    Moris
        14
    Moris  
    OP
       2022-08-24 17:38:29 +08:00
    @kaiger 差不多是,我现在想要写的脚本就是参考之前的备份脚本,在上面修改。之前的备份脚本传入短参数-a -b 之类的是可以的,所以这次也想顺便修改一下备份的脚本。谢谢你的建议,我会看看的。不过,你这个终端时远程终端吗?叫啥名字?好酷啊!
    Moris
        15
    Moris  
    OP
       2022-08-24 17:39:22 +08:00
    @bearice 好的,谢谢
    kaiger
        16
    kaiger  
       2022-08-24 17:46:36 +08:00
    @Moris #14

    终端名字叫:Urxvt
    Shell: zsh + oh-my-zsh + p10k
    主题参考的: https://github.com/catppuccin/urxvt
    Moris
        17
    Moris  
    OP
       2022-08-24 17:57:38 +08:00
    @kaiger 好的,谢谢你
    kaiger
        18
    kaiger  
       2022-08-24 18:00:19 +08:00
    @Moris #17

    不客气,有一说一,你好有礼貌 :)
    Moris
        19
    Moris  
    OP
       2022-08-24 18:03:36 +08:00
    @kaiger:)
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1488 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 27ms UTC 16:26 PVG 00:26 LAX 08:26 JFK 11:26
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86