我正在使用 Ubuntu 14.04
这是我第一次编写 bash 脚本。
这里是:
#!/bin/bash
${file}="/home/isra/files/mongoTemp.json"
${fdiff}="/home/isra/files/mTempDiff.json"
.
.
commands
.
.
mv ${file} ${fileold}
.
.
commands
.
.
exit
但它给了我这两个错误:
./index.sh: line 4: =$HOME/files/mongoTemp.json: No such file or directory
./index.sh: line 5: =$HOME/files/mTempDiff.json: No such file or directory
mv: missing file operand
couldn't open [$HOME/files/]
老实说,我不确定问题出在哪里。文件确实存在,当它第一次提示它们时我手动创建了它们,但它并没有解决问题,而且
mv
命令不起作用。我究竟做错了什么?
最佳答案
分配给变量时,使用 foo=
, 而不是 $foo=
, 或 ${foo}=
即,行:
${file}="/home/isra/files/mongoTemp.json"
${fdiff}="/home/isra/files/mTempDiff.json"
应该:
file="/home/isra/files/mongoTemp.json"
fdiff="/home/isra/files/mTempDiff.json"
其余的看起来不错。
请注意,前两个错误是由于
${..}=...
线。变量
$file
和 $fdiff
是空的,这会导致 mv
错误 "mv: missing file operand"
.您未显示的某些后续命令会导致最终错误。不过,最有可能的是,通过正确分配变量,一切都将得到解决。
关于bash 脚本错误 : file or directory doesnt exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28760290/