为什么这段代码无法运行?
文件是存在的(即使文件不存在,脚本也应该能够正常工作),而且我有权限。此命令在终端上和硬编码在脚本中都可以正常工作。失败的原因与"$1"相关。
附注:我知道有其他方法来追加文件。我现在一直在使用它们,但我仍然想修复这个代码或者至少知道为什么它不起作用。
#!/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"相关。
附注:我知道有其他方法来追加文件。我现在一直在使用它们,但我仍然想修复这个代码或者至少知道为什么它不起作用。
~
:test=~"/.test"
(用于需要引用空格的情况)。 - chepnertest=~/'.test'
可以。 - chorobaman bash
的第一段中有波浪号展开的解释。 - choroba