LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-08-2016, 02:52 PM   #1
mikenash
Member
 
Registered: Dec 2014
Posts: 84

Rep: Reputation: Disabled
Output from script that should not be there.


I am receiving output from a script that should not be there. The output is from the ls command and it depends on the directory it uses. The bad output comes from /home/linux1/Chapter7 or /home/linux1/Chapter7/source. Output from /home/linux1 or anything else appears to work. The file that is the bad output is called awk_data6. I remove before and after the search to ensure it does not exist. The "END" statement should create this file and therefore should never be found by the ls command. Is there an explaination?
Code:
linux1@mikenash-rhel67:/home/linux1/Chapter7/source $~>sh list_small_files6
rm: cannot remove `/home/linux1/Chapter7/source/awk_data6': No such file or directory
LIST OF FILES WITH LESS THEN 32 BYTES
-rw-rw-r-- 1 linux1 linux1     0 Apr  8 15:38 awk_data6
-rw-rw-r-- 1 linux1 linux1     0 Apr  5 13:16 linktest
Total 0 bytes for 2 files.
Code:
  rm ~/Chapter7/source/awk_data6
  ls -lR /home/linux1/Chapter7/source 2> /dev/null | awk -v byte_counter=0 '
  BEGIN{print "LIST OF FILES WITH LESS THEN 32 BYTES"}
     NF == 9  &&  $1 !~ /^d/  &&  $5 < 32{print $0; byte_counter=byte_counter+$5; ++file_counter}
  END{print "Total " byte_counter " bytes for " file_counter " files." }' > ~/Chapter7/source/awk_data6
  cat awk_data6
  rm ~/Chapter7/source/awk_data6
 
Old 04-08-2016, 05:12 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
The moment the redirection is opened the file exists. You can also see that it is zero bytes in size as at the point it was found it has not been closed, hence the size is not yet known.

So your script is behaving correctly
 
Old 04-09-2016, 02:45 PM   #3
lamont0
LQ Newbie
 
Registered: Feb 2016
Posts: 20

Rep: Reputation: Disabled
An END rule is executed, once, after all the input has been read. I would have thought that any file created after the scanning all input completed could not have been included, parsed and tested as if it were part of the output of the ls command. Will the END rule be included in the parsing and testing of the previous statement after all the input has been read? If it does then this make sense because at the point it becomes greater that 31 bytes it will not be included with the output.
I issue the ls command and it completes. I use this output as input to test and produce the output I expect. Then I create a new file to store this output. How and why would this new file be included as part of the output from the ls command?
 
Old 04-09-2016, 03:55 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
You misunderstand ... awk is not creating the file, bash is. The redirection is outside the awk script and controlled by bash. So the '>' after your script is what has opened the file
for writing, which means if it exists to be written to, then it exists once your script starts processing the ls input. Also, bash processing is done prior to any command being executed,
just in case you thought ls was run first.

As a side note, you do realise you could have done this whole script with the find command.
 
Old 04-10-2016, 10:46 AM   #5
lamont0
LQ Newbie
 
Registered: Feb 2016
Posts: 20

Rep: Reputation: Disabled
Thank you Grail. Your explanation was very clear and informative with the interactions of BASH and AWK script. These types of details are not obvious to a novice. I appreciate that you mentioned the find command because I first used the find command to get preview of what I needed the data to look like. The purpose of the awk script was my first exercise in learning about awk. A simple task but I learned a great amount of knowledge with doing it. Thanks again.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I redirect output from bash script WITHIN script Robert S Linux - General 7 07-19-2013 10:42 AM
[SOLVED] How to find out the output of a script is success or not in another script jhonnappier2007 Linux - Newbie 6 11-10-2011 12:37 AM
Standard script output to screen & -x option output to file BoBeau236 Programming 6 05-10-2011 07:43 AM
Make a script that uses the output from another script as the variables? Oakems Programming 11 06-18-2009 05:21 PM
Correct output from script appears only when script is run interactively kaplan71 Linux - Software 2 01-15-2009 11:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration