LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-03-2003, 11:04 PM   #1
jester_69
Member
 
Registered: May 2002
Location: Sydney Australia
Distribution: Redhat 6.1 & 7.2
Posts: 91

Rep: Reputation: 15
Shell Scripting Question


I have been having trouble adding an option to the botton of my shell script like so

"Please Choose from the following list of options:"
1. Show the path enviroment varible
2. List all text files under your home directory
3. Show the disk usage for your user home directory
4. Exit

Selection _ ( with a flashing cursor )

I need to have it that if wrong selection is made ( say 5 ) it will print an error & return after two seconds to the selection screen again

Here is the start of my script

clear
echo "Please Choose from the following list of options:"
echo 1. Show the path enviroment varible
echo 2. List all text files under your home directory
echo 3. Show the disk usage for your user home directory
echo 4. Exit
echo "What is your Selection" (how do i put flashing cursor & add error message if wrong button is pressed)
read vvar
if [ ${vvar} -eq 1 ];then
echo "This is what you are bashing"
cat .bash_profile |grep HOME


Any help/advice is appreciated

Regards

Andrew
 
Old 11-03-2003, 11:10 PM   #2
jhorvath
Member
 
Registered: Sep 2002
Location: OH, USA
Distribution: 2.6.16-1.2096_FC5 #1
Posts: 245

Rep: Reputation: 30
instead of the if[${vvar}...]

maybe a switch would do...

