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' ];
1
  • 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" ]. Commented Apr 29, 2016 at 8:01

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.