1832855 Members
2913 Online
110047 Solutions
New Discussion

Shell script problem

 
SOLVED
Go to solution
Barry Deevey
Occasional Contributor

Shell script problem

Hi all,
I'm trying to write a script that checks a directory, moves the files that contain data and deletes the remaining 0 length files - Below is my script that doesn't work properly:

for i in `ll /users/admin/output/ | awk '{print $9}'`
do
if [[ -s "$i" ]]
then
print $i
mv /users/admin/output/$i /users/admin/output/keep/$i
fi
done

rm /users/admin/output/late*

Could anybody point me in the right direction?? Also, could you recommend a good book that I could use to learn from (one that contains good examples etc.) Thank you in advance for any respone, they are very much appreciated.
Best Regards, Barry.

6 REPLIES 6
Frederic Soriano
Honored Contributor

Re: Shell script problem

Hello Barry

Try this:

for i in `ll /users/admin/output/ | awk '{print $9}'`
do
if [ -s "$i" ]
then
print $i
mv /users/admin/output/$i /users/admin/output/keep/$i
fi
done

rm /users/admin/output/late*

Double brackets are not needed here, but be sure that there is a space between the opening bracket and the test flag, and before the closing bracket.

ll may be replace by a 'ls -1', more effective, and avoiding an 'awk' pipe.

As a starting point, you should buy the excellent book from O'Reilly: Unix in a nutshell.

Hope this helps!

Regards.
Dan Hetzel
Honored Contributor

Re: Shell script problem

Hi Barry,

# remove empty files first
find /users/admin/output -type f -size 0 -exec rm {} \;
# move the remaining files but not the keep directory
cd /users/admin/output
mv !(keep) keep


Best regards,

Dan

Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Dan Hetzel
Honored Contributor

Re: Shell script problem

Hi again,

I forgot the book....

Unix Power Tools
Jerry Peek, Tim O'Reilly and Mike Loukides

O'Reilly and Associates

ISBN 0-679-79073-x

Dan
Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Open Systems
Advisor
Solution

Re: Shell script problem

For efficiency in scripting you could remove the ls altogether (if you don't want to use the find method)

eg:

for i in *
do
if [[ -s ${i} ]]
then
mv ${i} ${KeepDir}
else
rm ${i}
done
Barry Deevey
Occasional Contributor

Re: Shell script problem

Just a quick note, the method described by Frederic did not seem to work, I'll continue to play around with it for my own education.

Thank you all for your postings.

Cheers,
Barry.
Josef Nordtome
Advisor

Re: Shell script problem

Hello Barry,

The problem with your script is that you are trying to test $i from your current directory. You must put in the full path name in the if statement for it to work.

Using Frederic's example (use single brackets), the if statement should read

if [ -s "/users/admin/output/$i" ]

You can also cd to /users/admin/output before doing the for loop. This would allow you to remove the full path from the if statement. Hope this helps.

Josef
Life is something to do when you can't get to sleep.