这个问题在这里已经有了答案:
tilde expansion in environment variable
(2 个回答)
7年前关闭。
我有以下脚本
file=${file:-letsdump.sh}
checkfile="~/mysql_backup/$file"
if [ -e "$checkfile" ]; then
mv ~/mysql_backup/$file ~/mysql_backup/$file-bak-$(date +%d%m%Y_%H%M).bak
else
echo 'file doesnt exist'
fi
我知道我的文件在那里,我想这可能与 ~/?
它永远找不到文件
最佳答案
双引号(真的,任何引号)阻止 ~
从被扩大。改用:
checkfile=~/mysql_backup/"$file"
...或者,甚至更好,使用
$HOME
在所有脚本中,并考虑 ~
成为仅供交互/人类使用的设施。
关于shell - 波浪号 (~/) 无法处理 Shell 脚本中的 if then 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27238491/