简体   繁体   中英

Passing a values/arguments to Shell Script with spaces

Below is a shell script for taking 2 input parameter values,

  1. asd#@#g#@#h#@#j@#@k

  2. candidateid

which gives us output as

string0 asd
string1 g
string2 h 
.
.
.

candidateid

.
.
.

(& then both parameters are used in Oracle queries)

Now the problem is that the above code fails when I try to pass 1st parameter with spaces.

eg: /TOM/Process Folders/System Drive/a.jpg

The above given location should be considered as 1st string. If I give the above within Double Quotes, then it works fine. But the above parameter that I am getting is without quotes.

#!/bin/bash
input=$1

input1=$2

IFS='#' read -a arr <<< "${input//#@#/#}"

for((i=0;i<${#arr[@]};i++))
do
        echo "String$i ${arr[i]}"
done

read passportphotos <<< "${arr[0]}"
read academiccertificates <<< "${arr[1]}"
read dateofbirth <<< "${arr[2]}"
read addressproof <<< "${arr[3]}"
read pancard <<< "${arr[4]}"
read pfnominationform <<< "${arr[5]}"
read gratuitynomination <<< "${arr[6]}"
read investmentdeclaration <<< "${arr[7]}"
read resignationletter <<< "${arr[8]}"
read acceptanceoffer <<< "${arr[9]}"
read acceptancecodeofconduct <<< "${arr[10]}"
read medicalnomination <<< "${arr[11]}"
read backgroungverification <<< "${arr[12]}"
read personaldataform <<< "${arr[13]}"

echo $passportphotos
echo $academiccertificates
echo $dateofbirth
echo $addressproof
echo $pancard
echo $pfnominationform
echo $gratuitynomination
echo $investmentdeclaration
echo $resignationletter
echo $acceptanceoffer
echo $acceptancecodeofconduct
echo $medicalnomination
echo $backgroungverification
echo $personaldataform

instant_client="/root/ora_client/instantclient_11_2"
view=`$instant_client/sqlplus -s HRUSER/HRUSER@TOMLWF <<EOF

set heading off

set feedback off

set lines 10000

set pagesize 10000


insert into EMPLOYEEDOCUMENTS VALUES ((SELECT EMPLOYEEID FROM EMPLOYEE WHERE CANDIDATEID='$input1'),'Resume','Doc','$passportphotos','Y','HR',(SELECT SYSDATE FROM DUAL),'HR',(SELECT SYSDATE FROM DUAL),'HR',(SELECT SYSDATE FROM DUAL));
`

echo $view

Invoke your script this way:

delimiter_new.sh 'company_home/TOM/Proc_joingchecklist_test/Process Instance Documents/Instance.jpg' 14492

You need to put quotes around the filename so it will be treated as a single argument.

Also, you don't need to specify sh explicitly, the #!/bin/bash line in the script tells the OS to run bash.

I guess you did not really have to post all of that code to say you're in trouble with spaces in an argument.

As a complement, in any programming language, it's rather much more useful to use an array, or to map values into a container — using an associative array, in case of bash — than creating dozens of variables.

Edit :

I'm sorry; as pointed by Barmar, I misread the post, and I presented something that does really changes not the execution of your program. Fact is that, if the problem is with the command line argument, then you must include double quotes wrapping it; this is how the arguments are read.

You can, though, read all the arguments into an array, and then change the IFS , just as you did. Pay attention to the fact that your arguments must be separated by something already known at hand, as well as in your usage of IFS='#' .

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