我正在编写一个bash脚本,我需要将包含空格的字符串传递给我的bash脚本中的函数.
例如:
#!/bin/bash
myFunction
{
echo $1
echo $2
echo $3
}
myFunction "firstString" "second string with spaces" "thirdString"
Run Code Online (Sandbox Code Playgroud)
运行时,我期望的输出是:
firstString
second string with spaces
thirdString
Run Code Online (Sandbox Code Playgroud)
但是,实际输出的是:
firstString
second
string
Run Code Online (Sandbox Code Playgroud)
有没有办法将带空格的字符串作为单个参数传递给bash中的函数?
gho*_*g74 158
你应该加上引号,而且你的函数声明是错误的.
myFunction()
{
echo "$1"
echo "$2"
echo "$3"
}
Run Code Online (Sandbox Code Playgroud)
和其他人一样,它对我也有用.告诉我们您正在使用的shell版本.
小智 17
上述问题的另一个解决方案是将每个字符串设置为变量,使用由文字美元符号表示的变量调用该函数\$.然后在函数use eval中读取变量并按预期输出.
#!/usr/bin/ksh
myFunction()
{
eval string1="$1"
eval string2="$2"
eval string3="$3"
echo "string1 = ${string1}"
echo "string2 = ${string2}"
echo "string3 = ${string3}"
}
var1="firstString"
var2="second string with spaces"
var3="thirdString"
myFunction "\${var1}" "\${var2}" "\${var3}"
exit 0
Run Code Online (Sandbox Code Playgroud)
输出是:
string1 = firstString
string2 = second string with spaces
string3 = thirdString
Run Code Online (Sandbox Code Playgroud)
在尝试解决类似的问题时,我遇到了UNIX的问题,我的变量是空间分隔的.我试图将管道分隔的字符串传递给函数,awk用于设置稍后用于创建报告的一系列变量.我最初尝试了ghostdog74发布的解决方案,但无法让它工作,因为并非我的所有参数都在引号中传递.在为每个参数添加双引号后,它开始按预期运行.
下面是我的代码的before状态,并且在状态之后完全正常运行.
之前 - 非功能代码
#!/usr/bin/ksh
#*******************************************************************************
# Setup Function To Extract Each Field For The Error Report
#*******************************************************************************
getField(){
detailedString="$1"
fieldNumber=$2
# Retrieves Column ${fieldNumber} From The Pipe Delimited ${detailedString}
# And Strips Leading And Trailing Spaces
echo ${detailedString} | awk -F '|' -v VAR=${fieldNumber} '{ print $VAR }' | sed 's/^[ \t]*//;s/[ \t]*$//'
}
while read LINE
do
var1="$LINE"
# Below Does Not Work Since There Are Not Quotes Around The 3
iputId=$(getField "${var1}" 3)
done<${someFile}
exit 0
Run Code Online (Sandbox Code Playgroud)
After - 功能代码
#!/usr/bin/ksh
#*******************************************************************************
# Setup Function To Extract Each Field For The Report
#*******************************************************************************
getField(){
detailedString="$1"
fieldNumber=$2
# Retrieves Column ${fieldNumber} From The Pipe Delimited ${detailedString}
# And Strips Leading And Trailing Spaces
echo ${detailedString} | awk -F '|' -v VAR=${fieldNumber} '{ print $VAR }' | sed 's/^[ \t]*//;s/[ \t]*$//'
}
while read LINE
do
var1="$LINE"
# Below Now Works As There Are Quotes Around The 3
iputId=$(getField "${var1}" "3")
done<${someFile}
exit 0
Run Code Online (Sandbox Code Playgroud)
Rai*_*rim 13
我迟到了 9 年,但更有活力的方式是
function myFunction {
for i in "$*"; do echo "$i"; done;
}
Run Code Online (Sandbox Code Playgroud)
解决此问题的最简单方法是\"在运行shell脚本时只需要使用空格分隔的参数:
#!/bin/bash
myFunction() {
echo $1
echo $2
echo $3
}
myFunction "firstString" "\"Hello World\"" "thirdString"
Run Code Online (Sandbox Code Playgroud)
你对myFunction的定义是错误的.它应该是:
myFunction()
{
# same as before
}
Run Code Online (Sandbox Code Playgroud)
要么:
function myFunction
{
# same as before
}
Run Code Online (Sandbox Code Playgroud)
无论如何,它看起来很好,并在Bash 3.2.48上适合我.