For Loop with find


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting For Loop with find
# 1  
Old 05-16-2005
Bug For Loop with find

I'm trying to write a script to archive some log files, and I'm getting a little stuck. My 'find' command by itself works great, returning all of the log files I need, but when I try to combine it with my loop, I'm getting an error. Here's what I have so far:

for FILENAME in $(find . -type f -name '*log' -print | sed 's/^\.\///')
do
...
IF the file is not from today
ZIP the File
Move the file to an archive directory
...
done

But when I try to execute the program, I get the following error:

syntax error at line 13: `$' unexpected

Any ideas?? Thanks for your help.
# 2  
Old 05-16-2005
Post the code for all of your 'for' loop. What shell are you using?
# 3  
Old 05-16-2005
Here is my code

Here is my code (note, I removed some blank lines, and the error is now occuring on line 11):

Code:
#!/bin/sh

logdir="/opt/IBMIHS/logs"
TODAY=`date '+%y%m%d'`

cd $logdir

# This loop returns all files in the 'logdir' directory that contain
# the words log. The sed removes any leading ./ sequences

for FILENAME in $(find . -type f -name '*_log' -print | sed 's/^\.\///')
do
  if [ $FILENAME != *_log$TODAY.log ] ; then
    echo $FILENAME
    mv $FILENAME /export/home/
    echo "file moved from the logs direcory"
  fi
done

# 4  
Old 05-16-2005
the '$(....)' construct is specific to ksh/bash and is NOT available in Bourne shell [/bin/sh].
Either change '#!/bin/sh' to '#!/bin/ksh'

OR use `....` construct

Also the '$FILENAME != *_log$TODAY.log' is not going to work.
If you end up using 'ksh', look into using @(pattern):
if [ $FILENAME != @(*_log$TODAY.log) ]

For more info do 'man ksh' and search for 'File Name Generation'

BTW, wouldn't it be easier to do it like so iwthout additional filtering [not tested]:
Code:
#!/bin/ksh

logdir="/opt/IBMIHS/logs"
TODAY=`date '+%y%m%d'`

cd $logdir

# This loop returns all files in the 'logdir' directory that contain
# the words log. The sed removes any leading ./ sequences

for FILENAME in $(find . -type f ! -name "*_log${TODAY}.log" -print | sed 's/^\.\///')
do
    echo $FILENAME
    mv "$FILENAME" /export/home/
    echo "file moved from the logs direcory"
done


Last edited by vgersh99; 05-16-2005 at 06:41 PM..
# 5  
Old 05-17-2005
Thanks for the suggestions!

Thanks for the suggestions! Here is my final code (it's working the way I want!).

Code:
#!/bin/sh

logdir="/opt/IBMIHS/logs"
TODAY=`date '+%y%m%d'`

cd $logdir

# This loop moves all files in the 'logdir' directory that contain
# the word log, and are not from today to the /tmp/logs directory.

for FILENAME in `find . -type f -name "*_log*" ! -name "*${TODAY}*" -print | sed 's/^\.\///'`
do
  gzip $FILENAME
  mv $FILENAME.gz /tmp/logs
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

While loop, input from find command

Hello nix Experts, I am a *nix rookie and have run into this issue, can some one help me here and let me know what I am doing wrong. /home/user1> while read n > do > echo $n > done < <(find . -type f -ctime -1 | grep abc) I am getting the below error: -sh: syntax error near... (5 Replies)
Discussion started by: babyPen1985
5 Replies

2. UNIX for Dummies Questions & Answers

find command in for loop

i have executed the following command in terminal find /Users/vasu -name "*.txt" -print and i am getting the result /Users/vasu/file1.txt /Users/vasu/file2.txt /Users/vasu/file3.txtbut while i was trying to execute the same in the script it is not working,I tried with below logic in... (2 Replies)
Discussion started by: vmachava
2 Replies

3. Shell Programming and Scripting

Loop with Find—damn spaces!

Hi Guys, I'm trying to find all files with a particular extension and then loop some actions. The problem is that if the files have spaces in their names I get end up being each word as a separate result rather than the entire file. ext=".txt" out=".rtf" for i in $( find "$1" -name "*$ext" );... (9 Replies)
Discussion started by: imonkey
9 Replies

4. Shell Programming and Scripting

find in for loop

whats wrong with this script for i in Users.csv abd.csv > do > find . -name $i > done (2 Replies)
Discussion started by: theshashi
2 Replies

5. Shell Programming and Scripting

find in for loop

What's wrong with this script for i in Users.csv anc.csv > do > find . -name $i > doneDouble post. Continue here. (0 Replies)
Discussion started by: theshashi
0 Replies

6. Shell Programming and Scripting

Find result using for loop

I want to print each file i found using the find command. And not able to list the files at all here is the code SEARCH_DIR="/filesinfolder"; PATH_COUNT=0 for result in "'/usr/bin/find $SEARCH_DIR -daystart \( \( -name 'KI*' -a -name '*.csv' \) -o -name '*_xyz_*' \) -mtime 1'" do... (1 Reply)
Discussion started by: nuthalapati
1 Replies

7. Shell Programming and Scripting

find a string in a loop

Hi, Can anyone help with the problem below? I need to read all text files (file1, file2, file3) in a loop and find file1.txt: file2.txt: File3.txt: (7 Replies)
Discussion started by: Lenora2009
7 Replies

8. Shell Programming and Scripting

Help in loop find question at STEP 3

STEP 1 # Set variable FILE=/tmp/mainfile SEARCHFILE =/tmp/searchfile cat /tmp/mainfile Interface Ethernet0/0 "outside", is up, line protocol is up Hardware is i82546GB rev03, BW 100 Mbps Full-Duplex(Full-duplex), 100 Mbps(100 Mbps) MAC address 001e.f75e.8cb4, MTU 1500 IP address... (1 Reply)
Discussion started by: irongeekio
1 Replies

9. Shell Programming and Scripting

Loop and find not working ASAP

I grep some thing from file then in a loop give to find one by one but not working. #!/bin/bash grep -H DocumentRoot /etc/httpd/conf.d/*.conf | awk -F' ' '{ print $3 }'| sort -u | uniq > path.txt for j in `cat path.txt` do # echo "line is $i" find "$j" -type d... (3 Replies)
Discussion started by: aliahsan81
3 Replies

10. AIX

using find in for loop

hello gurus, src="/home/training" typ="*.sh" for i in `find $src -name $typ` do ... .. done the above code piece is giving this error: find: 0652-009 There is a missing conjunction I tried this way: for i in `find $src -name "$typ"` But then it didn't get any files, though there... (11 Replies)
Discussion started by: muthursyamburi
11 Replies
Login or Register to Ask a Question