Operating System - Linux
1756297 Members
2561 Online
108844 Solutions
New Discussion юеВ

Shell script: get default value

 
shasha_1
Super Advisor

Shell script: get default value

I would like to create a shell script that will do the following:

su - username

A list of choices will be shown and one of the choices is the default. Example is:

Choice A
Choice B
Choice C
Choice D(Default)

Choose option or Choice D(Default)

I'd like to get the default option so that I will not be asked for any inputs in the script.

How can I do this is my script?

Help please!!!
5 REPLIES 5
shasha_1
Super Advisor

Re: Shell script: get default value

Sorry, choices are like this:

Choice A
Choice B
Choice C
Choice D

Choose option or [Choice D]

If I just press enter, the default choice will be given to me, which is Choice D.

Again, how do I set this in my script so that I will not be prompted for any input and whatever is the default will be taken?

Matti_Kurkela
Honored Contributor

Re: Shell script: get default value

This looks suspiciously like asking free answers for a "Shell Scripting 101" exercise, but here goes...

You must first display the list of choices, then receive user's input, and finally choose what to do based on the input.

When you have to select just one thing among many, the "case ... esac" is often the best structure.

The basic structure would be like this:

#!/bin/sh
echo "Choose A, B, C or D:"
read choice
case "$choice" in
A|a)
echo "you chose A"
;;
B|b)
echo "you chose B"
;;
C|c)
echo "you chose C"
;;
*)
echo "you chose D, which is the default"
;;
esac

Note that you need to explicitly check for upper and lower case characters ("a" is not the same as "A"). The last choice is picked if none of the previous ones matched, because "*" always matches.
MK
Muthukumar_5
Honored Contributor

Re: Shell script: get default value

You can also use this as,

# cat test.sh
#!/bin/ksh
echo "Choice A,B,C,D(default)"
choice="";
read choice;

case ${choice} in

A)
echo "Choice A";
;;

B)
echo "Choice B";
;;

C)
echo "Choice C";
;;

*)
echo "Choice D";
;;
esac


hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: Shell script: get default value

You can also use if-elif-else for this as,

#!/bin/ksh
echo "Choice A,B,C,D(default)"
choice="";

read choice;

choice=$(echo $choice | tr "[a-z]" "[A-Z]")

if [[ ${choice} = "A" ]]
then

echo "Choice A"

elif [[ ${choice} = "B" ]]
then

echo "Choice B"

elif [[ ${choice} = "C" ]]
then

echo "Choice C"

else

echo "Choice D - Default"

fi

# END #

hth.
Easy to suggest when don't know about the problem!
Juan M Leon
Trusted Contributor

Re: Shell script: get default value

Hi Donna, is this for a generic user where they wont have prompt access or it is just to set different type environment.
first to generic user with out prompt access.

#!/usr/bin/sh
BOLD=`tput 'smso'`
NORMAL=`tput 'rmso'`

while :
do
echo "
Choose option
A ) Do AAAAA
B ) Do BBBBB
C ) Do CCCCC
D ) $BOLD Do BBBBB $NORMAL
X ) eXit
Choce or Enter to default option :

"
read OPTION
if [ $OPTION = "" ] ; then
OPTION=D
fi
case $OPTION in
a | A ) echo "Running ....AAAA" ; your command here ;;
b | B ) echo "Running ....BBBB" ;
your command here ;;
c | C ) echo "running ....CCCC" ;
your command here ;;
d | D ) echo "Running ....DDDD" ; your command here ;;
x | X ) exit 1;;
* ) echo "Option $OPTION not available try again" ;;
esac


done

and you can edit the .profile file and add the following line line at the end

exec my-script-name

Options to source environment it is almost the same

#!/usr/bin/sh
BOLD=`tput 'smso'`
NORMAL=`tput 'rmso'`

echo "
Choose option
A ) Source environment a
B ) Source environment b
C ) Source environment c
D ) $BOLD Source environment d $NORMAL

Choce or Enter to default option :

"
read OPTION
if [ $OPTION = "" ] ; then
OPTION=D
fi
case $OPTION in
a | A ) echo "Running ....AAAA" ; your command here ;;
b | B ) echo "Running ....BBBB" ;
your command here ;;
c | C ) echo "running ....CCCC" ;
your command here ;;
d | D ) echo "Running ....DDDD" ; your command here ;;
* ) echo "Option $OPTION not available try again" ;;

esac

I hope it helps