从自定义文件中读取程序的参数并使用此参数运行该程序
全部!
我需要从由以下字符串组成的文件“data”中读取参数:
-a -camb="1 0.5 1",diff="1 0 0" -q=5
-a -camb="0 1 0" -p -q -f=10
...
接下来,该参数必须传递给脚本中的程序 ./test:
#!/bin/bash
while read line
do
./test "$line"
done < "./data"
问题是“$line”作为 argv[1] 传递给./test,而不是 argv[1]、argv[2]、argv[3] 的序列
如何将字符串行拆分为多个参数?即 ./test 必须采用 argv[1]、argv[2] 等等?
请注意, -camb="1 0.5 1",diff="1 0 0" 必须作为整个参数 argv[2]!
all!
I need to read arguments from a file 'data' that consists of strings like:
-a -camb="1 0.5 1",diff="1 0 0" -q=5
-a -camb="0 1 0" -p -q -f=10
...
Next, that arguments must be passed to a program ./test within a script:
#!/bin/bash
while read line
do
./test "$line"
done < "./data"
the problem is that "$line" is passed as argv[1] to ./test, and not as a sequence of argv[1], argv[2], argv[3]
How can I split the string line to several arguments? I.e. the ./test must takes argv[1], argv[2], and so?
Note, that -camb="1 0.5 1",diff="1 0 0" must be as whole argument, argv[2]!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 eval 来实现此目的:
但是,这里有一个很大的警告:eval 对文件内容的解释可能比您想要的更多。例如,如果它包含任何 I/O 重定向(例如
>somefile
),它们将被应用。类似地,$variable
将被替换,; somecommand
将作为单独的命令执行,等等。基本上,如果数据文件的内容不够干净,您可能会得到一些意想不到的和潜在危险的结果。You can use eval for this:
There's a big warning here, however: eval may do more interpretation of the file contents than you want. For example, if it contains any I/O redirects (e.g.
>somefile
), they will be applied. Similarly,$variable
will be substituted,; somecommand
will be executed as a separate command, etc. Basically, if the contents of the data file aren't clean enough, you can get some unexpected and potentially dangerous results.引号是字面意思,而不是句法,这意味着它们的处理方式与外壳。但是您可以通过将它们设置在数组中来处理它们:
PS: 不要使用
eval
。The quotes are literal, not syntactic, which means they won't be handled the same way as on the shell. But you can handle them by setting them in an array:
PS: Don't use
eval
.