0

I’ve one shell script and inside that script am using one sql.

For example:

Select * from Table where city IN ( ‘Pais’, ‘london’, ‘Sydney’) 

My doubt is if I want to pass N number of parameters to my shell script so that all could set within IN clause, then how it can be done.

May I know please how can I achieve this? With limited number of parameters I have done but this is new scenario to me.

Updated:

Actual Script

#! /bin/bash echo ID, CITY, SALARY, ACCT_NUM, DATE, AMOUNT > /home/joy/Apple/SMMRY_RESULT.csv m_db unload /home/joy/IPC/target/send/sendFile.dbc -column_delimeter ',' -select "Select * from Table where CITY IN ( ‘Paris’, ‘london’, ‘Sydney’);" >> /home/joy/Output/Output_RESULT.csv var=$? --Line 1 echo ID, CITY, SALARY, ACCT_NUM, DATE, AMOUNT > /home/joy/Apple/SMMRY_RESULT.csv m_db unload /home/joy/IPC/target/send/sendFile.dbc -column_delimeter ',' -select "Select * from Table where DATE IN ( ‘2019-02-01’, '2019-02-02,' '2019-02-03');" >> /home/joy/Output/Output_RESULT.csv var1=$? -- Line 2 if [["$var"==0 $$ "$var1==="]]; echo "" | mailx -a "/home/joy/Output/Output_RESULT.csv" -a " /home/joy/Output/Output_RESULT.csv" -s "IPC DATA Output" '[email protected]' > /dev/null then echo "Script Success and Mail Sent" exit 0 else echo "Script Failed" exit 1 fi 

Questions:

1) How to give one parameter( clubbing all Dates or Cities at once) to script while running so that It could pass as one value comprises of all dates or cities where all values are enclosed with single quote and comma separated ?

For example sh myScript.sh "AllDatesAsFirstParameter" "AllCitiesAsSecondParameter" 

2) How can I restrict my Date parameter with specific pattern else shows error ?

The one I've tried below (Just as an example)

#! /bin/bash echo "My Value: $1" echo "My Second Value: $2" sh myScript.sh One Two 

Thanks

6
  • With limited number of parameters How did you do it? Commented Feb 8, 2019 at 15:32
  • Please post your current script or at least parts of it, so we can actually see how you've been trying to do it. Commented Feb 8, 2019 at 15:35
  • @KamilCuk For example for three parameters I’ve created $1, $2 and $3 in IN clause and passing values for these three parameters to script while executing. Commented Feb 8, 2019 at 15:37
  • this question completely lacks the context, how this query is even being executed, therefore impossible to answer in a reliable manner. Commented Feb 8, 2019 at 16:28
  • I’ve created $1, $2 and $3 in IN - exactly how? post the actual code. Believe me, it will help a lot. Commented Feb 8, 2019 at 20:28

2 Answers 2

1

I interpret the question as meaning that you want to expand the list of parameters of the form Paris London Sydney into the comma-delimited enquoted string 'Paris', 'London', 'Sydney'. You could do:

unset sep query='Select * from Table where city IN (' for x; do query="$query$sep'$x'"; sep=', '; done query="$query)" echo "$query" 

Basically, this just iterates over all the positional parameters, appending each to the end of the query surrounded by single quotes.

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

1 Comment

Question Updated, please check ..!
0

You can access the list of all passed command line arguments at once by refering to it with $@. This works with an arbitrary amount of arguments

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.