本文介绍了在BASH空格的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着写脚本,会裁剪和调整较大的照片转换成高清壁纸。

I'm trying to write script that'll crop and resize large photos into HD Wallpapers.

#! /bin/bash


for i in `ls *.jpg`
do
    width=`identify -format '%w' $i`
    height=`identify -format '%h' $i`

    if [ `echo "$width/$height > 16/9" | bc -l` ]
    then
        exec `convert $i -resize 1920 -gravity Center -crop '1920x1080+0+0' +repage temp`
    else
        exec `convert $i -resize x1080 -gravity Center -crop 1920x1080+0+0 +repage temp`
    fi

    rm $i
    mv temp $i
done

但似乎脚本与拥有文件名空间(如翻滚Weed.jpg)的问题。我该如何解决这个问题?

But it seems that the script has problems with file names having spaces (like Tumble Weed.jpg). How can I fix this?

推荐答案

首先,你不需要 LS 。通过使用backtics LS ,你含蓄使得bash解析字符串到一个列表,它通过空格分割。相反,使庆典生成列表,并没有这样的怪癖分开:

First, you don't need ls. By using ls in backtics, you implicitly make bash parse a string into a list, which splits by whitespaces. Instead, make bash generate the list and separate it without such quirks:

此外,您还需要将所有 $ I 用途括入引号,使庆典替代它作为一个整体,而不是作为一个字符串分割分割单词。

Also, you need to enclose all $i usages into quotes, to make bash substitute it as a whole, not as a string split to separate words.

下面是一个演示这两个观念的脚本:

Here's the script that demonstrates both ideas:

for i in *.jpg ; do
  echo "$i";
done

这篇关于在BASH空格的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 23:04