LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Shell Scripting Question (https://www.linuxquestions.org/questions/programming-9/shell-scripting-question-112105/)

jester_69 11-03-2003 11:04 PM

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

jhorvath 11-03-2003 11:10 PM

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

paonethestar 11-04-2003 12:08 AM

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.

jester_69 11-04-2003 12:23 AM

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

paonethestar 11-04-2003 12:37 AM

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 !.

/bin/bash 11-04-2003 01:34 AM

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


Robert0380 11-04-2003 02:06 AM

what is wrong is you forgot a semi colon


if [ ] ; <<<<< that one is missing on the last if statement.

jester_69 11-04-2003 06:34 AM

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^).

jester_69 11-04-2003 07:28 AM

/bin/bash what puts the #? at the bottom of the selections??

/bin/bash 11-04-2003 12:35 PM

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.

jester_69 11-04-2003 06:20 PM

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

/bin/bash 11-04-2003 08:55 PM

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


paonethestar 11-05-2003 06:38 AM

Instead u can use "echo -n " to remove trailing newlines.
Bye.

Skyline 11-05-2003 06:55 PM

(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



All times are GMT -5. The time now is 09:25 PM.