Help with UNIX scripting (mailx and for loops)

Status
Not open for further replies.

Saint Nick

Lifer
Jan 21, 2005
17,722
6
81
I'm very new to UNIX scripts. I'm trying to write a script that looks for a string in a certain type of files, then copy those files to my home directory on the UNIX box.

Here is what I have so far, and it works as it should...

Code:
find *.sas -exec grep -l "garmstrong" {} \;

But how do I take the list of files it outputs, and copy them to my home directory? Would I need to do something like this?

Code:
cp fileorig filedest

Content restored by People for the Ethical Treatment of Search Engines.
Markbnj
Programming moderator
 
Last edited by a moderator:

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
You could pipe that to xargs and I would suggest using -print0 and I think it's -0 on the xargs side, that causes the arguments to be null terminated instead of space terminated.
 

dinkumthinkum

Senior member
Jul 3, 2008
203
0
0
Two ways, not using xargs:

Using "logical AND" operator to find (-a):
Code:
find *.sas -exec grep -l "garmstrong" \{\} \; -a -exec cp \{\} ~ \;

Using a sh-style for loop:
Code:
for f in `grep -l "garmstring" *.sas`; do cp "$f" ~; done
 

Saint Nick

Lifer
Jan 21, 2005
17,722
6
81
Thanks to everyone who helped me with my previous issue.

Now, I am simply trying to send a list of files via e-mail to a user. List of files is stored within the $files variable. So far, I only have the following

Code:
mailx -s "Asset Suite Data Load Errors" "The following files contain errors:\n"
and

Code:
for f in $files
do
    echo $f
done
So, when the user views it, it is in a list format rather than inline.

UNIX scripting is so fun and I'm learning so much. I'm having a hard time understanding how data "works" in single commands with the pipe.
 
Status
Not open for further replies.