PDA

View Full Version : Save the results of a script to a text file



GrouchyGaijin
May 19th, 2013, 02:26 PM
Hi Guys,

I have a bash script that uploads videos to YouTube.




#!/bin/bash




cd /media/Elements/"Video Staging"/
read -p "Enter a title for the video:" titletext
read -p "Enter a description:" desc
read -p "Enter the file name:" video


""youtube-upload --unlisted -m email -p password -t "$titletext" -c People --description="$desc" "$video"



The script and the YouTube uploader that makes everything possible run well.
After a video has been uploaded the url for that video is displayed in the terminal.

Is there a way I can have that url saved automatically to a text file? I've looked around a bit and saw something called "script".
When I entered script path/to/text/file the upload script everything seemed to come to a stop and I was given a new prompt.
Obviously this is not the way to go.

Does anyone know how I can do this?

Vaphell
May 19th, 2013, 02:40 PM
can't you redirect your script or youtube-upload line to some file?

./script >> file #whole output

youtube-upload --unlisted -m email -p password -t "$titletext" -c People --description="$desc" "$video" >> file # only this line
>> is append

it's easy to dump info in a tidy format to file, give example of youtube-upload output.

what's up with that ""? i don't think empty string in front of youtube-upload is needed for anything.

steeldriver
May 19th, 2013, 02:41 PM
You can redirect the standard output stream from your script to a file with the '>' operator


./yourscript > results.txt

If you want to redirect any errors to the file as well,


./yourscript > results.txt 2>&1

In newer versions of bash you can use &> instead of the > file 2>&1 syntax e.g.


./yourscript &> results.txt

Or if you want to capture in a file AND see it in the terminal, you can 'tee' the stream


./yourscript | tee results.txt
or

./yourscript 2>&1 | tee results.txt

If you want to append to the file instead of overwrite is, use 'tee -a'


./yourscript 2>&1 | tee -a results.txt

GrouchyGaijin
May 19th, 2013, 02:59 PM
Thanks guys - I thought I had tried > file and had an error.
Must have been a typo since it works now.