请问如何给 shell 脚本传递长参数? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
Moris
V2EX    Linux

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

  •  
  •   Moris Aug 24, 2022 3132 views
    This topic created in 1346 days ago, the information mentioned may be changed or developed.

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

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

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

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

    Supplement 1    Aug 24, 2022

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

    #!/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 
    Supplement 2    Aug 24, 2022
    代码复制错了,应该是这个
    ```
    #!/bin/bash
    ARGS= `getopt -a -o ah --long install,help -- "$@"`
    if [ $? != 0 ]; then
    echo "Terminating..."
    exit 1
    f
    eval set -- "${ARGS}"
    while true
    do
    case "$1" in
    -i|--install)
    ./install.sh;;
    -h|--help)
    ./help.sh;;
    \?)
    echo "no args"
    exit 1;;
    esac
    done
    ```
    Supplement 3    Aug 24, 2022

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

    #!/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 
    Supplement 4    Aug 24, 2022
    代码复制错了,应该是这个
    ```
    #!/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

    ```
    Supplement 5    Aug 24, 2022

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

    #!/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 replies    2022-08-24 18:03:36 +08:00
    AirCrusher
        1
    AirCrusher  
       Aug 24, 2022
    getopt
    vtwoextb
        2
    vtwoextb  
       Aug 24, 2022
    getopts
    circle33
        3
    circle33  
       Aug 24, 2022
    获取命令行参数再执行对应的逻辑
    $1 可以获取第一个命令行参数,分支选择可以用 case xxx in xxx
    Moris
        4
    Moris  
    OP
       Aug 24, 2022
    @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
       Aug 24, 2022
    @vtwoextb 请问能具体说一下吗?我在网上找到的 getopt 教程都没有涉及长参数的
    Moris
        6
    Moris  
    OP
       Aug 24, 2022
    @AirCrusher 请问能具体说一下吗?
    arch9999
        7
    arch9999  
       Aug 24, 2022 via iPhone
    打开 acme.sh

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

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

    navi 了解一下?



    可以定制任何命令



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

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

    不客气,有一说一,你好有礼貌 :)
    Moris
        19
    Moris  
    OP
       Aug 24, 2022
    @kaiger:)
    About     Help     Advertise     Blog     API     FAQ     Solana     2326 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 56ms UTC 04:32 PVG 12:32 LAX 21:32 JFK 00:32
    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