Reputation: 59
Read more than one value in variable space seprated
I have a variable that stores output of command. Sometimes output of command is single word and sometimes output command is having multiple words with space separated
Example
MAC= 00:1c:c4:5b:6e:fe 00:1c:c4:5b:6e:fe 00:1c:c4:5b:6e:fe 00:1c:c4:5b:6e:fe
echo $MAC
I want to use if statement like below
if $mac has more than one word then echo not ok else echo ok
Thanks in advance
Upvotes: 0
Views: 771
Reputation: 123448
Try:
if [[ "$MAC" =~ "\s" ]]; then echo not ok; else echo ok; fi
The following would yield the string before the first occurrence of (whitespace):
address=${MAC%% *}
Upvotes: 1
Reputation: 5789
If you want to simply determine if there are spaces in $MAC, you can do:
case $MAC in
*\ * )
echo "not ok"
;;
*)
echo "ok"
;;
esac
Upvotes: 1
Reputation: 195029
you should not have the space after the MAC=
.
try
awk '{print (NF>1?"not ":"")"ok"}' <<<$MAC
e.g.
kent$ MAC="00:1c:c4:5b:6e:fe 00:1c:c4:5b:6e:fe 00:1c:c4:5b:6e:fe 00:1c:c4:5b:6e:fe"
kent$ awk '{print (NF>1?"not ":"")"ok"}' <<<$MAC
not ok
Upvotes: 1