• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Help with UNIX scripting (mailx and for loops)

Status
Not open for further replies.
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:
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.
 
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
 
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.
Back
Top