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.
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.
But I don’t know how to redirect that “return string” into a variable to do check the successs.
That’s what I have:
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?
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).
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 fortest, 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
or using
$()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
nullglobshell option (this will make./*.movto expand to the empty string, if there are no files). Also, quote that$for it’ll break if your file names contain spacesHTH.