2

New to bash script, i need to check if the first word in Group equals the second word in Users.

Group=`echo $rules | egrep -v 'Test'`
Users=`echo $rules | grep -i 'Test' | awk '{print substr($0, index($0,$2))}'`

if [ '$Group' -eq '$Users' ];
then
echo $Group
echo $Users

else
:
fi

Can I use something like this or how is this possible?

if [ '$Group $1' -eq '$Users $2' ];

or

if [ '^$Group' -eq '^$Users' ];
John Doe
  • 36
  • 1
  • 3
  • 1
    can you provide some sample input to play with? Also, I guess you need double quotes to have the values expanded --> `if [ "$Group" -eq "$Users" ]`. – fedorqui Apr 29 '16 at 08:01

1 Answers1

4

The operator -eq is used for integer comparison. If you want to compare strings you must use = like this:

if [ "$a" = "$b" ]

which is equivalent to

if [ "$a" == "$b" ]

but NOT equivalent to (note the spaces)

if [ "$a"="$b" ]

However, if you want to use a regular expression (you used ^ which isn't a glob pattern wildcard) in a if statement look here

That said you should do the splitting before the if statement and put Users and Groups in two arrays.

Community
  • 1
  • 1