bash 使用空格将值/参数传递给 Shell 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13884554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing a values/arguments to Shell Script with spaces
提问by Driver123
Below is a shell script for taking 2 input parameter values,
下面是一个用于获取 2 个输入参数值的 shell 脚本,
asd#@#g#@#h#@#j@#@kcandidateid
asd#@#g#@#h#@#j@#@k候选人编号
which gives us output as
这给了我们输出
string0 asd
string1 g
string2 h
.
.
.
candidateid
.
.
.
(& then both parameters are used in Oracle queries)
(& 然后这两个参数都用在 Oracle 查询中)
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
例如: /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=
input1=
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
回答by Barmar
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 shexplicitly, the #!/bin/bashline in the script tells the OS to run bash.
此外,您不需要sh明确指定,#!/bin/bash脚本中的行告诉操作系统运行 bash。
回答by Rubens
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.
作为补充,在任何编程语言中,使用数组或将值映射到容器(在 bash 的情况下使用关联数组)比创建数十个变量要有用得多。
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.
抱歉; 正如 Barmar 所指出的那样,我误读了这篇文章,我提出了一些确实改变了程序执行的内容。事实是,如果问题出在命令行参数上,那么您必须将双引号括起来;这就是读取参数的方式。
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='#'.
但是,您可以将所有参数读入一个数组,然后IFS像您一样更改。请注意,您的参数必须由手头已知的内容以及您对IFS='#'.

