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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script that reads all the scripts in the directory and run them within that script
# 1  
Old 06-09-2009
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 ...

Code:
#!/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

# 2  
Old 06-09-2009
Consider using select builtin in the ksh
# 3  
Old 06-09-2009

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

Code:
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

# 4  
Old 06-10-2009
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?
# 5  
Old 06-10-2009
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...

Code:
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...
# 6  
Old 06-12-2009
Ive checked both scripts but they are nor working :-(
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:

Code:
=====================================
 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?

Last edited by I-1; 06-12-2009 at 05:51 AM..
# 7  
Old 06-12-2009
Quote:
Originally Posted by I-1
./script5.sh: 27: script2.sh: not found

You must call ./script2.sh, not script2.sh.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

A single script to run multiple scripts

Hi , Can someone help! I need a shell script to run multiple scripts by using single shell script, incase any one of the scripts fails, it should get exit and after trouble shooting if we re-execute it, it should start from the failed script. I have a written a scripting till the... (1 Reply)
Discussion started by: anniesurolyn
1 Replies

2. Shell Programming and Scripting

Run 2 shell scripts simultaneously from one script

i Run 2 scripts on all of around 50 nodes every day. 1.Mod_1.sh 2.Mod_2.sh eg.. i run file with specific node no like Mod_1.sh NODE_(node number) Mod_2.sh NODE_(node number) I want to run both file by using single script with unique node number. Eg.. Mod_new.sh NODE_(node... (11 Replies)
Discussion started by: Ganesh Mankar
11 Replies

3. Shell Programming and Scripting

How to run scripts parallely inside shell script?

Hi , I have 4 scripts example script1,script2,script3,script4 . I have to run script1,script2 and script3 parallely since this 3 scripts dont have dependencies . Once script1,script2 and script3 got completed successfully , I have to trigger script4. Can someone help me on this how to... (10 Replies)
Discussion started by: vinothsekark
10 Replies

4. UNIX for Dummies Questions & Answers

Trying to make a script to run 3 other scripts in a screen.

Hello, my name is Spurkle. I'm new to linux stuff. Currently I am trying to make a script which will run three other scripts in screen. This is the code: sudo screen -S hubServ /var/servers/hub/hub.sh sudo screen -S facServh /var/servers/factions/factions.sh sudo screen -S bunServ... (5 Replies)
Discussion started by: Spurkle
5 Replies

5. Shell Programming and Scripting

Using a script to define variables and run multiple other scripts in succession.

I'm pretty new to scripting in Korn shell so please forgive me... What I'm trying to do is to create a script that calls multiple other ksh scripts and defines variables for text files. I need it to define my user defined variables (file paths, date & time stamps, etc that are currently in... (1 Reply)
Discussion started by: bluejwxn8
1 Replies

6. Shell Programming and Scripting

Shell script to run all the python scripts from particular directory

I have N number of python scripts which i am receiving from REST web server and i am saving them in /home/mandar/ . Now i want to write a script to run all the python scripts from this directory and monitor them every minute...if any process is dead or hung it should be restarted by this script. ... (0 Replies)
Discussion started by: Mandar Nandale
0 Replies

7. UNIX for Dummies Questions & Answers

Sh script to run multiple php scripts

I wrote a .sh script to run 5 php scripts. The problem is that it's running 1 then 2 then 3 in that order .... I want it to execute all 5 at ONCE.... nohup php /home/script1/script1.php && nohup php /home/script2/script2.php && nohup php /home/script3/script3.php && nohup php... (1 Reply)
Discussion started by: holyearth
1 Replies

8. Shell Programming and Scripting

1 script or multiple scripts?? - check files, run jobs

Question for anyone that might be able to help: My objective is to eheck if a file (a source file) exists in a directory. If it does then, I'd like to call an application (Informatica ETL file...not necessary to know) to run a program which extracts data and loads it into multiple targets. ... (6 Replies)
Discussion started by: jnanasakti
6 Replies

9. Shell Programming and Scripting

Check if script run by a user directly or via other scripts

Hi, i have a script 'a.sh' that should be called only by certain scripts like b.sh, c.sh Inside a.sh, how can i determine 1) if this script was run directly from command prompt (or scheduler) 2) if called via other scripts? Is there an easy way to get parent process name (not just pid),... (2 Replies)
Discussion started by: ysrinu
2 Replies

10. Shell Programming and Scripting

Run scripts within a script..

Hi all... I have several scripts of varying types (shell script, expect script, awk script) that I would like to run within 1 script.. They also take a command line argument (which it is getting successfully). The problem is, the parent script is exiting after the first script it calls is... (2 Replies)
Discussion started by: earnstaf
2 Replies
Login or Register to Ask a Question