简体   繁体   中英

Bash menu script - Cannot execute commands

I am building a minimal standard bash menu. 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.

I found this SO question with a similar title, but not answering why commands inside the bash script are not being executed:

Cannot execute shell commands in bash script

What works and not?

Press 1: Does not change to correct cd.

Press 2: Creates the file in correct folder.

Press 3: Works.

Press 4: Works (R file is prepared with: a <- 1).

Wanted behaviour:

I need the commands inside the bash menu script, to be executed.

    #!/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.
and also in case 3 you used /tmp/./test1.sh it should be ./tmp/test1.sh or sh /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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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