我是 macosx 上 shell 编程的新手,遇到了一些小问题。我编写了以下 shell 脚本:
#!/bin/sh
function createlink {
source_file=$1
target_file="~/$source_file"
if [[ -f $target_file ]]; then
rm $target_file
fi
ln $source_file $target_file
}
createlink ".netrc"
当我执行这个脚本时,我收到消息 ln: ~/.netrc: No such file or directory 我不知道为什么会这样!你看到错误了吗?谢谢!
最佳答案
问题是 波浪号扩展
不会发生,因为路径在变量值中(波浪号扩展
发生在 变量扩展
之前)。您可以使用 $HOME
而不是 ~
来改善这个问题。也就是
target_file="${HOME}/${source_file}"
这应该可以解决您的问题。
进一步阅读:man bash
的 EXPANSION
部分
关于bash - ln 提示没有这样的文件或目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16622899/