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?