Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 使用php中的shell将文本追加到文件中_Bash_Shell_Ubuntu_Quoting - Fatal编程技术网

Bash 使用php中的shell将文本追加到文件中

Bash 使用php中的shell将文本追加到文件中,bash,shell,ubuntu,quoting,Bash,Shell,Ubuntu,Quoting,我试图使用shell脚本将传递的文本作为参数附加到文件中 这是壳牌公司的代码 echo $1>>/etc/freeradius/mods-enabled/ldap 这个shell将获得文本以将其添加到/etc/freeradius/mods-enabled-path中的文件ldap中,我用这种格式称这个shell # sh /etc/append.sh hello how are you man 但在本例中,shell仅获取第一个单词“hello”并将其附加到文件中。我如何告

我试图使用shell脚本将传递的文本作为参数附加到文件中 这是壳牌公司的代码

 echo $1>>/etc/freeradius/mods-enabled/ldap
这个shell将获得文本以将其添加到/etc/freeradius/mods-enabled-path中的文件ldap中,我用这种格式称这个shell

# sh /etc/append.sh hello how are you man 

但在本例中,shell仅获取第一个单词“hello”并将其附加到文件中。我如何告诉shell所有单词都是相同的变量,并且应该插入到文件中

这取决于您使用的shell。
bash
中,此脚本应该可以工作:

#!/bin/bash  
echo "${@:1}">>/etc/freeradius/mods-enabled/ldap  
编辑:正如DTSCode在注释中指出的那样,脚本中的
:1
部分是多余的,因此这将更加正确:

#!/bin/bash
echo "$@" >> /etc/freeradius/mods-enabled/ldap  
然后授予执行文件和调用的权限

/etc/append.sh hello how are you man  
或者没有执行权限调用

bash /etc/append.sh hello how are you man

@应该引用:
“${@:1}”
。虽然:1在这里是多余的,所以它实际上应该是
echo“$@”>/etc/…
。(实际上,您可能只需要打印值,而不需要“$*””。@DTSCode很好!我想知道我怎么会漏掉引号(可能是因为我在我的笔记本电脑上运行了一个测试,它在没有引号的情况下也能工作)。将编辑答案。