Register
It is currently Mon Sep 01, 2014 12:33 pm

1 or more options to execute


All times are UTC - 6 hours


Post new topic Reply to topic  [ 17 posts ] 
Author Message
 PostPosted: Sat Jun 27, 2009 11:14 pm   

Joined: Wed Apr 08, 2009 12:41 pm
Posts: 15
Code:
#!/bin/bash

function error() {
   clear
   echo "Enter valid n°"
   read -p 'Press "Enter" to continue'
}

while : ; do
clear;
echo -e "\n"
echo "    __________________________ "
echo "   |         $(tput smso) Main Menu $(tput rmso)      |"
echo "   |--------------------------|"
echo "   |  1. List Directory       |"
echo "   |  2. Show disk space      |"
echo "   |  3. displays processes   |"
echo "   |  4. Restart apache        |"
echo "   |  5. Restart  Exim          |"
echo "   |  6. Restart MySQL         |"
echo "   |  7. Install Logwatch       |"
echo "   |  8. Show Partitions        |"
echo "   |  9. Empty Log files        |"
echo "   |  10. Show date              |"
echo "   |  0. Quit                         |"
echo "   |__________________________|"
echo
echo "$(tput smso) If entering multiple selections, leave a space $(tput rmso)"

read -p "Enter selection(s): " x

if [ -z $x ]; then
   error
elif [[ ! $x =~ [0-3] ]]; then
   error
elif [ $x -eq 0 ]; then
   clear
   exit 0
else
   for option in ${x}; do
      case $option in
       1) echo -e "\n DIRECTORY LISTING"
          ls -al
       ;;
       2) echo -e "\n AVAILABLE DISK SPACE"
          df -Th
       ;;
       3) echo -e "\n RUNNING PROCESSES"
          ps auxf
       ;;
      esac
   done | less -P 'to exit, press "q"'
fi
done
I did not change any thing in your script; just added additional items. This will show you the problem I have now. Thank you very much, Watel. I really appreciate all your help :-)


Last edited by winsysop on Mon Aug 03, 2009 7:47 pm, edited 1 time in total.

Top
 Profile  
 PostPosted: Sun Jun 28, 2009 1:55 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 535
Code:
#!/bin/bash

function Menu() {
clear;
echo;echo
echo "    __________________________ "
echo "   |         $(tput smso) Main Menu $(tput rmso)      |"
echo "   |--------------------------|"
echo "   |  1. List Directory       |"
echo "   |  2. Show disk space      |"
echo "   |  3. displays processes   |"
echo "   |  0. Quit                 |"
echo "   |__________________________|"
echo
echo "$(tput smso) If entering multiple selections, leave a space $(tput rmso)"

read -p "Enter selection(s): " x
export x
}

while : ; do
   Menu

   for option in ${x}; do
      case $option in
       0) clear; exit 0
       ;;
       1) echo;echo "Listing directory"
          ls -al
          read -p "Press \"Enter\" to continue"
       ;;
       2) echo;echo "Show amount of available disk space"
          df -Th
          read -p "Press \"Enter\" to continue"
       ;;
       3) echo;echo "displays running processes"
          ps auxf
          read -p "Press \"Enter\" to continue"
       ;;
       *) clear
          echo "Enter valid n°"
          read -p "Press \"Enter\" to continue"
          Menu
       ;;
      esac
   done
done


Top
 Profile  
 PostPosted: Sun Jun 28, 2009 4:35 pm   

Joined: Wed Apr 08, 2009 12:41 pm
Posts: 15
Hello Watael, and thank you very much for taking the time to rewrite this script. I really appreciate it. If you can do me one more favor; is there any way to tweak this script to run/execute multiple selections/options without being prompted to Press "Enter" to continue. Other than that, the script is awesome. Thank you again

Scott


Top
 Profile  
 PostPosted: Sun Jun 28, 2009 5:39 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 535
To keep things clean on screen, you can erase `read...` statements for 1 to 3, and pipe commands output to `less`.
Code:
case $option in
   0) clear; exit 0
   ;;
   1) ls -al | less -P 'to exit, press "q"'
   ;;
[...]


Top
 Profile  
 PostPosted: Sun Jun 28, 2009 6:27 pm   

Joined: Wed Apr 08, 2009 12:41 pm
Posts: 15
Here is what I did:
Code:
1) echo;echo "Listing directory"
     ls -al | less -P 'Press "q" to continue'
