简体   繁体   English

Bash菜单脚本-无法执行命令

[英]Bash menu script - Cannot execute commands

I am building a minimal standard bash menu. 我正在建立一个最小的标准bash菜单。 The selections works fine but commands are not being executed. 选择工作正常,但命令未执行。 I suspect I might to wrap in the command, but not sure how that is being done. 我怀疑我可能会包装命令,但是不确定如何完成。 I will often have 2 commands sent in, thus the commands needs to be separated in a way that bash understand it as line of commands. 我经常会收到2条命令,因此需要以bash将其理解为命令行的方式来分隔命令。

I found this SO question with a similar title, but not answering why commands inside the bash script are not being executed: 我发现了这样的标题类似的问题,但没有回答为什么未执行bash脚本中的命令:

Cannot execute shell commands in bash script 无法在bash脚本中执行shell命令

What works and not? 什么有效,什么无效?

Press 1: Does not change to correct cd. 按1:不更改以更正cd。

Press 2: Creates the file in correct folder. 按2:在正确的文件夹中创建文件。

Press 3: Works. 按3:工作。

Press 4: Works (R file is prepared with: a <- 1). 按4:Works(R文件准备为:a <-1)。

Wanted behaviour: 想要的行为:

I need the commands inside the bash menu script, to be executed. 我需要执行bash菜单脚本中的命令。

    #!/bin/bash


# -----
# Menu:
# -----

while :
do
echo "Menu"

echo "1 - Change directory to /tmp"
echo "2 - Create file test1.sh with hello message, in /tmp"
echo "3 - Execute test1.sh"
echo "4 - Execute R-script [/tmp/test2.R]"
echo "Exit - any kind but not [1-4]"

read answer;

case $answer in
    1)
    echo "Change directory to [\tmp]"
    cd /tmp # Command to be executed.
    break
    ;;

    2)
    echo "Create file [test1.sh] in [\tmp]"
    # Commands to be executed.
    cd /tmp
    touch test1.sh
    chmod +x test1.sh
    echo echo "hello" > test1.sh
    break
    ;;
    3)
    echo "Execute file [test1.sh]"
    /tmp/./test1.sh # Command to be executed.
    break
    ;;
    4)
    echo "Execute R-script [/tmp/test2.R]"
     cd /tmp && /usr/bin/Rscript test2.R # Command to be executed.
    break
    ;;

    *)
    # Command goes here
    echo "Exit"
    break
    ;;
esac
done

you should not use break when you want to execute all cases. 要执行所有情况时,请勿使用break。
and also in case 3 you used /tmp/./test1.sh it should be ./tmp/test1.sh or sh /tmp/test1.sh 并且在情况3中,您使用的是/tmp/./test1.sh它应该是./tmp/test1.shsh /tmp/test1.sh
your code should be: 您的代码应为:

#!/bin/bash

# -----
# Menu:
# -----

while :
do
    echo "Menu"

    echo "1 - Change directory to /tmp"
    echo "2 - Create file test1.sh in /tmp"
    echo "3 - Execute test1.sh"
    echo "4 - Execute R-script [/tmp/test2.R]"
    echo "Exit - any kind but not [1-4]"

    read answer;

    case $answer in
        1)
            echo "Change directory to [\tmp]"
            cd /tmp # Command to be executed.
            pwd

        ;;

        2)
            echo "Create file [test1.sh] in [\tmp]"
            touch /tmp/test1.sh # Command to be executed.

        ;;
        3)
            echo "Execute file [test1.sh]"
            ./tmp/test1.sh # Command to be executed.

        ;;
        4)
            echo "Execute R-script [/tmp/test2.R]"
            /usr/bin/Rscript /production/20_front_trader/build/x_run_front_trader.R # Command to be executed.

        ;;

        *)
            # Command goes here
            echo "Exit"

        ;;
    esac
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM