要检查其输出重定向到一个变量的命令是否成功 [英] Want to check whether a command succeeded by redirecting its output to a variable

查看:140
本文介绍了要检查其输出重定向到一个变量的命令是否成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在写它加载视频文件到到YouTube使用 GoogleCL bash脚本。

I'm currently writing a bash script which loads video files up to to YouTube using GoogleCL.

因为我在一个循环做这个东西上传(因为可以有多个视频文件)我想检查是否每个文件已经成功上传之前,我上传下一个。

As I'm doing this uploading stuff in a loop (because there can be multiple video files) I would like to check if each file had been uploaded successfully before I upload the next one.

命令谷歌YouTube上发布--access非上市--category科技$ F (其中$ f重新presents文件)输出一个字符串,它告诉我是否上传已成功与否。

The command google youtube post --access unlisted --category Tech $f (where $f represents the file) outputs a string which tells me whether the upload has been successful or not.

但我不知道该怎么重定向返回字符串到一个变量做检查successs。

But I don't know how to redirect that "return string" into a variable to do check the successs.

这就是我有:

for f in ./*.ogv ./*.mov ./*.mp4
do
    if [[ '*' != ${f:2:1} ]]
    then
        echo "Uploading video file $f"

        # How to put the return value of the following command into a variable?
        google youtube post --access unlisted --category Tech $f > /dev/null

        # Now I assume that the output of the command above is available in the variable RETURNVALUE
        if [[ $RETURNVALUE == *uploaded* ]]
        then
            echo "Upload successful."
        else
            echo "Upload failed."
        fi
    fi
done

任何人可以帮助我吗?

Can anybody help me?

推荐答案

我的猜测是,你可以依靠从谷歌命令错误code,以及(我假设,如果上传失败则返回错误,但你应该仔细检查这一点)。

My guess is that you could depend on the error code from the google command as well (I'm assuming it returns error if it failed to upload, but you should probably double check this).

for f in ./*.ogv ./*.mov ./*.mp4; do
  if [[ '*' != ${f:2:1} ]]; then

    echo "Uploading video file $f"
    if google youtube post --access unlisted --category Tech "$f" > /dev/null
    then
      echo "Upload successful."
    else
      echo "Upload failed."
    fi

  fi
done

一个常见的​​误解是,如果想要一个括号前pression评估,这是不正确的,如果总是一个命令,并检查错误状态;该命令一般 [这是测试,它计算除权pression的别名。 (是的,我会感到惊讶,如果没有优化的快捷方式,使其走得更快里面的bash,但在概念上它仍然是正确的)。

A common misconception is that if wants a bracketed expression to evaluate, this is not true, if always takes a command and checks the error status; usually this command is [ which is an alias for test, which evaluates the expression. (And yes, I'd be surprised if there isn't an optimized shortcut to make it go faster inside bash, but conceptually it's still true).

捕获输出通过反引号完成的,像这样

Capturing output is done via backticks, like so

result=`command argument a b c`

或使用 $()

result=$(command argument a b c)

但它可能是最好使用错误code在这种情况下。

but it's probably better to use the error code in this case.

编辑:
你有一个有趣的,如果你的函数事情..我没​​有注意到第一,但如果启用 nullglob shell选项(这会让<$ C可避免$ C> ./*。MOV 扩大为空字符串,如果没有文件)。此外,引述 $ F 或将打破,如果你的文件名包含空格

You have a funny if thing in your function.. I didn't notice at first, but it can be avoided if you enable nullglob shell option (this will make ./*.mov to expand to the empty string, if there are no files). Also, quote that $f or it'll break if your file names contain spaces

shopt -s nullglob
for f in ./*.ogv ./*.mov ./*.mp4; do
  echo "Uploading video file $f"
  if google youtube post --access unlisted --category Tech "$f" > /dev/null
  then
    echo "Upload successful."
  else
    echo "Upload failed."
  fi
done

心连心。

这篇关于要检查其输出重定向到一个变量的命令是否成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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