Code:
switch ${vvar}
    case 1)
        `do something'
        ;;
    case 2)
        `do something else`
         ;;
    *)
        echo "Error: bad selection" && sleep 2 && `go back to menu`
        ;;
esac
not exact on the syntax ...haven't done scripting in a minute
or even a while loop might be better ..check the syntax on that as well ..

and i believe that the blinking cursor is dependant on your / the user's system and what not ...what terminal..etc...not sure that you can force that

Last edited by jhorvath; 11-03-2003 at 11:20 PM.
 
Old 11-04-2003, 12:08 AM   #3
paonethestar
Member
 
Registered: Oct 2003
Posts: 47

Rep: Reputation: 16
Code:
while [ 1 ]
do
echo "          
1. Show the path enviroment varible
2. List all text files under your home directory
3. Show the disk usage for your user home directory
4. Exit"
read hello
case $hello in

        1)
                ;ur code
                ;;
        2)
                ;ur code
                ;;
       3)
                ;ur code
                ;;
        *)
                ;ur code
                ;;
esac
done
I think u can write ur code for each case .This is perfect sytax for ur code.Bye.

Last edited by paonethestar; 11-04-2003 at 12:11 AM.
 
Old 11-04-2003, 12:23 AM   #4
jester_69
Member
 
Registered: May 2002
Location: Sydney Australia
Distribution: Redhat 6.1 & 7.2
Posts: 91

Original Poster
Rep: Reputation: 15
What is wrong with the last "fi" statement in this script ?

#!/bin/bash
# this sets the inital stuff
vvar=0
while test ${vvar} != 4; do
clear
echo "Please Choose from the following list of options:"
echo 1. Show the path enviroment varible
echo 2. List all text files under your home directory
echo 3. Show the disk usage for your user home directory
echo 4. Exit
echo ""
read vvar
if [ ${vvar} == 1 ];then
cat .bash_profile |grep HOME
echo "Press any key to continue"
echo ""
read evar #evar empty variable
fi
if [ ${vvar} == 2 ]; then
ls -la *.txt
echo "Press any key to continue"
echo ""
read evar #evar empty variable
fi
if [ ${vvar} == 3 ]; then
du ~/ -s -h
echo "Press any key to continue"
echo ""
read evar #evar empty variable
fi
if [ ${vvar} == 4 ]; then
echo "Thanks for Coming $USER"
exit 0
fi
if [ ${vvar} == * ] then
echo "Sorry, Wrong key pressed.Please choose another option from list"
sleep 2
echo "Press any key to continue"
echo ""
read evar #evar empty variable
fi
done
vvar=1
 
Old 11-04-2003, 12:37 AM   #5
paonethestar
Member
 
Registered: Oct 2003
Posts: 47

Rep: Reputation: 16
Code:
if [ ${vvar} == 1 ];then
                cat .bash_profile |grep HOME
                echo "Press any key to continue"
                echo ""
                read evar #evar empty variable

        else if [ ${vvar} == 2 ]; then
                ls -la *.txt
                echo "Press any key to continue"
                echo ""
                read evar #evar empty variable

        else if [ ${vvar} == 3 ]; then
                du ~/ -s -h
                echo "Press any key to continue"
                echo ""
                read evar #evar empty variable

        else if [ ${vvar} == 4 ]; then
                echo "Thanks for Coming $USER"
                exit 0
        else
                echo "Sorry, Wrong key pressed.Please choose another option from list"
                sleep 2
                echo "Press any key to continue"
                echo ""
                read evar #evar empty variable
        fi
        fi
        fi
        fi
change ur code to this after reading sentence.Because u can't assign "*" for remaing all other choices like this.It may take it as string or any.With my knowledge i can say that assignment causing prob !.

Last edited by paonethestar; 11-04-2003 at 12:38 AM.
 
Old 11-04-2003, 01:34 AM   #6
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
I also posted this in your other post in General Linux forum.

Code:
#!/bin/bash
select program in  'Show the path enviroment varible' \
    'List all text files under your home directory' \
    'Show the disk usage for your user home directory' \
    'Exit'
do

  case $REPLY in

     1)
     #Show the path enviroment varible
     echo -e ${PATH//:/\\n}
     ;;
     2)
     #List all text files under your home directory
     file `find ~/ -type f -maxdepth 1`|grep ASCII
     ;;
     3)
     #Show the disk usage for your user home directory
     du -s ~/
     ;;
     4)
     break
     ;;
     *)
     echo "Please select another option from the list"
     sleep 2
     ;;

   esac

done

Last edited by /bin/bash; 11-04-2003 at 01:39 AM.
 
Old 11-04-2003, 02:06 AM   #7
Robert0380
LQ Guru
 
Registered: Apr 2002
Location: Atlanta
Distribution: Gentoo
Posts: 1,280

Rep: Reputation: 47
what is wrong is you forgot a semi colon


if [ ] ; <<<<< that one is missing on the last if statement.
 
Old 11-04-2003, 06:34 AM   #8
jester_69
Member
 
Registered: May 2002
Location: Sydney Australia
Distribution: Redhat 6.1 & 7.2
Posts: 91

Original Poster
Rep: Reputation: 15
Thank you all especially you paonethestar

Thats exectly what i needed

script writing can be a beautiful thing if you know what your doing 8^).
 
Old 11-04-2003, 07:28 AM   #9
jester_69
Member
 
Registered: May 2002
Location: Sydney Australia
Distribution: Redhat 6.1 & 7.2
Posts: 91

Original Poster
Rep: Reputation: 15
/bin/bash what puts the #? at the bottom of the selections??
 
Old 11-04-2003, 12:35 PM   #10
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
That would be the PS3 variable.
PS3="Please make your selection..."
You probably don't want to export PS3, that way when the script finishes the PS3 will go back to it's normal self.
 
Old 11-04-2003, 06:20 PM   #11
jester_69
Member
 
Registered: May 2002
Location: Sydney Australia
Distribution: Redhat 6.1 & 7.2
Posts: 91

Original Poster
Rep: Reputation: 15
One last question

Currently list is shown like this

1. Show the PATH environment variable
2. List all text files under your user home directory
3. Show the disk usage for your user home directory
4. Exit
Selection:
-- (cursor is here)

how can i make it so it is here

1. Show the PATH environment variable
2. List all text files under your user home directory
3. Show the disk usage for your user home directory
4. Exit
Selection: _

I know this is just cosmectic but am interested to know all the same

Regards

Jester
 
Old 11-04-2003, 08:55 PM   #12
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
You need to post that part of your script. For instance using my script and adding the PS3 variable I get this:

1) Show the path enviroment varible
2) List all text files under your home directory
3) Show the disk usage for your user home directory
4) Exit
Selection: _

The cursor is just a block not an underscore.

Code:
#!/bin/bash
PS3="Selection: "
select program in  'Show the path enviroment varible' \
    'List all text files under your home directory' \
    'Show the disk usage for your user home directory' \
    'Exit'
do

...
...
...

done

Last edited by /bin/bash; 11-04-2003 at 08:57 PM.
 
Old 11-05-2003, 06:38 AM   #13
paonethestar
Member
 
Registered: Oct 2003
Posts: 47

Rep: Reputation: 16
Instead u can use "echo -n " to remove trailing newlines.
Bye.
 
Old 11-05-2003, 06:55 PM   #14
Skyline
Senior Member
 
Registered: Jun 2003
Distribution: Debian/other
Posts: 2,104

Rep: Reputation: 45
(apol. in advance mods - also reply in Linux General)

Check out man du ls etc etc etc to fine-tune the options in the functions at the top: - a quick example:

Code:
#!/bin/bash

a() { echo $PATH; }
b() { ls -la; }
c() { du -sh; }

echo ""
echo "Please type either 1 or 2 or 3 for the following respectively:" 
echo ""

OPTIONS="PATH ls du"

select opt in $OPTIONS; do
                if [ "$opt" = "PATH" ]; then
                a;
                exit
               elif [ "$opt" = "ls" ]; then
                b;
                exit
               elif [ "$opt" = "du" ]; then
                c;
                exit
               else
                clear
                echo "You haven't selected an appropriate option - try again"
               fi
           done
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
a shell-scripting question: mrchaos Slackware 3 09-22-2005 11:00 AM
Shell scripting question. dragin33 Linux - General 2 08-11-2004 05:17 PM
Shell Scripting Question Onyx^ Linux - General 5 04-27-2004 10:37 AM
Shell Scripting Question b_vasu Linux - Newbie 1 11-21-2003 02:10 PM
Shell Scripting Question chrisk5527 Linux - General 12 07-09-2003 03:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration