0

Q1.我想从文件中读取命令的位置参数。意味着 some_command 位置参数...我该怎么做?

Q2. what is this line means :
    echo $(0<file_name)
   i can echo contents of file name using this command but when
   i do echo 0<file_name it does nothing kindly clear my doubt.
4

3 回答 3

1

您可以使用以下命令读取位置参数:

set -- $(<file_name)

...其中$(<xyz)$(0<xyz)是等效的bashism:

set -- `cat xyz`
于 2013-09-03T04:23:16.897 回答
0

A1)getopts按照 BashFAQ 的建议使用

A2)$()执行命令。与“``”相同

于 2013-09-03T04:15:56.540 回答
0

A1:在您的脚本中放置这个以根据文件的内容设置您的位置参数:

read -rd '' -a R < filename  ## Reads input from file, and splits it with the values from IFS ($' \t\n'). This doesn't only read a line. With no delimeter (-d ''), it reads the whole input.
set -- "${R[@]}"  ## "${R[@]}" will expand to multiple arguments depending on the values of the array R. This is just the common way to transfer the contents of an array to the positional parameters.

$()对于和之类的格式$(<file_input),扩展的值也会受到分词的影响,但可能会因路径名扩展而改变。noglob要防止路径名扩展,请使用( set -o noglob, )禁用它set -f,或者只使用read -a.

A2:根据 bash 手册:命令替换 $(cat file) 可以替换为等效但更快的 $(< file)。. 它前面的可选数字,例如 0,似乎是自定义输入文件描述符,实际上似乎并没有什么区别:echo "$(<file)"而且echo "$(40<file)"是一样的。

但是当我这样做时echo 0<file_name,它什么也没做

它不会,因为这已经是从file_name. 并且echo只根据其参数或参数发送输出echo arg1 arg2 ...,而不是像 cat: 这样的输入cat < file_name

笔记:

  • 建议您只使用一个参数 for echo,并在 doublequotes 内连接您的字符串""
  • 0<<与默认输入使用 fd 0 相同。
于 2013-09-03T05:23:32.630 回答