Hi,
I have an array with characters and I am looking for specific character in that array and if those specific character not found than I use goto statment which is define somehwhere in the script. My code is:
set a = (A B C D E F)
@ i = 0
while ($i <= ${#a})
if ($a[$i] != "F" || $a[$i] != "D") then
goto USAGE
endif
@ i++
end
Can this be done in one line using awk/sed/grep? That is if this array has characters other than D or F than goto statement should be executed. That is I expect array a with only D and F .
Thank you so much for your help in advance.
It's not a good idea to call an external executable in shell script if it's not necessary as extra resources are requried to load the binary and execute it making your script slower and consume more resources.
I'd be more inclined to use any time you would have spend simplifying this script into moving it to a better shell (like ksh or bash).
And once you have moved into bash or ksh, you can do what you want in the following way:
#!/bin/bash
function Usage {
echo "Use me as you wish"
exit 0
}
a=( D A F D D F )
if grep -q '[^ DF]' <<< ${a[*]} ; then
echo contains garbage
Usage
fi
Then the grep searches for [^ DF], which means anything that is not D, F or space. The output of grep is not needed, so that's why the -q. And the test is performed on the return value of grep. <<< is a redirection from a variable and ${a[*]} (or ${a[@]}) is the whole array.
@dixits: you probably mean && instead of || otherwise your condition will always be true... @mirni. It can also be done without a here-string, or an external program:
case ${a[@]} in
*[^DF\ ]*) echo contains garbage
Usage
esac