简单 bash 重定向中的“没有这样的文件或目录”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15600469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:58:15  来源:igfitidea点击:

"No such file or directory" on simple bash redirection

bashio-redirection

提问by ACarter

Here is some code of mine:

这是我的一些代码:

gzip -c $path > /var/www/wiki/backup/$now/$file.gz

I'm gzipping the contents of $path(the path to a directory), and then sending the compressed file to /var/www/wiki/backup/$now/$file.gz. $nowcontains a directory name, $fileis the name I want to write the compressed file to.

我正在$path压缩(目录路径)的内容,然后将压缩文件发送到/var/www/wiki/backup/$now/$file.gz. $now包含一个目录名,$file是我想将压缩文件写入的名称。

However, on running the program, I get this error:

但是,在运行程序时,我收到此错误:

backup.sh: line 20: /var/www/wiki/backup/Sunday/extensions.gz: No such file or directory
                                         ^$now    ^$file

(line 20 is the line given above)

(第 20 行是上面给出的行)

Why is the program breaking? I know Sunday/extensions.gzdoesn't exist (although Sunday does), that's why I'm asking you to write to it!

为什么程序会中断?我知道Sunday/extensions.gz不存在(虽然星期天确实存在),这就是为什么我要你写信给它!

Full program code:

完整程序代码:

#!/bin/bash

now=$(date +"%A")
mkdir -p /var/www/wiki/backups/$now

databases=(bmshared brickimedia_meta brickimedia_en brickimedia_customs)
locations=("/var/www/wiki/skins" "/var/www/wiki/images" "/var/www/wiki/")

for db in ${databases[*]}
do
#command with passwords and shoodle
:
done

for path in ${locations[*]}
do
#echo "" > var/www/wiki/backup/$now/$file.gz
file=`echo $path | cut -d/ -f5`
echo $path
gzip -c $path > /var/www/wiki/backup/$now/$file.gz
done

采纳答案by suspectus

The directory created is backups, the gzip is to backup.

创建的目录是备份,gzip 是备份

mkdir -p /var/www/wiki/backups/$now

gzip -c $path > /var/www/wiki/backup/$now/$file.gz

回答by glenn Hymanman

One of your locations is "/var/www/wiki/". Then you have

您的位置之一是“/var/www/wiki/”。那么你有

file=`echo $path | cut -d/ -f5`
gzip -c $path > /var/www/wiki/backup/$now/$file.gz

Since $filecontains the empty string, you're attempting to write to /var/www/wiki/backup/Sunday/.gz. That's a problem but it's not the error you're reporting.

由于$file包含空字符串,您正在尝试写入/var/www/wiki/backup/Sunday/.gz. 这是一个问题,但这不是您报告的错误。

When I try to gzip a directory, I get this error

当我尝试 gzip 目录时,出现此错误

$ gzip -c ./subdir/ > subdir.gz
gzip: ./subdir/ is a directory -- ignored

That's a problem but it's not the error you're reporting.

这是一个问题,但这不是您报告的错误。

@suspectus solved your reported problem.

@suspectus 解决了您报告的问题。