LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 12-21-2009, 02:19 AM   #1
m4rtin
Member
 
Registered: Sep 2007
Posts: 261

Rep: Reputation: 16
Question 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?

Last edited by m4rtin; 12-21-2009 at 02:43 AM.
 
Old 12-21-2009, 02:31 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Have you had any ideas or tried anything yet? LQ is more likely to help on seeing those -- and a sample list.
 
Old 12-21-2009, 03:03 AM   #3
m4rtin
Member
 
Registered: Sep 2007
Posts: 261

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by catkin View Post
Have you had any ideas or tried anything yet? LQ is more likely to help on seeing those -- and a sample list.
yes, sorry. I have made this:

Code:
#/bin/bash

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"
done
However, there are multiple-word interface names in it and 'select' makes those as different options. Is there a possibility to change this?
 
Old 12-21-2009, 03:06 AM   #4
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
What output does the snmpwalk command give?:
Code:
snmpwalk -v 2c -c public 192.168.1.1 ifAlias
and which parts of the output are you after?
 
Old 12-21-2009, 03:18 AM   #5
m4rtin
Member
 
Registered: Sep 2007
Posts: 261

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by Disillusionist View Post
What output does the snmpwalk command give?:
Code:
snmpwalk -v 2c -c public 192.168.1.1 ifAlias
and which parts of the output are you after?
This:
Code:
snmpwalk -v 2c -c public 192.168.1.1 ifAlias | sed 's/^.*\: //'
will give me something similar:

Quote:
Customer1
Customer 2
Name 3 45
Interface eth0
blah blah

At the moment if I run this select script I get:

Quote:
1) Customer1
2) Customer
3) 2
4) Name
5) 3
6) 45
7) Interface
8) eth0
9) blah
10) blah
11) quit
but I would like to have:

Quote:
1) Customer1
2) Customer 2
3) Name 3 45
4) Interface eth0
5) blah blah
6) quit
Any ideas how to achieve this? Or is it possible at all using select?

Last edited by m4rtin; 12-21-2009 at 03:24 AM.
 
Old 12-21-2009, 03:40 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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}
Does this do the trick?
 
Old 12-21-2009, 03:58 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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}
 
Old 12-21-2009, 04:58 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
var=$( snmpwalk -v 2c -c public 192.168.1.1 ifAlias | awk -F": " '{
    a[++d]=$2
}
END{
    for(i=1;i<=d;i++){
        print i") "a[i]
    }
    printf "Choose: "
    getline choice < "-"
    echo $choice
}' )

echo $var
 
Old 12-21-2009, 06:16 AM   #9
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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.
 
Old 12-21-2009, 09:41 AM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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.
 
  


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
Selecting certain parts of a list of columns in BASH mikejreading Linux - Newbie 6 05-07-2009 04:48 AM
Conditional (IF) test in bash sh script - presence of first parameter Critcho Linux - Newbie 6 10-01-2008 12:20 AM
Passing parameters to bash script Kamikazee Programming 4 10-01-2005 06:41 AM
bash script ? -- spaces in passed parameters azwr Linux - Newbie 3 06-18-2004 06:57 PM
Passing Parameters to Bash Script mooreted Linux - Software 3 04-05-2004 09:08 PM

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

All times are GMT -5. The time now is 01:18 AM.

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