tim cwik on 14 Mar 2012 12:02:48 -0700


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] Need some script advice


On 03/13/2012 11:25 AM, Sam Gleske wrote:
On Tue, Mar 13, 2012 at 10:47 AM, Michael Leone<turgon@mike-leone.com>  wrote:
Note that I am not a Linux scripting guy by ANY stretch of the
imagination. But I do have a simple script that I could use some
advice on.

I have a script that we use to transfer FTPed invoices from a machine
on our DMZ to the trusted LAN. This script reaches out to the DMZ and
runs a script that that ZIPs up all invoices it finds. Then my script
RSYNCs it from the DMZ to the trusted LAN. This all works.

Next, my script extracts out from the zip all the individual files,
and moves them to a special folder. Then it makes a copy of the zip
file in an archive folder. Then it deletes the zip from the working
directory (since we safely have it in the archive).

Well, somehow the script hiccupped the other day, and did NOT copy the
zip from the working directory to the archive before deleting it. (no,
I don't know why yet - possibly a credentials problem, the interactive
run may not have been done by the right account. That's a separate
problem).

What I need to do: verify that the script exists in the archive BEFORE
deleting. If it doesn't exist, I want it to email me.

I know I can do (thank you Google, for the example):

if [ -f zip-file-name]
then
     echo 'Adding to archive'
     cp *.zip /Archive
     mailx -s "All good!" ...
else
     echo 'File not found!'
     mailx -s "Problem making archive! Come look" .....
fi

Here's what I don't know how to do - determine that zip-file-name, so
I can check it. There will only ever be 1 zip file in the working
directory. How do I capture that name, to then feed it into the code
snippet above. I guess what I am asking:

how can I save the name of the single file that is in this directory
to a variable? (I will then use that variable in the test above)

I'm sure it's simple, I just don't know how ...
RECOMMENDED SOLUTION:
Before I answer your script question, why do you need to copy?  Why
not mv the file to the archive?  This way if mv fails then the file
just isn't moved and there's no problem.  Then you could check if the
file is still in the current working directory instead of checking the
archive.

You could easily do it like this...
for file in *.zip;do
     if mv $file /Archive/; then
         echo "$file successfully added to archive."
         mailx -s "All good!" ...
     else
         echo "Failed moving $file to archive!"
         mailx -s "Problem making archive! Come look" .....
     fi
done

I use the exit status of the mv command to determine rather than
checking the file still exists.  See how if statements work with exit
codes by reading the Bash Beginners Guide.
http://tldp.org/LDP/Bash-Beginners-Guide/html/chap_07.html

ANSWER TO ORIGINAL SCRIPTING QUESTION:
Assuming there is, and will always will be only one zip file you can
simply assign a variable like:
zipfilename=$(ls *.zip)
#or alternatively
zipfilename=`ls *.zip`

Even though you are sure there will never be more than one .zip file, you still may want to check zip_count=`ls *.zip |wc -l` and then check $zip_count is 1. If not send mail because there are other, possibly larger, problems.

For what it's worth.
and then run the code you want.

If you have more than one zip file then you'll need to run a loop.

for file in *.zip;do
     if [ -f /Archive/$file]
     then
	echo 'Adding to archive'
	cp *.zip /Archive
	mailx -s "All good!" ...
     else
	echo 'File not found!'
	mailx -s "Problem making archive! Come look" .....
     fi
done


-- SAM
___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug

___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug