Want to check whether a command succeeded by redirecting its output to a variable Want to check whether a command succeeded by redirecting its output to a variable bash bash

Want to check whether a command succeeded by redirecting its output to a variable


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  fidone

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`

or using $()

result=$(command argument a b c)

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

EDIT: 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 nullglobfor 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."  fidone

HTH.


I would call it, The command ... outputs a string. 'Return' is a keyword, and the return value is a number, where 0 means by convention success (0 errors) and a different value indicates an error code.

You grab the output by:

result=$(google youtube post --access unlisted --category Tech $f)

but will often see the inferior solution:

result=`cmd param1 param2`

inferior, because backticks are easily confused with apostrophes (depending on the font) and hard to nest, so don't use them.

From 'man bash':

The return value of a simple command is its exit status, or 128+n if the command is terminated by signal n.

and:

return [n]
Causes a function to exit with the return value specified by n. If n is omitted, the return status is that of the last command executed in the function body. If used outside a function, but during execution of a script by the . (source) command, it causes the shell to stop executing that script and return either n or the exit status of the last command executed within the script as the exit status of the script. If used outside a function and not during execution of a script by ., the return status is false. Any command associated with the RETURN trap is executed before execution resumes after the function or script.

The return value/exit code of the last command is gained through $?.

The keyword for the meaning you meant is command substitution. Again 'man bash':

Command Substitution
Command substitution allows the output of a command to replace the command name. There are two forms:

          $(command)   or          `command`

Bash performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word splitting. The command substitution $(cat file) can be replaced by the equivalent but faster $(< file).

   When  the old-style backquote form of substitution is used,

backslash retains its literal meaning except when followed by $, `, or . The first backquote not preceded by a backslash terminates the command substitution. When using the $(command) form, all characters between the parentheses make up the command; none are treated specially.

   Command substitutions may be nested.  To nest when using the

backquoted form, escape the inner backquotes with backslashes.

If the substitution appears within double quotes, word splitting and pathname expansion are not performed on the results.


If you are still getting output after > /dev/null then it's coming out on stderr, so standard backticks or $() won't work.

First, see if the return code indicates a problem: Examine $? after success and failure.

If it turns out the return code isn't sufficent, you can redirect the output:

RETURNVALUE=$(google youtube post --access unlisted --category Tech $f 2>&1 >/dev/null)

2>&1 puts stderr on the stdout fd, before you redirect stdout to nothing.