compare “n” number of variables

An easy way to compare “n” number of variables in bash, precisely to find if all of them are equal or not, can be easily done in the following way:

while [ $i -lt 10 ]; do
Sequence[$i]=whatever sequence you want
let i=$i+1
done

for i in ${Sequence[*]}; do
[ "${Sequence[0]}" = "$i" ]
j=$(( $? + j ))
done

if [ $j -eq 0 ];
echo="they are all the same"
fi

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s