【发布时间】:2016-04-14 12:52:54
【问题描述】:
为什么这段代码不起作用?
#!/bin/bash
test="~/.test"
function fn_append () {
echo "check vars: $1 ··· ${1} ··· $2"
echo "check comm: echo \"$2\" >> $1"
#this returns "No such file or directory"
echo $2 >> $1
echo $2 >> ${1}
echo $2 >> "$1"
#this creates file named $1
echo $2 >> '$1'
#this works (but it isn't enough)
echo $2 >> ~/.test
#this is the command Im trying to create.
#echo "alias ll='ls -lstra'" >> ~/.test
}
fn_append ${test} "alias ll='ls -lstra'"
执行输出如下:
check vars: ~/.test ··· ~/.test ··· alias ll='ls -lstra'
check comm: echo "alias ll='ls -lstra'" >> ~/.test
./test.sh: line 9: ~/.test: No such file or directory
./test.sh: line 10: ~/.test: No such file or directory
./test.sh: line 11: ~/.test: No such file or directory
该文件确实存在(尽管脚本无论如何都不能工作)并且我有权限。该命令适用于终端并在脚本上硬编码。失败的是与“$1”相关的东西。
P.D:我知道还有其他附加文件的方法。我现在一直在使用它们,但我仍然想修复此代码,或者至少知道它为什么不起作用。
【问题讨论】: