LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Blogs > NullTerminator
User Name
Password

Notices

Rate this Entry

bash read file

Posted 05-24-2013 at 09:20 AM by NullTerminator
Updated 05-24-2013 at 11:26 AM by NullTerminator

Sample of while loop to process input
Code:
#!/bin/sh
##################################
# Purpose: Cleanup any confirmation reports (*.pdf) older than xx days
# Called By:  batch daily cleanup process,  /Applications/xyz/distributor.sh
# Author: jeb
# Date: 4/20/07
# Comments: The while loop is preferred over the find -exec rm {} syntax because it
#           does not spawn a process for each file to be deleted.
#           The script expects (*.pdf) instead of (*.PDF) but the script would work
#           in case of (*.PDF) as the testing showed shell to be case insensitive.
# -mtime +30  yields files modified more than 30 days ago
# -mtime -10  would yield files modified less than 10 days ago
##################################
find /abc/xyz/reports/confirmations -type f -name "*.pdf"  -mtime +30 > cleanuplist
while read -r filename                                               
do                                                                   
print  "deleting " $filename                                   
rm $filename                                                         
done < cleanuplist
Another way to read a file via cat and a pipe.
You can use a semicolon to combine commands on one line.
Code:
while read line; do echo $line; done
Code:
cat thefile | while read firstWord secondWord restOfLine
do
  echo $firstWord
  echo $secondWord
  echo $restOfLine
  # do other stuff like ls -l "$firstWord"
done
Posted in Uncategorized
Views 521 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 12:39 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration