V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
Moris
V2EX  ›  Linux

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

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

    我有一个多功能脚本 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   ·   我们的愿景   ·   实用小工具   ·   1093 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 22:58 · PVG 06:58 · LAX 15:58 · JFK 18:58
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.