Hi I am trying to write a shell script that is reading all the scripts in the current directory (currently 5) and is allowing me to run the scripts that is in the directory.
I want that this scripts asks te user to execute 1 of the listed scripts.
I have 4 sample scripts in the directory:
script1.sh
script2.sh
script3.sh
script4.sh
What I want to achieve more is that:
when the user selects a script the script should be executed and return to the menu again after it finishes the script in the directory
The input of the script should be visible for the user
The user has the option to quit the script in a clean fashion
Here is my start for now... I am a litte stuck ...
#!/bin/sh
echo "Please Select a Script"
read SCRIPTSINDIR
#read the current directory and writes them to a file
ls -l -S -r > $SCRIPTSINDIR
#get the files from the directory
more $SCRIPTSINDIR
Thanks for your explanation and the script ...
I see that the script has do be calles "script" and then a number and then a number.
Is it also possible to dynamically read the directory?
lets say maybe there is a script calles "this_new_script.sh" it will list it in the menu but there is no way I can run this?
I have modified this script to tackle 3 issue which I could see.
1) same what you mentioned, calling files dynamically
2) handling wrong input
3) if the script is put in a file, lets say prog.sh, this will be listed in the menu and if an option is chosen to select it, the program will go recursive.
I have modified the script a bit, instead of writing a fresh program...
print_menu()
{
n=1
bar======================================
printf "%s\n" "$bar" " Please Select a Script" "$bar"
for item
do
if [[ $item != $prog ]]
then
printf "%3d: %s\n" "$n" "$item"
n=$(( $n + 1 ))
fi
done
echo $prog
printf "%3d: %s\n" "$n" "Quit"
printf "%s\n Select 1-%d: " "$bar" "$n"
}
prog=$0
while :
do
print_menu *.sh
read x
case $x in
[1-$(( $n - 1 ))]) sh `ls -1 *.sh | grep -v $prog | head -n $x | tail -1` ;;
$n) exit ;;
*) echo "Wrong Input\n"
esac
done
root@CISCO-LAB-PC:/home/iwan/scripts# ./script55.sh
=====================================
Please Select a Script
=====================================
./script55.sh: 33: [[: not found
./script55.sh: 33: [[: not found
./script55.sh: 33: [[: not found
./script55.sh: 33: [[: not found
./script55.sh: 33: [[: not found
./script55.sh: 33: [[: not found
./script55.sh
1: Quit
=====================================
Select 1-1: