LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-13-2012, 04:32 PM   #1
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 737

Rep: Reputation: 78
Bash for statement


so i have a Q,
i run a find command to find all files that are "ASCII" and output their full path to a file.

then i run
Code:
for i in `cat all-ascii-files`; do grep -P '\d{3}-\d{3}-\d{3}' $i >> out.txt; done
but something odd happens, i will get some stdout messages that some files are not there "no such file or directory", then the cursor sits there not at a prompt (cant see prompt), but when i hit enter i get a Exit 1 status code for the above "for" statement. why?

Last edited by Linux_Kidd; 11-13-2012 at 04:34 PM.
 
Old 11-13-2012, 04:34 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
For vs while loop.
 
Old 11-13-2012, 08:53 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
I think that was a hint to try this construct:
Code:
while read i; do
     put your commands here
     and here
done < filename
 
Old 11-13-2012, 09:01 PM   #4
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 737

Original Poster
Rep: Reputation: 78
what do you mean? i dont always get a Exit 1 when using 'for' loops with this input file, so i dont understand why i get this Exit code.

i looked up the diffs between 'for' and 'while' loops and what i have looks ok.

so from bash prompt its
Code:
[server]# while read i; do grep 'whatever' $i; done < all-ascii-files >> outfile
why is this diff than my 'for' loop?

Last edited by Linux_Kidd; 11-13-2012 at 09:08 PM.
 
Old 11-13-2012, 09:02 PM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
You could try modifying the code to print the file names passed to grep and then examine all-ascii-files to see what is the name of the file causing the problem
Code:
for i in `cat all-ascii-files`; do
    echo "DEBUG: i is '$i'"
    grep -P '\d{3}-\d{3}-\d{3}' $i >> out.txt
done
 
Old 11-13-2012, 09:07 PM   #6
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 737

Original Poster
Rep: Reputation: 78
why would a file name cause an Exit 1? if the file does not exist then the grep simply gives error to stdout "no such file or directory" and the 'for' loop continues.

i can see if the last entry in the file is a file that does not exist then grep exits with a Exit 1 code, but why do i see the command line "script" exit on stdout only when i type a key?

Last edited by Linux_Kidd; 11-13-2012 at 09:11 PM.
 
Old 11-13-2012, 09:36 PM   #7
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 737

Original Poster
Rep: Reputation: 78
Quote:
Originally Posted by catkin View Post
You could try modifying the code to print the file names passed to grep and then examine all-ascii-files to see what is the name of the file causing the problem
Code:
for i in `cat all-ascii-files`; do
    echo "DEBUG: i is '$i'"
    grep -P '\d{3}-\d{3}-\d{3}' $i >> out.txt
done
i did the test slightly different, i grep'd for something i knew would be no match, wc -l for input and output files are exactly the same. the Exit 1 i see must be grep failing the match when grep'ing the last $i ??

i just dont understand why i see the Exit status after hitting a key.
 
Old 11-14-2012, 06:32 AM   #8
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
It's due to the IFS.
Use
Code:
cat all-ascii-files|while read ITEM; do grep -P '\d{3}-\d{3}-\d{3}' "${ITEM}" >> out.txt; done
 
Old 11-14-2012, 08:18 AM   #9
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 737

Original Poster
Rep: Reputation: 78
Quote:
Originally Posted by unSpawn View Post
It's due to the IFS.
Use
Code:
cat all-ascii-files|while read ITEM; do grep -P '\d{3}-\d{3}-\d{3}' "${ITEM}" >> out.txt; done
hmmm, i still get Exit 1 status when it finishes
Code:
[1]+ Exit 1                  cat all-ascii-file | while read ITEM; do
    grep -P '\d{3}-\d{3}-\d{3}' "${ITEM}" >> junk.out;
done
so, it seems that the Exit 1 status code is from grep not finding anything on the last $ITEM it looks at. i grep'd a file for non-existent string and $?=1, but grep for a known match and $?=0.

but correct me if i am wrong, the reason i see the odd way job finishing when using 'for' loop is due to IFS? which IFS causes that?

thanks for the help, its be a learning session for me.
 
Old 11-14-2012, 08:39 AM   #10
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by Linux_Kidd View Post
Code:
[1]+ Exit 1
This says you either ran your code in the background (bg, ampersand) or some character got interpreted as a backgrounding command?
Code:
cat all-ascii-files|while read ITEM; do [ -f "${ITEM}" ] || echo "epic fail \""${ITEM}"\""; done

Quote:
Originally Posted by Linux_Kidd View Post
the reason i see the odd way job finishing when using 'for' loop is due to IFS? which IFS causes that?
Spaces, mostly:
Code:
echo -en "Crack Hitler\nBe Aggressive\nEvidence\n" >> /tmp/listing
# * FNM song titles, obviously.
for ITEM in `cat /tmp/listing`; do echo "${ITEM}"; done
cat /tmp/listing | while read ITEM; do echo "${ITEM}"; done
Also see 'man bash': IFS.
 
Old 11-14-2012, 10:30 AM   #11
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 737

Original Poster
Rep: Reputation: 78
yes, my very bad, i am & them.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get some bash scripts into a simple bash script with some echo and if statement. y0_gesh Programming 3 03-01-2012 09:46 AM
OR statement in Bash akeenabawa Linux - Software 3 11-14-2008 09:17 AM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 06:00 AM
Bash: Print usage statement & exit; otherwise continue using Bash shorthand operators stefanlasiewski Programming 9 02-07-2006 05:20 PM
bash statement os2 Programming 2 03-20-2005 10:13 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:51 PM.

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