script that reads all the scripts in the directory and run them within that script

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

Consider using select builtin in the ksh

Don't use select; it doesn't offer anything that cannot be done better with other shell commands.

print_menu()
{
  n=1
  bar======================================
  printf "%s\n" "$bar" " Please Select a Script"  "$bar"

  for item
  do
    printf "%3d: %s\n" "$n" "$item"
    n=$(( $n + 1 ))
  done

  printf "%3d: %s\n" "$n" "Quit"

  printf "%s\n Select 1-%d: " "$bar" "$n"
}

while :
do
  print_menu *.sh
  read x
  case $x in
    [1-5]) script$x.sh ;;
    $n) exit  ;;
  esac
done

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

I think it should work for the time being...

Ive checked both scripts but they are nor working :frowning:
I am using "#!/bin/sh" to run the script but it gives me an error.

Can you please help me fix this?

This is the eerror that I get:

=====================================
 Please Select a Script
=====================================
  1: script1.sh
  2: script2.sh
  3: script3.sh
  4: script4.sh
  5: script5.sh
  6: Quit
=====================================
 Select 1-6: 2
./script5.sh: 27: script2.sh: not found
=====================================
 Please Select a Script
=====================================
  1: script1.sh
  2: script2.sh
  3: script3.sh
  4: script4.sh
  5: script5.sh
  6: Quit
=====================================
 Select 1-6: 

-----Post Update-----

I thing I allready know what the problem is ...
I am using "#!/bin/sh to run this script ... how do I make it work under shell scripting?

You must call ./script2.sh, not script2.sh.

its still not working...

This is what I get:

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: 

The script:

[b]When you post code, please wrap it in

 tags.


#!/bin/sh
 # ....
if [[ $item != $prog ]]
  # ...

[/quote]

[indent]

if [ "$item" != "$prog" ]