将引用作为参数传递给函数
Passing quoted as arguments to the function
我想找到一个可能非常简单的问题的答案:我想传递带空格的引号字符串作为函数的独立参数。
有如下数据文件(举例):
one
two three
four five six
seven
并且有一个具有 2 个简单功能的脚本:
params_checker()
{
local first_row=""
local second_row=""
local third_row=""
echo "Expected args are:${first_row} ; ${second_row} ; ${third_row}"
echo "All args are:"
for arg in "$@"; do
echo "${arg}"
done
}
read_from_file()
{
local args_string
while read line; do
args_string="${args_string} \"${line}\""
echo "Read row: ${line}"
done < ./test_input
params_checker ${args_string}
}
read_from_file
换句话说,我想从文本文件中获取行作为函数 params_checker 的参数(文件中的每一行作为不同的参数,我需要保留空格在行中)。尝试用引号 "substrings" 组合字符串失败,输出为:
~/test_sh$ sh test_process.sh
Read row: one
Read row: two three
Read row: four five six
Read row: seven
Expected args are:"one" ; "two ; three"
All args are:
"one"
"two
three"
"four
five
six"
"seven"
预期为 $1="one"、$2="two three"、$3="four five six" ...
在传递给 params_checker 期间引用 ${args_string} 给出了另一个结果,字符串作为单个参数传递。
您能否帮助找出如何将此类带有空格的字符串作为不同的独立函数参数从文件中传递的正确方法?
非常感谢您的帮助!
给你,这应该给你你要找的东西:
#!/bin/bash
params_checker()
{
local first_row=""
local second_row=""
local third_row=""
local forth_row=""
echo "Expected args are: ${first_row} ; ${second_row} ; ${third_row} ; ${forth_row}"
echo "All args are:"
for i in "$@"
do
echo "$i"
done
}
read_from_file()
{
ARRAY=()
while read line; do
echo "Read row: ${line}"
ARRAY+=("$line")
done < ./test_input
params_checker "${ARRAY[@]}"
}
read_from_file;
在 BASH 中应该可以正常工作。如果您的文件名为 test.sh,您可以 运行 像这样 ./test.sh
在 bash/ksh/zsh 中,您将使用数组。在sh
中,可以使用参数“$1”、“$2”等:
read_from_file()
{
set -- # Clear parameters
while read line; do
set -- "$@" "$line" # Append to the parameters
echo "Read row: ${line}"
done < ./test_input
params_checker "$@" # Pass all parameters
}
我想找到一个可能非常简单的问题的答案:我想传递带空格的引号字符串作为函数的独立参数。
有如下数据文件(举例):
one
two three
four five six
seven
并且有一个具有 2 个简单功能的脚本:
params_checker()
{
local first_row=""
local second_row=""
local third_row=""
echo "Expected args are:${first_row} ; ${second_row} ; ${third_row}"
echo "All args are:"
for arg in "$@"; do
echo "${arg}"
done
}
read_from_file()
{
local args_string
while read line; do
args_string="${args_string} \"${line}\""
echo "Read row: ${line}"
done < ./test_input
params_checker ${args_string}
}
read_from_file
换句话说,我想从文本文件中获取行作为函数 params_checker 的参数(文件中的每一行作为不同的参数,我需要保留空格在行中)。尝试用引号 "substrings" 组合字符串失败,输出为:
~/test_sh$ sh test_process.sh
Read row: one
Read row: two three
Read row: four five six
Read row: seven
Expected args are:"one" ; "two ; three"
All args are:
"one"
"two
three"
"four
five
six"
"seven"
预期为 $1="one"、$2="two three"、$3="four five six" ... 在传递给 params_checker 期间引用 ${args_string} 给出了另一个结果,字符串作为单个参数传递。
您能否帮助找出如何将此类带有空格的字符串作为不同的独立函数参数从文件中传递的正确方法?
非常感谢您的帮助!
给你,这应该给你你要找的东西:
#!/bin/bash
params_checker()
{
local first_row=""
local second_row=""
local third_row=""
local forth_row=""
echo "Expected args are: ${first_row} ; ${second_row} ; ${third_row} ; ${forth_row}"
echo "All args are:"
for i in "$@"
do
echo "$i"
done
}
read_from_file()
{
ARRAY=()
while read line; do
echo "Read row: ${line}"
ARRAY+=("$line")
done < ./test_input
params_checker "${ARRAY[@]}"
}
read_from_file;
在 BASH 中应该可以正常工作。如果您的文件名为 test.sh,您可以 运行 像这样 ./test.sh
在 bash/ksh/zsh 中,您将使用数组。在sh
中,可以使用参数“$1”、“$2”等:
read_from_file()
{
set -- # Clear parameters
while read line; do
set -- "$@" "$line" # Append to the parameters
echo "Read row: ${line}"
done < ./test_input
params_checker "$@" # Pass all parameters
}