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
# 8  
Old 08-09-2013
Quote:
Originally Posted by ASC
Can anyone tell me why the printline function i wrote is logging everything correctly except the last echo statement in the script ?
It works for me (in bash, and after removing the word function in sh and ksh).
Someone mentioned the printline versus printLine in your post, but that gives "not found" errors, and does not explain the bug that you encounter.
# 9  
Old 08-09-2013
Quote:
Originally Posted by ASC
Code:
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 "*************************************************"

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.
I saw nothing in your code that causes pathname expansion (aka file globbing). Therefore, I suspect that the final line of asterisks is actually written correctly. Perhaps the source of the filenames you see is whatever code you're using to read the log.

A demonstration:
Code:
$ echo '*********' > file
$ cat file
*********
$ echo $(cat file)
[ here you will see the current directory's contents, minus dot-files ]

So, exactly what code are you using to display myfile.log's contents?

Regards,
Alister
# 10  
Old 08-09-2013
Alright guys
This is what i have written :
Code:
#This is a simple shell script which will read details from a java properties file namely billing.properties
#!/bin/sh
# Argument 1 : fully qualified path of the environment specific file
# syntax : ./complete_db_install.sh <fully quantified name of property file>

#set -e


## Utility functions ##

printline()
{
	echo $1 >> product_install.log

}


## 1. PRINT USAGE OF SCRIPT
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

Content of product_install.log is as follows. I am viewing it using vi editor

********************USAGE*************************
Script takes following argument
Argument 1 : fully qualified path of the environment specific file
Syntax :./complete_db_install.sh <argument1>
b.sql complete_db_installBak.sh complete_db_install.sh config newtrial.sh product_install1.sh product_install.log product_install.sh sql_scripts test_install.sh test.sh trial.sh workingmod.sh working.sh
# 11  
Old 08-09-2013
This differs from the function in your original post in a critical way. The echo argument is unquoted.

Aside from the pathname expansion, the leading whitespace before "Argument 1" should have been lost.

By the way, to preserve whitespace and formatting, use code tags for data as well as code.

Regards,
Alister

Last edited by alister; 08-09-2013 at 06:32 PM..
# 12  
Old 08-09-2013
also (minor, but still)change this:
Code:
#This is a simple shell script which will read details from a java properties file namely billing.properties 
#!/bin/sh

to this:
Code:
#!/bin/sh
#This is a simple shell script which will read details from a java properties file namely billing.properties

the shell (shebang) interpreter spec should be in the FIRST line of a script.
# 13  
Old 08-10-2013
Thanks guys it worked .. while playing with the script i accidently stripped of the quotes around $1.
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #819
Difficulty: Medium
In CSS, E ~ F matches an F element preceded by an E element.
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