我需要将一个文件中的多行作为一个以逗号分隔的参数传递给脚本。每当我尝试使用处理文件的输出作为单个字符串时,逗号就会成为分隔符。我该怎么做?
测试用例:
测试用例:
[user@host]$ #Here is a word list, my target words are on lines starting with "1,"
[user@host]$ cat word_list_numbered.txt
1,lakin
2,chesterfield
3,sparkplug
4,unscrawling
5,sukkah
1,girding
2,gripeful
3,historied
4,hypoglossal
5,nonmathematician
1,instructorship
2,loller
3,containerized
4,duodecimally
5,oligocythemia
1,nonsegregation
2,expecter
3,enterrologist
4,tromometry
5,salvia
[user@host]$ #Here is a mock operation, it just outputs the number of args, I want all selected words as one argument
[user@host]$ cat operation.sh
echo "This script has $# arguments"
[user@host]$ #Here is a script that outputs the needed words as comma delimited
[user@host]$ grep '^1,' word_list_numbered.txt | tr -d '1,' | tr '\n' ',' | sed 's/,$//' lakin,girding,instructorship,nonsegregation[user@host]$
[user@host]$ #Here is the operation script receiving that comma delimited list
[user@host]$ ./operation.sh $(grep '^1,' word_list_numbered.txt | tr -d '1,' | tr '\n' ',' | sed 's/,$//')
This script has 4 arguments
[user@host]$ #oops, too many arguments
[user@host]$ ./operation.sh foo,bar
This script has 1 arguments
[user@host]$ ./operation.sh foo bar
This script has 2 arguments
[user@host]$
详细信息:
- 所需单词在以1开头的行中,
- 所有单词应作为一个逗号分隔的参数传递给operation.sh,
- 我无法控制word_list_numbered.txt的格式或operation.sh需要将所有单词作为一个逗号分隔的参数传递的要求,
- 多次运行operation.sh并不是最优解 - 我提出这个问题是为了避免这样做。
$IFS
的值是多少? - swornabsent