简体   繁体   English

bash脚本和条件语句

[英]bash scripting and conditional statements

I am trying to run a simple bash script but I am struggling on how to incoperate a condition. 我正在尝试运行一个简单的bash脚本,但我正在努力解决如何陷入困境。 any pointers. 任何指针。 the loop says. 循环说。 I would like to incoperate a conditions such that when gdalinfo cannot open the image it copies that particular file to another location. 我想加入一个条件,当gdalinfo无法打开图像时,它会将该特定文件复制到另一个位置。

for file in `cat path.txt`; do gdalinfo $file;done

works fine in opening the images and also shows which ones cannot be opened. 在打开图像时效果很好,并显示哪些图像无法打开。
the wrong code is 错误的代码是

for file in `cat path.txt`; do gdalinfo $file && echo $file; else cp $file /data/temp

Again, and again and again - zilion th again... 一次又一次-zilion th ...

Don't use contsructions like 不要使用类似的结论

for file in `cat path.txt`

or 要么

for file in `find .....`
for file in `any command what produces filenames`

Because the code will BREAK immediatelly, when the filename or path contains space. 因为当文件名或路径包含空格时,代码将立即BREAK。 Never use it for any command what produces filenames. 切勿将其用于产生文件名的任何命令。 Bad practice. 不好的做法。 Very Bad. 很坏。 It is incorrect, mistaken, erroneous, inaccurate, inexact, imprecise, faulty, WRONG. 它是错误的,错误的,错误的,不准确的,不精确的,不精确的,错误的,错误的。

The correct form is: 正确的形式是:

for file in some/*   #if want/can use filenames directly from the filesystem

or 要么

find . -print0 | while IFS= read -r -d '' file

or (if you sure than no filename contains a newline) can use 或者(如果你确定没有文件名包含换行符)可以使用

cat path.txt | while read -r file

but here the cat is useless, (really - command what only copies a file to STDOUT is useless). 但是这里的cat是没用的,(真的 - 命令将文件复制到STDOUT 没用的)。 You should use instead 您应该改用

while read -r file
do
   #whatever
done < path.txt

It is faster (doesn't fork a new process, as do in case of every pipe). 它更快(不会像每个管道一样派生一个新进程)。

The above while s will fill the corect filename into the variable file in cases when the filename contains a space too. 以上while旨意填写corect文件名到变量file的情况下,当文件名包含空格了。 The for will not . for 不会 Period. 期。 Uff. UFF。 Omg. 我的天啊。

And use "$variable_with_filename" instead of pure $variable_with_filename for the same reason. 出于同样的原因,使用"$variable_with_filename"而不是纯$variable_with_filename If the filename contains a white-space any command will misunderstand it as two filenames. 如果文件名包含空格,则任何命令都会误认为它是两个文件名。 This probably not, what you want too.. 这可能不是,你想要的......

So, enclose any shell variable what contain a filename with double quotes. 因此,请将包含文件名的双引号括起来的所有shell变量括起来。 (not only filename, but anything what can contain a space). (不仅是文件名,还包含任何可以包含空格的东西)。 "$variable" is correct. "$variable"是正确的。

If i understand right, you want copy files to /data/temp when the gdalinfo returns error. 如果我理解正确,那么当gdalinfo返回错误时,您希望将文件复制到/data/temp

while read -r file
do
   gdalinfo "$file" || cp "$file" /data/temp
done < path.txt

Nice, short and safe (at least if your path.txt really contains one filename per line). 美观,简短,安全(至少如果path.txt实际上每行包含一个文件名)。

And maybe, you want use your script more times, therefore dont out the filename inside, but save the script in a form 也许,你想要更多次使用你的脚本,因此不要在里面输出文件名,而是将脚本保存在表单中

while read -r file
do
   gdalinfo "$file" || cp "$file" /data/temp
done

and use it like: 并使用它像:

mygdalinfo < path.txt

more universal... 更通用...

and maybe, you want only show the filenames for what gdalinfo returns error 也许,您只想显示gdalinfo返回错误的文件名

while read -r file
do
   gdalinfo "$file" || printf "$file\n"
done

and if you change the printf "$file\\n" to printf "$file\\0" you can use the script in a pipe safely, so: 如果你将printf "$file\\n"更改为printf "$file\\0"你可以安全地使用管道中的脚本,所以:

while read -r file
do
   gdalinfo "$file" || printf "$file\0"
done

and use it for example as: 并将其用作例如:

 mygdalinfo < path.txt | xargs -0 -J% mv % /tmp/somewhere

Howgh. Howgh。

You can say: 你可以说:

for file in `cat path.txt`; do gdalinfo $file || cp $file /data/temp; done

This would copy the file to /data/temp if gdalinfo cannot open the image. 如果gdalinfo无法打开图像,这会将文件复制到/data/temp

If you want to print the filename in addition to copying it in case of failure, say: 如果除了在发生故障时复制文件名之外还要打印文件名,请说:

for file in `cat path.txt`; do gdalinfo $file || (echo $file && cp $file /data/temp); done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM