Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 3 down vote accepted
#!/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

share|improve this answer
    
+1 for the script, but he never mentioned mysql :) –  tink Mar 21 '13 at 22:44
    
How can I put it inside case e.g. case $Option in d) date="${date}" i) idmfg="${idMfg}" esac; so I can run as: new.sh -d 2013-03-20 -i 201 –  Mario Mar 21 '13 at 23:49
    
Use the getopts tutorial mentioned in the answer –  dtmilano Mar 22 '13 at 5:44
    
@tink : MySQL is just an example ;) –  sputnick Mar 23 '13 at 3:49
 new.sh 2013-03-20 201

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.