1
#usernamecheck.sh 

#!/bin/bsh
DBUSER=user_1
DBPASSWORD=XXXXX
DBSOURCENAME=database_1
DBNAME=database_1
DBNAME1=snapshot_v
DBSERVER=X.X.X.X
DBCONN="-h ${DBSERVER} -u ${DBUSER} --password=${DBPASSWORD}"

echo "select user from device_id where ip='1.1.1.1'|mysql $DBCONN $DBNAME

The output of executing this script is:

user
----
JohnDoe

The above bash script which gives me user name of IP (1.1.1.1). I would like to pass the IP as a command line argument. I tried,

echo "select user from device_id where ip=$1|mysql $DBCONN $DBNAME

and executed like

$./usernamecheck.sh '1.1.1.1'

I get the error message:

Error in SQL syntax

Hope i'm passing the command line argument correct. But not sure, why does this error pop up?

1 Answer 1

2

If you use a query, you say:

WHERE ip='1.1.1.1'

What you are currently saying is:

WHERE ip=$1

which gets translated to

WHERE ip=1.1.1.1

So you are missing a quote around $1 to make it work:

WHERE ip='$1'
#        ^  ^

All together:

echo "select user from device_id where ip='$1' | mysql $DBCONN $DBNAME
#                                         ^  ^
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks mate. Worked perfect !

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.