Visit Our UNIX and Linux User Community


Redirect output of echo to a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Redirect output of echo to a file
# 1  
Old 08-08-2013
Redirect output of echo to a file

Hi ,
I am trying to redirect output of echo to a file.So i wrote a function named printline.Here is my sample script
Code:
myscript.sh

function printline()
{
   echo "$1" >> myfile.log

}

usage()
{
printLine "********************USAGE*************************"
printLine "Script takes following  argument "
printLine "	Argument 1 : fully qualified path of the environment specific file"
printLine "Syntax :./complete_db_install.sh <argument1>"
printLine "*************************************************"
}

## 2. Read command line parameters/arguments of the script ##
if [ $# -le 0 ] || [ $# -gt 1 ]
then
    usage
    exit 1
fi

when i invoke this script without any parameters it redirects output to myfile.log but the problem i see is that all lines of usage except last line i.e "*****" does not get printed in log file instead a list of all files in the current directory gets printed in the log file.

Pls help

Last edited by jim mcnamara; 08-09-2013 at 12:07 AM.. Reason: code tags
# 2  
Old 08-09-2013
please change your code highlight to "CODE" from "ICODE" ...

also, you are imo overscripting the process ... this is fine if you have the time except that it also increases the possibilities of errors as well as make it harder to debug your script ...

anyways, see modified script ...
Code:
#! /bin/bash
# myscript.sh
PATH=/usr/bin:/bin:/usr/local/bin:/usr/sbin:/sbin

usage()
{
    echo "********************USAGE*************************"
    echo "Script takes following argument "
    echo " Argument 1 : fully qualified path of the environment specific file"
    echo "Syntax :./complete_db_install.sh <argument1>"
    echo "*************************************************"
}

## 2. Read command line parameters/arguments of the script ##
if [ $# -le 0 ] || [ $# -gt 1 ]
then
    usage >> /dir/myfile.log
    exit 1
fi

exit 0

# 3  
Old 08-09-2013
Thanks,
I agree i came up with that solution since i wanted to print o/p of echo into into a text file.Also i wanted a single place to redirect ..i mean there will be lots of echo statements in the script .I dont want to mess up the code by redirecting at each and every line say for example
Code:
echo "abc" >> myfile.txt
echo "xyz >> myfile.txt"

Is there a way in which i can write a wrapper custom function which will redirect output of echo to any file so that i can just use that every where in the code...
# 4  
Old 08-09-2013
From what I see your function is declared with a lower case "l" but is called with an upper case L.
# 5  
Old 08-09-2013
the issue with your printline function is that nobody will see anything on the screen since all echo statements are being redirected to the log file ...

you would be better off redirecting the output of commands, functions and loops individually as that would give you much better flexibility ...
Code:
# this will echo to screen and also save info to 2 different files

myfunction(){
    user=$1
    userhome=/home/$user
    echo "$user: $userhome" >> /dir/user.home.list
    echo "hello, $user"
    echo "this is my your list"
    ls $userhome | tee -a /dir/dir.list
}

myfunction | tee -a /dir/my.log 2>>&1

Code:
# this will send redirect output of loop to files and to screen if ">> /dir/my.log" is removed ...

for file in *
do
    if [ -f $file ]
    then
        echo $file | tee -a /dir/file.list
    else
        echo "---" 
    fi
done >> /dir/my.log

# 6  
Old 08-09-2013
Quote:
Originally Posted by ASC
Thanks,
I agree i came up with that solution since i wanted to print o/p of echo into into a text file.Also i wanted a single place to redirect ..i mean there will be lots of echo statements in the script .I dont want to mess up the code by redirecting at each and every line say for example
Code:
echo "abc" >> myfile.txt
echo "xyz >> myfile.txt"

Is there a way in which i can write a wrapper custom function which will redirect output of echo to any file so that i can just use that every where in the code...
You can group a code block in { } and redirect it
Code:
{
echo "abc"
echo "xyz"
} >> myfile.txt

Also you can echo multiline
Code:
echo "\
abc
xyz" >>  myfile.txt

Or combine the techniques
Code:
{
echo "\
two
lines"
date
} >> myfile.txt

# 7  
Old 08-09-2013
That is what i exactly want .When i execute the script i don't want to clutter the console with lots of echo statements. Script will just say
Code:
 echo "installation started "
printline "all intermediate debugging statements  are redirected to text file"
echo " installation complete or refer to above log file for error details."

Idea is console will show minimum statements and redirect to log files whenever required.
Can anyone tell me why the printline function i wrote is logging everything correctly except the last echo statement in the script ?
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #791
Difficulty: Easy
Logic gates cannot be used to store data.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Echo cannot redirect first or second output to logfile

I created a script to do some work. I want to use "echo" to redirect "date" to log file. echo works to screen. But cannot redirect first or second "echo" output to logfile. Please help. My code looks like: STARTTIME=`date +%m-%d-%Y` LOGFILE=/directory/logfile.log echo "Start time:" $STARTTIME... (8 Replies)
Discussion started by: duke0001
8 Replies

2. Shell Programming and Scripting

Redirect script output to a file and mail the output

Hi Guys, I want to redirect the output of 3 scripts to a file and then mail the output of those three scripts. I used below but it is not working: OFILE=/home/home1/report1 echo "report1 details" > $OFILE =/home/home1/1.sh > $OFILE echo... (7 Replies)
Discussion started by: Vivekit82
7 Replies

3. Shell Programming and Scripting

script to mail monitoring output if required or redirect output to log file

Below script perfectly works, giving below mail output. BUT, I want to make the script mail only if there are any D-Defined/T-Transition/B-Broken State WPARs and also to copy the output generated during monitoring to a temporary log file, which gets cleaned up every week. Need suggestions. ... (4 Replies)
Discussion started by: aix_admin_007
4 Replies

4. UNIX for Advanced & Expert Users

Redirect Topas output to a file

Hi, I want to know how to redirect the output of topas -P to a file in a readable format. I tried doing it by using topas -P > topas.txt but the output is not properly aligned and when I opened it using vi it ahd some characters. Please help me out in this. Thanks (1 Reply)
Discussion started by: Preetha
1 Replies

5. Shell Programming and Scripting

How to redirect output of ls to a file?

Hi All, I want to redirect only the file names to a new file from the ls -ltr directroy. how Can i do it. my ls -ltr output will be as below. -rwxr-xr-x 1 118 103 28295 Jul 26 2006 event.podl -rwxr-xr-x 1 118 103 28295 Jul 26 2006 xyz.podl I want my new file... (6 Replies)
Discussion started by: girish.raos
6 Replies

6. Shell Programming and Scripting

How redirect standard output to a file

Hi guys, i have a script named purgeErrors.ksh, when i execute this script i need to redirect the output to a log file in the same directory, how can i do that ?? -- Aditya (5 Replies)
Discussion started by: chaditya
5 Replies

7. UNIX for Dummies Questions & Answers

redirect output into the middle of a file

If I want to cat one file and have the output inserted into a specific place on another file, how is this done? I know how to append >> and to overwrite > but say I have a file with: File1: abc def ghi jkl And a File with: File2: mno pqr stu vwx And I want to place the... (5 Replies)
Discussion started by: glev2005
5 Replies

8. UNIX for Dummies Questions & Answers

redirect output to a file name

Hi all!! is possible to assign the output of some command to filename, i.e. grep_output.txt Otherwise, I want to open a new file which name is inside another, how can I do it? Thanks a lot! (7 Replies)
Discussion started by: csecnarf
7 Replies

9. UNIX for Dummies Questions & Answers

Redirect output to a file

Ahhhrrrggg I'm having a brain fart... I want to take the output of a command and redirect it to a file... This works.... $ man cp | cat >> copy_help but this doesn't keytool -help |cat >> keytool_help It just produces... these lines... more keytool_help ] ... ... (11 Replies)
Discussion started by: jimmyc
11 Replies

10. Shell Programming and Scripting

redirect output to file?

Hi: I am currently working on a program which requires direct its ouput to a file here is an example ./proram arg_1 arg_2 when program ends all output will be arg_2 file Is that possible I am not a bad programmer, However I am stuck there. Can anyone give a hint? Thanks SW (1 Reply)
Discussion started by: slackware
1 Replies

Featured Tech Videos