bash script for selecting a parameter from list of parameters
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
bash script for selecting a parameter from list of parameters
"snmpwalk -v 2c -c public 192.168.1.1 1.0.3.45.23.2.1" will print me a list(about 15 names, but may vary) of network interface names to stdout(most of those names ae multi word names). I would like to make a script, which puts a serial number(1,2,3,4,5,etc) before each interface name printed to stdout and then the script should wait for user input. The user input has to be one of those serial numbers and ENTER. Then this script will to something using this certain interface name. How to accomplish this in bash? Any example scripts?
You can try to pass the output of the snmpwalk command directly to the "in list", but previously you have to set the Input Field Separator (internal variable IFS) to a newline. This forces the shell to split the input fields not based on blank spaces, but on newlines only. Easier to show, than to explain:
Code:
#/bin/bash
#
# Store the current IFS - default is <space><tab><newline>
#
OLD_IFS=${IFS}
#
# Set IFS to newline
#
IFS="
"
#
# Use "process substitution" to build the list for the select statement
# See: http://tldp.org/LDP/abs/html/process-sub.html
#
PS3="Choose: "
select i in $(< <(snmpwalk -v 2c -c public 192.168.1.1 ifAlias | sed 's/^.*\: //')) quit
do
[ "$i" = "quit" ] && exit 0
echo "You chose $i"
break
done
#
# Restore original IFS
#
IFS=${OLD_IFS}
Just a note. I'm not sure, since I cannot reproduce your example, but maybe setting the IFS is enough and the process substitution is not really necessary. You can give it a try:
Code:
#/bin/bash
OLD_IFS=${IFS}
IFS="
"
SNMPWALK=$(snmpwalk -v 2c -c public 192.168.1.1 ifAlias | sed 's/^.*\: //')
PS3="Choose: "
select i in $SNMPWALK quit
do
[ "$i" = "quit" ] && exit 0
echo "You chose $i"
break
done
IFS=${OLD_IFS}
Just in case you don't want to use IFS or don't want to split a scalar variable using the command-line parser:
Code:
#!/bin/bash
exec 4< <(exec snmpwalk -v 2c -c public 192.168.1.1 ifAlias | sed 's/^.*\: //')
ITEMS=()
if [[ BASH_VERSION[0] -ge 4 ]]; then
readarray -u 4 ITEMS
else
while read -u 4 LINE; do
ITEMS[${ITEMS[@]}]=$LINE
done
fi
exec 4<&-
select A in "${ITEMS[@]}" quit; do
[[ $A = quit ]] && break
<do something with $A>
done
OR
Code:
...
ITEMS[$(( QID = ${#ITEMS[@]} ))]='Quit'
select __ in "${ITEMS[@]}"; do
ID=$(( --REPLY ))
[[ ID -eq QID ]] && break
< do something with ${ITEMS[ID]} >
done
There's a reason why using the command-line to split a scalar variable may cause unexpected result. This is mostly when the string has a glob pattern. For example
Code:
VAR='1 * 2 3 4'
for A in $VAR; do
echo "$A"
done
may output:
Code:
1
<some file(s)>
2
3
4
Last edited by konsolebox; 12-21-2009 at 06:21 AM.
The bash select command would allow the user to choose by typing in a number rather than a string and would thus be easier on the user and less error prone.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.