How to make sure the input string is one of many options?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to make sure the input string is one of many options?
# 1  
Old 12-07-2011
How to make sure the input string is one of many options?

How to make sure the input string is one of many options e.g centos-5.5-i386 windows-2003r2-x64 ?
The options are dynamic, so "case" condition check doesn't work.

I use grep -o -w , it doesn't work every time because - is valid word boundry

Code:
#word windows-2003 failed  the check as expected
$input=windows-2003
$echo "centos-5.5-i386 windows-2003r2-x64" | grep -o -w $input

#word windows-2003r2 passed the check, but it is not a valid option
$input=windows-2003r2
$echo "centos-5.5-i386 windows-2003r2-x64" | grep -o -w $input
windows-2003r2

# 2  
Old 12-07-2011
Bug

give this a try
Code:
$ export input=centos
$ echo "centos-5.5-i386 windows-2003r2-x64 freebsd-6.1-i386" | egrep -o -w "\s?$input[^\s]*\s?"
centos-5.5-i386
$ export input=freebsd
$ echo "centos-5.5-i386 windows-2003r2-x64 freebsd-6.1-i386" | egrep -o -w "\s?$input[^\s]*\s?"
freebsd-6.1-i386
$ export input=windows
$ echo "centos-5.5-i386 windows-2003r2-x64 freebsd-6.1-i386" | egrep -o -w "\s?$input[^\s]*\s?"
windows-2003r2-x64

# 3  
Old 12-07-2011
Quote:
Originally Posted by MR.bean
give this a try
Code:
$ export input=centos
$ echo "centos-5.5-i386 windows-2003r2-x64 freebsd-6.1-i386" | egrep -o -w "\s?$input[^\s]*\s?"
centos-5.5-i386
$ export input=freebsd
$ echo "centos-5.5-i386 windows-2003r2-x64 freebsd-6.1-i386" | egrep -o -w "\s?$input[^\s]*\s?"
freebsd-6.1-i386
$ export input=windows
$ echo "centos-5.5-i386 windows-2003r2-x64 freebsd-6.1-i386" | egrep -o -w "\s?$input[^\s]*\s?"
windows-2003r2-x64

Thanks, but it is misunderstood,
the input has to be EXACT match of one of options e.g.
centos-5.5-i386 windows-2003r2-x64 freebsd-6.1-i386
so, centos/freebsd/windows are not valid input
I am looking for a shell one-liner,any command, to achieve this, grep is just an example

I could compare the length of the input string and length of the string being search/replaced, but not an elegant solution.
# 4  
Old 12-07-2011
Bug

i guess you have to do it this way then

Code:
$ export input=windows-2003r2
$ echo "centos-5.5-i386 windows-2003r2-x64" | egrep -o -e "\s$input\s" -e "^$input\s" -e "\s$input$"
$ export input=windows-2003r2-x64
$ echo "centos-5.5-i386 windows-2003r2-x64" | egrep -o -e "\s$input\s" -e "^$input\s" -e "\s$input$"
 windows-2003r2-x64

# 5  
Old 12-08-2011
I found this "if condition check" satisfy my need.

@(pattern-list)
Matches one of the given patterns
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert a user input string after matched string in file

i am having file like this #!/bin/bash read -p 'Username: ' uservar match='<color="red" />' text='this is only a test so please be patient <color="red" />' echo "$text" | sed "s/$match/&$uservar\g" so desireble output what i want is if user type MARIA this is only a test so please... (13 Replies)
Discussion started by: tomislav91
13 Replies

2. UNIX for Beginners Questions & Answers

Search a string and display its location on the entire string and make a text file

I want to search a small string in a large string and find the locations of the string. For this I used grep "string" -ob <file name where the large string is stored>. Now this gives me the locations of that string. Now how do I store these locations in a text file. Please use CODE tags as... (7 Replies)
Discussion started by: ANKIT ROY
7 Replies

3. Shell Programming and Scripting

Input two password(pass1/pass2) options for single node..

Hi Team, i want to input two password for single node like pass1/pass2 one of the pass1 is working some node and pass2 is working for some nodes . For nodes having pass1 i have to run diffrent script and for nodess having pass2 i have to run diffrent script Sooo how can put two pass... (1 Reply)
Discussion started by: Ganesh Mankar
1 Replies

4. Shell Programming and Scripting

How do i make input start with a letter?

Howdy folks, How do i make the input start with a letter and certain num of numbers and if anything else is entered it should display error for ex; a123455 is good ( exactly 7 characters) and ex: sio234234 is not good ex: b233 is not good.. ex: jlasjdlfks is not good ... (2 Replies)
Discussion started by: coolkid
2 Replies

5. Shell Programming and Scripting

make loop from input file

Hi Guys, I have file A.txt PP97 PP66 PP87 PP66 PP47 PP57 PP44 PP20 PP66 PP16 PP13 PP51 PP68 PP70 PP75 PP30 (2 Replies)
Discussion started by: asavaliya
2 Replies

6. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

7. Shell Programming and Scripting

make a variable read the value from the standard input

Hi, In Perl, how can we define a variable make it read the value from the standard input? Meaning, how can have the user type in the value that will be assigned to the variable? Thanks, (2 Replies)
Discussion started by: Pouchie1
2 Replies

8. BSD

Make options for wget

I mam trying to install wget from the command line without user intervention. So I devised something as follows: cd /usr/ports/ftp/wget make -DWITHOUT_GNUTLS=yes -DWITHOUT_IPV6=no -DWITHOUT_NLS=no -DWITHOUT_OPENSSL=no However, this will still get me into the screen where user input is required.... (4 Replies)
Discussion started by: figaro
4 Replies

9. UNIX for Dummies Questions & Answers

Installing Apache with make options

I am installing apache on freebsd and the screen comes up with make options. Here I would like to deselect DAV and DAV_FS. How can this be done on the command line in order to achieve a silent install? This is what I have currently, but the installation script still enters the menu with the make... (2 Replies)
Discussion started by: figaro
2 Replies

10. Shell Programming and Scripting

Scripts that take String options

I need help to know how to create a script that takes string options and not only 1 charecter. I wanted to make a script that takes 'string' options eg. hscript.sh -product XXX -version XXX I know that I can make a script that takes "1 Charecter " options eg. hscript.sh -m XXX -l XXX -s XXX ,... (2 Replies)
Discussion started by: hanhanbib
2 Replies
Login or Register to Ask a Question