简体   繁体   中英

Pass N arguments in shell script

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" 'kellyIPC@outlook.com' >
/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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM