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