Test command:Duplicate Header row in Log File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Test command:Duplicate Header row in Log File
# 1  
Old 09-25-2012
Test command:Duplicate Header row in Log File

I have a script that is inventorying (not sure if thats a word) my environment.

It goes out and pulls Hostname OS, IP, and ENV (dev, prod, etc)..then spits all that to a logfile

At the top of my script i have a check to see if the logfile exist.
Code:
[[ -f $LOGFILE ]] || touch $LOGFILE && echo "ENV" "\t" "HOSTNAME" "\t" "OS" "\t" "IPADDRESS"

What i want it to do is IF exist, do nothing...otherwise touch and echo header to file.

Currently every run it echos the header row.
# 2  
Old 09-25-2012
Quote:
Originally Posted by nitrobass24
I have a script that is inventorying (not sure if thats a word) my environment.

It goes out and pulls Hostname OS, IP, and ENV (dev, prod, etc)..then spits all that to a logfile

At the top of my script i have a check to see if the logfile exist.
Code:
[[ -f $LOGFILE ]] || touch $LOGFILE && echo "ENV" "\t" "HOSTNAME" "\t" "OS" "\t" "IPADDRESS"

What i want it to do is IF exist, do nothing...otherwise touch and echo header to file.

Currently every run it echos the header row.
I think you want something more like:
Code:
if [ ! -f $LOGFILE ]
then printf "ENV\tHOSTNAME\tOS\tIPADDRESS\n" > $LOGFILE
fi

You have to redirect the output into the file you want it in instead of just writing it to stdout while it is directed to your terminal. But if you redirect it with the command form you're using you'll end up with an empty file because the shell redirection will create the file before the initial test checks for its existence.

Note that I used printf rather than echo because there are many different versions of echo. According to the standards the results are undefined if any of the arguments to echo contain a backslash character. Historically System V echo accepted \ escapes and BSD echo did not. System V echo didn't accept any options (and if it got initial arguments starting with - it printed them as part of the output) while BSD echo used a -n option to omit the terminating newline usually added to the output (System V echo used \c at the end to terminate output without a terminating newline). The printf utility should work the same on any UNIX-like system.
# 3  
Old 09-25-2012
Try the "-s" file test:
-s file file is not empty.
Code:
if [[ ! -s $LOGFILE ]] then
 touch $LOGFILE && echo "ENV" "\t" "HOSTNAME" "\t" "OS" "\t" "IPADDRESS"
fi

# 4  
Old 09-25-2012
Quote:
Originally Posted by spacebar
Try the "-s" file test:
-s file file is not empty.
Code:
if [[ ! -s $LOGFILE ]] then
 touch $LOGFILE && echo "ENV" "\t" "HOSTNAME" "\t" "OS" "\t" "IPADDRESS"
fi

This still won't work. The output of the echo isn't redirected away from your terminal.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to display the header of a matched row in a file?

Hi, So I am trying to print the first row(header) first column alongwith the matched value. But I am not sure how do I print the same, by matching a pattern located in the file eg File contents Name Place Jim NY Jill NJ Cathy CA Sam TX Daniel FL And what I want is... (2 Replies)
Discussion started by: sidnow
2 Replies

2. Shell Programming and Scripting

Exclude the header row while splitting the file

Hi All, i have script like ... "TYPE_ID" "ID" "LIST_ID" "18" "52010" "1059" "18" "52010" "1059" "18" "52010" "1059" "18" "52010" "1059" i am using the below code it's not taking the header row. awk -F"\t" -v file=test1.txt -v file1=test2.txt ' { if(... (7 Replies)
Discussion started by: bmk
7 Replies

3. UNIX for Dummies Questions & Answers

File Row Line Count without Header Footer

Hi There! I am saving the file count of all files in a directory to an output file using: wc -l * > FileCount.txt I get: 114 G4SXORD 3 G4SXORH 0 G4SXORP 117 total But this count includes header and footer. I want to subtract 2 from the count and get ... (7 Replies)
Discussion started by: gagan8877
7 Replies

4. Shell Programming and Scripting

Add column header and row header

Hi, I have an input like this 1 2 3 4 2 3 4 5 4 5 6 7 I would like to count the no. of columns and print a header with a prefix "Col". I would also like to count the no. of rows and print as first column with each line number with a prefix "Row" So, my output would be ... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

5. Shell Programming and Scripting

Exclude the header row in the file to validate

Hi All, File contains header row.. we need to exclude the header row...no need to validate the first row in the file. Data in the file should take valid data(two columns)..we need to exclude the more than two columns in the file except the first line. email|firstname a|123|100 b|345... (4 Replies)
Discussion started by: bmk
4 Replies

6. UNIX for Dummies Questions & Answers

append column and row header to a file in awk script.

Hi! Is there a way to append column and row header to a file in awk script. For example if I have Jane F 39 manager Carlos M 40 system administrator Sam F 20 programmer and I want it to be # name gend age occup 1 Jane F 39 manager 2 Carlos M ... (4 Replies)
Discussion started by: FUTURE_EINSTEIN
4 Replies

7. UNIX for Dummies Questions & Answers

Merge all csv files in one folder considering only 1 header row and ignoring header of all others

Friends, I need help with the following in UNIX. Merge all csv files in one folder considering only 1 header row and ignoring header of all other files. FYI - All files are in same format and contains same headers. Thank you (4 Replies)
Discussion started by: Shiny_Roy
4 Replies

8. Shell Programming and Scripting

Perl array with row header

Here is the csv file file i have: ServerName, IPAddress, Gateway, Notes ServerA, 192.168.1.100, 192.168.1.1, This is some server ServerB, 192.168.1.110, 192.168.1.1, This is some other server ServerC, 192.168.1.120, 192.168.1.1, This is some other other server I would like to have the... (6 Replies)
Discussion started by: Ikon
6 Replies

9. Shell Programming and Scripting

how can I bcp out a table into a text file including the header row in the text file

Hi All, I need to BCP out a table into a text file along with the table headers. Normal BCP out command only bulk copies the data, and not the headers. I am using the following command: bcp database1..table1 out file1.dat -c -t\| -b1000 -A8192 -Uuser -Ppassword -efile.dat.err Regards,... (0 Replies)
Discussion started by: shilpa_acc
0 Replies

10. UNIX for Dummies Questions & Answers

insert header row into .xls

Hello, I am building an .xls file extracting info from a DB to be eventually emailed. All is good except how do I put in a header row.. like date, name of report etc. before the columns with the actual column name and data? Thanks for any assistance.. the below is after I have signed into... (11 Replies)
Discussion started by: Tish
11 Replies
Login or Register to Ask a Question