Bash:变量值中的空格,以后用作参数 [英] Bash: space in variable value later used as parameter

查看:84
本文介绍了Bash:变量值中的空格,以后用作参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编写bash脚本以帮助使用Imagick的convert命令创建宝丽来缩略图时.我遇到问题了.虽然,我设法解决了这个问题(实际上,因为convert足够灵活),但我仍然想知道如何在没有这种特定解决方法的情况下解决此问题.

While writing a bash script to help creating polaroid thumbnail using Imagick's convert commmand. I encounter a problem. Although, I manage to work around with this (actually, because convert is flexible enough), I still want to know how to solve this without such specific workaround.

因此,基本上,bash脚本将获得标题值,其中可能包含空格.我想使用该标题作为convert的参数.如果标题为空(''),我将不使用选项'-caption'进行转换命令.像这样:

So basically, the bash script will get a caption value which may contain space. I want to use that caption as parameter of convert. If the caption is empty (''), I will not use the option '-caption' for convert command. Like this:

CAPTION="Is this Cute?" # The actual value will be tacked from the parameter of this bash.
IN_FILE="resources/puppy.png"
OUTFILE="resources/puppy_polaroid.png"

# If CAPTION is not empty, reformat CAPTION
if [ "$CAPTION" != "" ]; then CAPTION="-caption \"$CAPTION\""; fi
# otherwise, do not use '-caption' add all

COMMAND="convert $CAPTION \"$IN_FILE\" \"$OUTFILE\""
echo "Command: $COMMAND" #This echo a value command
`$COMMAND`

echo回显可以复制的value命令,可以将其粘贴到终端中并运行.但是bash无法运行.我该怎么做?

The echo echoes the value command that can be copied can pasted in a terminal and run. BUT the bash does not run. How I can do this?

注意:如果是convert,请执行-caption "".我知道这一点,目前正在使用它来解决.

NOTE: In case of convert, -caption "" do the job. I know this and currently use this as work around.

预先感谢您的帮助.

从答案中,这是现在对我有用的代码.

From the answer, here is the code that work for me now.

... # Get CAPTION and GRAVITY from parameters

if [ "$CAPTION" != "" ]; then ARGS_CAPTION=(-caption "$CAPTION"); fi
if [ "$GRAVITY" != "" ]; then ARGS_GRAVITY=(-gravity "$GRAVITY"); fi

if [ ! -f "$IN_FILE"  ]; then echo "The input file does not exist: '$IN_FILE'"; exit; fi
if [ "$OUTFILE" == "" ]; then OUTFILE=${IN_FILE%.*}-${IN_FILE#*.}-polaroid.png; fi

ARGS=("${ARGS_CAPTION[@]}" -thumbnail 480x480 -border 5x5 -pointsize 60 "${ARGS_GRAVITY[@]}" +polaroid -thumbnail 120x120)
echo convert "${ARGS[@]}" "$IN_FILE" "$OUTFILE";
convert "${ARGS[@]}" "$IN_FILE" "$OUTFILE"

我希望这对寻求类似解决方案的人有用.

I hope that this will be useful for those seeking similar solution.

推荐答案

您需要阅读条目050 BASH常见问题解答中:

我试图将命令放入变量中,但是复杂的情况总是会失败!

I'm trying to put a command in a variable, but the complex cases always fail!

变量保存数据.函数保存代码.不要将代码放在变量内!在许多情况下,人们尝试将命令或命令参数推入变量,然后运行它们.每种情况都需要单独处理.

Variables hold data. Functions hold code. Don't put code inside variables! There are many situations in which people try to shove commands, or command arguments, into variables and then run them. Each case needs to be handled separately.

...

  1. 我正在基于仅在运行时知道的信息来构建命令

上述问题的根源在于,即使该参数包含空格,您仍需要一种方法来将每个参数保持为单独的单词.引号不会这样做,但会使用数组. (我们在上一节中看到了一些内容,我们在其中动态构建了addrs数组.)

The root of the issue described above is that you need a way to maintain each argument as a separate word, even if that argument contains spaces. Quotes won't do it, but an array will. (We saw a bit of this in the previous section, where we constructed the addrs array on the fly.)

如果需要动态创建命令,请将每个参数放在数组的单独元素中.具有数组的外壳(例如Bash)使此操作变得更加容易. POSIX sh没有数组,因此最接近的是在位置参数中建立元素列表.这是上一部分中sendto函数的POSIX sh版本:

If you need to create a command dynamically, put each argument in a separate element of an array. A shell with arrays (like Bash) makes this much easier. POSIX sh has no arrays, so the closest you can come is to build up a list of elements in the positional parameters. Here's a POSIX sh version of the sendto function from the previous section:

这篇关于Bash:变量值中的空格,以后用作参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