Mario
Mario

Reputation: 2453

Database access in Bash Shell scripting

I am new to shell scripting world.

I wrote a bash shell script.

Name of the script is new.sh

Select T1.date as dateFact,
T2.idmfg as idMfg,
T1.id as userId,
FROM Table1 T1
JOIN 
Table2 T2
ON 
T1.id=T2.id 
WHERE 
T1.date ='${Date}'
T2.idmfg='${idMfg}'; 

While running new.sh, I want to enter Date and idMfg manually. E.g.

sh new.sh -d 2013-03-20 -i 201

Where, Date is 2013-03-20 and idMfg is 201.

How can I do it?

Thank you in advance.

Upvotes: 0

Views: 2614

Answers (2)

Gilles Quénot
Gilles Quénot

Reputation: 185053

#!/bin/bash

Date="$1"
idMfg="$2"

mysql<<EOF
SELECT T1.date AS dateFact, T2.idmfg AS idMfg, T1.id AS userId
FROM Table1 T1
JOIN Table2 T2
ON T1.id=T2.id 
WHERE T1.date = "${Date}", T2.idmfg = "${idMfg}";
EOF

Usage :

./new.sh 2013-03-20 201

To go further if you want named switches, see the tutorial: http://wiki.bash-hackers.org/howto/getopts_tutorial Examples: http://mywiki.wooledge.org/BashFAQ/035

Upvotes: 4

suspectus
suspectus

Reputation: 17258

 new.sh 2013-03-20 201

Inside the script the first (date) param is $1, the second $2.

Upvotes: 0

Related Questions