;;


It works, but I am still prompted to enter "q" after it runs each command, even if I have selected more than one command. I need the script to run multiple commands, and prompt me after completing the final command to return to the main menu.

Thank you very much, Watael.


Top
 Profile  
 PostPosted: Sun Jun 28, 2009 9:08 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 535
Code:
#!/bin/bash

function error() {
   clear
   echo "Enter valid n°"
   read -p 'Press "Enter" to continue'
}

while : ; do
clear;
echo -e "\n"
echo "    __________________________ "
echo "   |         $(tput smso) Main Menu $(tput rmso)      |"
echo "   |--------------------------|"
echo "   |  1. List Directory       |"
echo "   |  2. Show disk space      |"
echo "   |  3. displays processes   |"
echo "   |  0. Quit                 |"
echo "   |__________________________|"
echo
echo "$(tput smso) If entering multiple selections, leave a space $(tput rmso)"

read -p "Enter selection(s): " x

if [ -z $x ]; then
   error
elif [[ ! $x =~ [0-3] ]]; then
   error
elif [ $x -eq 0 ]; then
   clear
   exit 0
else
   for option in ${x}; do
      case $option in
       1) echo -e "\n DIRECTORY LISTING"
          ls -al
       ;;
       2) echo -e "\n AVAILABLE DISK SPACE"
          df -Th
       ;;
       3) echo -e "\n RUNNING PROCESSES"
          ps auxf
       ;;
      esac
   done | less -P 'to exit, press "q"'
fi
done


Last edited by Watael on Sun Jun 28, 2009 9:54 pm, edited 2 times in total.

Top
 Profile  
 PostPosted: Sun Jun 28, 2009 9:49 pm   

Joined: Wed Apr 08, 2009 12:41 pm
Posts: 15
I really appreciate your help and I hate to bother you again with this. I will use the first version you wrote for the time being. I will play with it and see if I can get it to work. Take care, Watael.


Top
 Profile  
 PostPosted: Sun Jun 28, 2009 9:50 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 535
I just modified the script in order to deal with "letter errors"


Top
 Profile  
 PostPosted: Mon Aug 03, 2009 11:36 am   

Joined: Wed Apr 08, 2009 12:41 pm
Posts: 15
For some odd reason, the script won't work if I add 10 items or more to the menu. The only way to get the script to work is to have less than 10 items in the menu. Any help to get the script to work with 10 items or more is highly appreciated. Thank you


Top
 Profile  
 PostPosted: Mon Aug 03, 2009 3:46 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 535
what does your script look like now?


Top
 Profile  
 PostPosted: Mon Aug 03, 2009 7:49 pm   

Joined: Wed Apr 08, 2009 12:41 pm
Posts: 15
I just updated my first/original post with the new version of your script. Thank you very much


Top
 Profile  
 PostPosted: Mon Aug 03, 2009 8:53 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 535
You know there's a condition statement in my script where options number must be between 0 and 3.
Does removing/changing this statement make it work a better way?


Top
 Profile  
 PostPosted: Mon Aug 03, 2009 9:12 pm   

Joined: Wed Apr 08, 2009 12:41 pm
Posts: 15
Yes, I played with this statement, but didn't work:
Code:
elif [[ ! $x =~ [0-11] ]]; then


Top
 Profile  
 PostPosted: Mon Aug 03, 2009 11:11 pm   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
That statement will never work.

This because the regexp statement interprets that as 0-1 and 1.

[0-9][0-9] would match any number from 0 up until 99
[0-2][0-9] would match any number from 0 up until 29

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Tue Aug 04, 2009 7:58 am   

Joined: Wed Apr 08, 2009 12:41 pm
Posts: 15
Fredrikooo,
You are right; The regexp statement interprets that statement as 0-1 and 1. In theory, [0-x][0-x] should work, but in practice it didn't. I tried different possible options, but none worked. :-/


Top
 Profile  
 PostPosted: Fri Sep 04, 2009 11:38 pm   

Joined: Wed Apr 08, 2009 12:41 pm
Posts: 15
any help with this is greatly appreciated :-)


Top
 Profile  
 PostPosted: Sat Sep 05, 2009 12:32 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 535
It seems you should change statement to
Code:
elif [ $x -gt 11 ]; then error


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 17 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 12 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP