LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 01-05-2012, 02:06 AM   #1
RanjanR
LQ Newbie
 
Registered: Sep 2010
Posts: 7

Rep: Reputation: 0
How to take parameters in array variables and search for a pattern?


Hello,

How can I take the parameters passed in a function in array and search for a pattern?
I want to search the pattern fruit/apple and save the value in a variavle.
In below code array is not used and its not giving the correct output.

#!/usr/bin/ksh
a=${1}
b=fruit/${a}
c=
getfruit()
{
for i in $*;
do
if [ ${i} = "*${b}" ]
then
vfruit=${i}
echo 'fruit found'
else
echo Check for next fruit'
fi
done
}
getfruit season/fruit/red/apple season/fruit/red/cherry season/fruit/apple season/fruit/orange
echo 'fruit location is:' ${vfruit}


src>try.ksh apple

Please help. If this can be done without using array...that would also be fine.


The program should return:
fruit location is: season/fruit/apple
 
Old 01-05-2012, 04:08 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Don't use italics, font colors, start/stop lines, or "quote" tags.

Your main problem is here:

Code:
if [ ${i} = "*${b}" ]
The traditional [..] test cannot do globbing matches. It will work if you change to the newer [[..]] built-in and remove the quotes around the "*" (globs must be unquoted in order to work).

In addition, in the [..] form you need to quote all variables. "[" is a command name, a synonym for the test command. Everything that follows it is an argument to the test. If $i contained a string with whitespace, it would be word-split into multiple arguments, and would break the command.

Note that [[..]] doesn't have that problem, as it doesn't do word-splitting on variables. But it never hurts to quote them anyway.


As for the rest of your script:

1) You have a variable "c" that you never use.

2) $* expands into a list of the input parameters as a single string. But since you didn't quote it, that string is then re-split back into individual words. You should be using "$@" instead, which outputs the parameters as individual words, and when quoted, acts as if each word were quoted individually.

Or in this case just use "for i ; do". A for loop defaults to the input parameters when no input words are given.

3) QUOTE ALL OF YOUR VARIABLE SUBSTITUTIONS. As shown above, you should never leave the quotes off a variable expansion unless you explicitly want the resulting string to be word-broken by the shell (there are only a few safe locations, such as in [[..]]). This is a vitally important concept in scripting, so train yourself to do it correctly now.

Speaking of which, you really don't need to use the full bracket form for all of your variables. "${i}" is actually more cluttered and harder to read than "$i", and more work to type. Save the brackets for when you really need them.

4) Clean, consistent formatting makes code readable and more easily debuggable. Indent all your sub-commands, and separate logical sections with whitespace. Add comments anywhere the code isn't completely obvious (and remember, what seems obvious to you now will not be a year or so down the line).


So with all that, here's your script fully cleaned up and working:

Code:
#!/usr/bin/ksh

a="$1"
b="fruit/$a"

getfruit(){

	for i; do

		if [[ "$i" == *$b ]]; then
			vfruit="$i"
			echo 'fruit found'
		else
			echo 'Check for next fruit'
		fi

     done
}

getfruit season/fruit/red/apple season/fruit/red/cherry season/fruit/apple season/fruit/orange

echo "fruit location is: $vfruit"

exit 0

Finally, you're probably better off here replacing the if test with a case statement. And if you want to store the list into an array, just use it instead of the input parameters.

Code:
#!/usr/bin/ksh

a="$1"
b="fruit/$a"
fruitlist=( season/fruit/red/apple
	    season/fruit/red/cherry
	    season/fruit/apple
	    season/fruit/orange
	  )

getfruit(){

	for i in "${fruitlist[@]}"; do

		case "$i" in

			*$b) vfruit="$i"
			     echo 'fruit found' ;;

			*)   echo 'Check for next fruit' ;;

		esac

	done
}

getfruit

echo "fruit location is: $vfruit"
PS: You do know that the "red" fruits will never match the *$b globbing pattern, right?

Last edited by David the H.; 01-05-2012 at 04:14 PM. Reason: errrorrs...
 
1 members found this post helpful.
Old 01-06-2012, 05:29 AM   #3
RanjanR
LQ Newbie
 
Registered: Sep 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Thank You so much!!!
 
  


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
lockout parameters /variables andy curran Linux - Security 1 04-28-2011 11:49 AM
[SOLVED] /bin/bash if statement pattern search, end of pattern special character? headhunter_unit23 Programming 3 04-29-2010 08:05 AM
bash: use file as input into array, parse out other variables from array using awk beeblequix Linux - General 2 11-20-2009 10:07 AM
How to use variables in search pattern in gensub function of awk rajeshksv Linux - Newbie 1 08-07-2009 07:07 AM
Scripting Question - multiple {parameters} and variables gchilders Linux - Newbie 3 10-08-2008 08:31 AM

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

All times are GMT -5. The time now is 03:19 PM.

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