Arun
Arun

Reputation: 1220

Bash : Command line argument issue

#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?

Upvotes: 1

Views: 37

Answers (1)

fedorqui
fedorqui

Reputation: 289535

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
#                                         ^  ^

Upvotes: 2

Related Questions