Hi all
I have a variable called "variable" and is of the form
variable ="AAA BBB CCC DDD" {basically it has values separated by spaces}
What is the simplest way to check if "variable" has more that one value in its list?
Thanks.
Hi all
I have a variable called "variable" and is of the form
variable ="AAA BBB CCC DDD" {basically it has values separated by spaces}
What is the simplest way to check if "variable" has more that one value in its list?
Thanks.
echo ${variable} | grep -q ' '
if [[ $? -eq 0 ]] ; then ...
set -f
set -- $variable
printf "Number of words: %d\n" $#
Using awk -
echo $variable | awk ' { if ( NF > 1 ) print "Yes"; else print "No" } '
Try:
echo $var | wc -w
or,
set $var
echo $#
This works for bash and ksh93
variable="AAA BBB CCC DDD"
if [[ $variable =~ ' ' ]]
then
printf "found space\n"
fi
That may fail if $var contains wildcard characters or if it begins with a hyphen.
More portably (works in any Bourne-type shell):
case $var in *\ *) echo "Found space";; esac
#! /usr/bin/perl
$var="aa bb cc dd";
@arr=split(" ",$var);
print $#arr;
#! /usr/bin/perl
$var="aa bb cc dd";
@arr=split(" ",$var);
print $#arr+1;
echo $var | awk '{print NF}'