bash 脚本错误 : file or directory doesnt exist

标签 bash shell ubuntu ubuntu-14.04

我正在使用 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/

相关文章:

ubuntu - Nginx:Ubuntu 上的 nginx 权限被拒绝

ubuntu - 在 xubuntu 20.02 上使用证书文件时 mosquitto_sub 失败并显示 "Error: Problem setting TLS options: File not found."

bash - 目录 * 和文件顺序

git - Hook /接收后 : Syntax error: "(" unexpected (expecting "done")

linux - 如果用户突然关闭 ssh session ,如何从 bash 脚本中获知

shell - Gradle:无法使用shell命令初始化变量

bash - 如何通过在 ubuntu 中包含 "cd"命令来运行 shell 脚本?

java - 从 Java 执行源命令

linux - AWK - 显示列包含特定字符串的行

bash - 如何在大量远程服务器上运行快速脚本?