[Novalug] Unix script help request

Kevin Starkey kevin.linuxfan at gmail.com
Sat Feb 28 01:57:02 EST 2009


Garrett's help was much appreciated, as were all of the helpful hits I 
received.

I hammered out the script early Monday morning and got it all tested and 
working 'perfectly' before noon. The script will run as the first step 
of a daily JCL job (CA-7 triggered), followed by an FTP step. A similar 
job will run Monthly. There's likely to be only 1 or 2 files out there 
on the Unix server on most days; maybe a few more than that on certain 
days perhaps.

One of the tests I ran I had two files out there, one with the string I 
was looking for and one without it. By the time I submitted the job and 
flipped over to IOF the job had already finished, so I'm pretty 
satisfied with the scripts' efficiency.

Take care,

James Ewing Cottrell 3rd wrote:
> Well, it has been said that any program that works is a good one, but it 
> can be done more eloquently:
>
> grep -l pattern files |
> while read name
> do
> 	mv $name S$name
> done
>
> Or, if you are into using xargs:
>
> grep -l pattern files | xargs -i @ mv @ S@
>
> I hate to be a wet blanket, but there are several things wrong with 
> Garrett's script:
>
> [1] grep is executed once per file rather than once only.
> [2] grep -l will quit reading when a file once a pattern. His version 
> unnecessarily reads the rest of the file after the first match.
> [3] anytime you test $? you missed an opportunity to use if:
>
> 	if grep ....
> 	then it_matched
> 	else it_did_not
> 	fi
> [4] this is more of a pet peeve of mine, but I would rather use a case 
> statement to do string matching:
>
> 	case $? in
> 	(0) success;;
> 	(*) failure;;
> 	esac
>
> The reason is that the == (or is it = ?) and -eq are the *opposite* from 
> what perl uses, and in my mind, Perl's version make the most sense.
>
> One way to debug this is to place an echo in front of the mv to see what 
> kind of commands will be generated. Then, when you are done, either take 
> the echo out, or pipe it to sh.
>
> And yes, you can pipe both into and out of a while statement:
>
> grep -l pattern files | while read x; do echo mv $x S$x; done | sh -x
>
> grep -l pattern files | xargs -i @ echo mv @ S@ | sh -x
>
> JIM
>
> Garrett Nievin wrote:
>   
>> One possible solution:
>>
>> If you do a grep with the "-q" option (quiet), it will just set the
>> exit code to 0 if the text was found and 1 if it was not.  You can
>> check the exit code using the shell variable ?.  The question mark
>> character is the name of the variable.
>>
>> Here's some nice structured mainframe-y code:
>>
>> # see if $loopfile contains the magic string, and do something accordingly
>> grep -q '<TRANS_CDE>RSCF' $loopfile
>> if [ $? -eq 0 ] ; then
>> 	echo "string was found"
>> else
>> 	echo "string was not found"
>> done
>>
>>
>>
>> On Sat, 21 Feb 2009 08:32:38 -0500
>> Kevin Starkey <kevin.linuxfan at gmail.com> wrote:
>>
>>     
>>> Hi all,
>>>
>>> My Linux experience (meager as it is) is finally starting to be 
>>> applicable at my job (mainframe programmer). We just started using a 
>>> Unix file server on the mainframe and occasionally we run into an issue 
>>> to solve and I volunteered to tackle this one. The issue is, I might 
>>> have 0, 1, or > 1 files in a directory with the same name pattern of 
>>> "EBPV************.out.xml" and one of these files might be the one I'm 
>>> looking for, or it might not. I need to identify a particular string 
>>> inside of the file, and if it matches, then I want to add an 'S' to the 
>>> start of the name (SEBPV....).
>>>
>>> Below is the start of a script that I hope is close to what I need, but 
>>> I need to add "grep" and a conditional statement where I only do the 
>>> renaming if the grep finds what I'm looking for, but I'm not sure how to 
>>> code that part (this is my first script).
>>>
>>> ---------------------------------------------------------------------------------------------------
>>> #!/bin/bash
>>>
>>> # Change to directory
>>> cd /proj/preed/output
>>>
>>> #Get all needed files in current directory
>>> originalFiles=$(ls EBPV*.out.xml)
>>>
>>> # Loop through all files and do your changes
>>> for loopFile in $originalFiles
>>> do
>>> # Create your new filename including the extension
>>> mv $loopFile S$loopFile
>>> done
>>> ---------------------------------------------------------------------------------------------------
>>>
>>> I need to somehow add:
>>>
>>> grep $loopfile "<TRANS_CDE>RSCF"
>>>
>>> and only if it's found then I want to do the "mv ...." (renaming).
>>>
>>> Any help would be greatly appreciated.
>>>
>>> Thanks,
>>> Kevin.
>>>
>>> _______________________________________________
>>> Novalug mailing list
>>> Novalug at calypso.tux.org
>>> http://calypso.tux.org/cgi-bin/mailman/listinfo/novalug
>>>
>>>       
>>
>> _______________________________________________
>> Novalug mailing list
>> Novalug at calypso.tux.org
>> http://calypso.tux.org/cgi-bin/mailman/listinfo/novalug
>>
>>
>> ------------------------------------------------------------------------
>>
>>
>> No virus found in this incoming message.
>> Checked by AVG - www.avg.com 
>> Version: 8.0.237 / Virus Database: 270.11.2/1963 - Release Date: 02/20/09 19:22:00
>>
>>     
>
> _______________________________________________
> Novalug mailing list
> Novalug at calypso.tux.org
> http://calypso.tux.org/cgi-bin/mailman/listinfo/novalug
>
>   
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://calypso.tux.org/pipermail/novalug/attachments/20090228/c67159b4/attachment.html 


More information about the Novalug mailing list