- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Shell script: get default value
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-29-2005 05:02 PM
тАО07-29-2005 05:02 PM
Shell script: get default value
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!!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-29-2005 05:05 PM
тАО07-29-2005 05:05 PM
Re: Shell script: get default value
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-31-2005 09:24 PM
тАО07-31-2005 09:24 PM
Re: Shell script: get default value
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.
- Tags:
- case
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-31-2005 09:50 PM
тАО07-31-2005 09:50 PM
Re: Shell script: get default value
# 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-31-2005 11:12 PM
тАО07-31-2005 11:12 PM
Re: Shell script: get default value
#!/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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-09-2005 02:55 AM
тАО08-09-2005 02:55 AM
Re: Shell script: get default value
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