【发布时间】:2013-05-13 10:33:13
【问题描述】:
我是 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 我不知道为什么会这样!你看到错误了吗?谢谢!
【问题讨论】:
-
如果在您的主目录中运行,此脚本将首先
rm指定文件,然后无法链接到不存在的文件。if [[ $PWD == $HOME ]] ; then echo "error message"; exit 1; fi应该可以帮助您避免这种情况。
标签: bash macos shell variable-expansion tilde-expansion