【问题标题】:Ubuntu Bash Menu Script with Arguments带参数的 Ubuntu Bash 菜单脚本
【发布时间】:2020-11-21 03:09:41
【问题描述】:

我必须编写一个可以运行其他脚本的 bash 菜单脚本,但我不能为脚本传递参数,我该怎么办?那是我的代码

 #!/bin/bash
# Bash Menu Script Example

PS3='Please enter your choice: '
options=("Option 1" "Option 2" "Option 3" "Quit")
select opt in "${options[@]}"
do
    case $opt in
        "Option 1")
            ./script1.sh
            ;;
        "Option 2")
            ./script2.sh
            ;;
        "Option 3")
            ./script3.sh
            ;;
        "Quit")
            break
            ;;
        *) echo "invalid option $REPLY";;
    esac
done

如何为 script1.sh、script2.sh 和 script3.sh 传递参数

【问题讨论】:

    标签: bash shell ubuntu


    【解决方案1】:

    您可以在 case 语句中调用脚本时发送参数。例如,使用您的代码:

    #!/bin/bash                                      
    # Bash Menu Script Example                       
                                                     
    PS3='Please enter your choice: '                 
    options=("Option 1" "Option 2" "Option 3" "Quit")
    select opt in "${options[@]}"                    
    do                                               
        case $opt in                                 
            "Option 1")                              
                ./script1.sh HelloWorld              
                ;;                                   
            "Option 2")                              
                ./script2.sh                         
                ;;                                   
            "Option 3")                              
                ./script3.sh                         
                ;;                                   
            "Quit")                                  
                break                                
                ;;                                   
            *) echo "invalid option $REPLY";;        
        esac                                         
    done   
    

    ./script1 看起来像这样:

    #!/bin/bash
    
    echo $1
                
    

    这是使用您的代码并选择第一个选项的结果。

    [20:06:18][/] sh test.sh
    1) Option 1
    2) Option 2
    3) Option 3
    4) Quit
    Please enter your choice: 1
    HelloWorld
                      
    

    【讨论】:

      猜你喜欢
      • 2012-05-21
      • 1970-01-01
      • 2014-06-05
      • 2019-02-08
      • 1970-01-01
      • 2017-11-29
      • 1970-01-01
      • 2021-03-05
      相关资源
      最近更新 更多