0

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
19
  • What you mean by "But the above parameter that i am getting is without qoutes"? How are you invoking the script that you cannot add the quotes around it? Commented Dec 14, 2012 at 18:52
  • the above code needs 2 parameter to execute. Commented Dec 14, 2012 at 19:00
  • If you want to provide a parameter with spaces, you have to quote it. Commented Dec 14, 2012 at 19:05
  • 1
    @Venkatesh as I said in my edit, you must either add the quotes, or find an adequate separator to your command line arguments Commented Dec 14, 2012 at 19:27
  • 1
    @Venkatesh what I mean is that, if you really cannot use quotes in your command line call, you must determine a new separator; considering between guava & #@#grape as your string, you must be able to say, either that after each pair of whitespaces you'll have an argument, or that your arguments are separated by the &, which would give you arg1=between guava and arg2= #@#grape Commented Dec 14, 2012 at 19:37

2 Answers 2

2

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.

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

6 Comments

You many need to specify sh or bash in case the file does not have execute permissions
@another.anon.coward Fix the permissions.
@Barmer: yes barmer. i am already aware of this one. but i cant put quotes around the filename. i am getting the argument from a tool which is just passing me the file location. because i dont have the control do it. I can just pass watever i am getting from the tool.
Ofc :) (... Probably make little sense to have a shell script without execute permission )
How are you getting it from the tool? Is it in a variable? Then put double quotes around the variable. Or is your tool running your script ? In that case, it's what I said above -- the tool has a bug if it doesn't quote the arguments, because there's no way for your script to tell that a filename with spaces should be just one argument.
|
1

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='#'.

5 Comments

u really arent gettin my point. try executin the code first & then put your views please. Its a run time argument. It still breaks the the argument wherever it gets spaces.
would you mind to point out what is the first code in your question? people are here to help, not to find out your intentions from within your posts
Quotes aren't needed in assignment statements like this, so it won't fix it.
@rubens: i am getting the parameters from a tool. & i am running script .... simply ... sh script.sh parameter1 parameter2. here parameter1 consists of spaces while parameters doesn't.
thanks, Barmar, I noticed my mistake; Venkatesh, i've made an edit, please, check it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.