How do I echo output from shellscript?

Booting, installing, newbie
Post Reply
Message
Author
User avatar
paulh177
Posts: 975
Joined: Tue 22 Aug 2006, 20:41

How do I echo output from shellscript?

#1 Post by paulh177 »

I have a noddy shellscript which does some untars and runs a few makes.
I cannot for the life of me remember how to echo output from commands in a shellscript to stdout or capture output to a logfile :oops:

can someone point me in the right direction please?

fragment example (there's maybe 50 lines of this sort of stuff) :

Code: Select all

gunzip -c libpng-*.tar.gz |tar xf -
rm libpng-*.tar.gz
mv libpng-* libpng
cd libpng
make -f scripts/makefile.std CC=gcc ZLIBLIB=../zlib ZLIBINC=../zlib
rm *.so.* *.so
cd ..

User avatar
rarsa
Posts: 3053
Joined: Sun 29 May 2005, 20:30
Location: Kitchener, Ontario, Canada
Contact:

#2 Post by rarsa »

There are two things here.

Normally running that script will send the output to the stdout, no need to do anything else.

If you want to send ALL the output from the script to a log file the best way is to execute the script redirecting output to a log file with ">"

Code: Select all

# myscript > mylog.log
If you just want to selectivelly send the output of some commands and append them to a new or existing log file use ">>" in each of the commands

Code: Select all

LOG_FILE=/tmp/mylog.log
echo "#first line of my log file" > ${LOG_FILE}
gunzip -c libpng-*.tar.gz |tar xf - >> ${LOG_FILE}
rm libpng-*.tar.gz 
mv libpng-* libpng 
cd libpng 
make -f scripts/makefile.std CC=gcc ZLIBLIB=../zlib ZLIBINC=../zlib 
rm *.so.* *.so 
cd ..
[url]http://rarsa.blogspot.com[/url] Covering my eclectic thoughts
[url]http://www.kwlug.org/blog/48[/url] Covering my Linux How-to

User avatar
Pizzasgood
Posts: 6183
Joined: Wed 04 May 2005, 20:28
Location: Knoxville, TN, USA

#3 Post by Pizzasgood »

Code: Select all

echo "Hola mundo"
MyVariable=`which geany`
echo $MyVariable
echo "This will output contents of $MyVariable also, along with text"
echo 'You need single quotes to do an exclamation!"
echo 'They also let you do the $ sign without being interpreted'
echo `which geany`
echo "This goes into afile.txt" > afile.txt
echo "This gets appended to afile.txt" >> afile.txt
which geany > anotherfile.txt
Note: > makes a new file or replaces an existing one. >> makes a new file or appends to an existing one.

You can redirect the stderr to the output or /dev/null also

Code: Select all

rm NONexistantFile 2>&1
rm NONexistantFile 2>/dev/null
Last edited by Pizzasgood on Fri 26 Jan 2007, 21:38, edited 2 times in total.
[size=75]Between depriving a man of one hour from his life and depriving him of his life there exists only a difference of degree. --Muad'Dib[/size]
[img]http://www.browserloadofcoolness.com/sig.png[/img]

User avatar
paulh177
Posts: 975
Joined: Tue 22 Aug 2006, 20:41

#4 Post by paulh177 »

OK thanks guys.
it's the 2>&1 that I was particularly trying to remember ...
if i have

Code: Select all

# command_string 2>&1 >> logfile
then that would ensure both stderr and stdout from command_string are captured to a file?

User avatar
Pizzasgood
Posts: 6183
Joined: Wed 04 May 2005, 20:28
Location: Knoxville, TN, USA

#5 Post by Pizzasgood »

It should. I had problems with a particular command skipping out of something similar once though, but I was probably doing something different (I think it was when I was storing it in a variable to log later).
[size=75]Between depriving a man of one hour from his life and depriving him of his life there exists only a difference of degree. --Muad'Dib[/size]
[img]http://www.browserloadofcoolness.com/sig.png[/img]

Post Reply