We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Grab and import script

operation1612
on
Medium Priority
360 Views
Last Modified: 2013-12-16
I need to create a script tu unzip and call other script...below is the flow. the script will run everynight at 12.

filename: /tmp/file.zip
script to import: /tmp/import.php

1) check the file.zip existing in /tmp folder...
2) if not exist, create a log /var/log/myscript.log [ date, time, file.zip is not exists ]
3) if exist, do unzip /tmp/file.zip and it will have file name file.csv
4) after unzip is finished ( which is quite big size of file.csv, > 30MB ) write log to /var/log/myscript.log [date,time, unzip is successfull 31MB]
5) then only run import script to sql, "import-command /tmp/import.php"
6) after finished, write log to /var/log/myscript.log [date, time, import success ]

how do i start?

#/bin/bash


Comment
Watch Question

Author

Commented:
finally it will remove the file.zip and file.csv
Top Expert 2005

Commented:
#!/bin/bash
filename="/tmp/file.zip"
toimport="/tmp/import.php"
LOGTO=/var/log/myscript.log

if [ -s "$filename" ]; then
 unzip "$filename" -d /tmp #-d tells where to unzip
 [ -s "/tmp/file.csv" ] || { echo "`date`: file.csv not found in the zipfile" >> $LOGTO; exit 1; };
 echo "`date`: file.csv unzipped OK `wc -c /tmp/file.csv`" >> $LOGTO
 import-command /tmp/import.php # what You mean by import-command? Maybe simple: /tmp/import.php /tmp/file.csv # would suffice?
 echo "`date`: file.csv imported OK" >> $LOGTO
else
 echo "`date`: $filename not found" >> $LOGTO;
 exit 1;
fi
Top Expert 2005

Commented:
If import-command return well defined exit codes

 if import-command /tmp/import.php; then
   echo "`date`: file.csv imported OK" >> $LOGTO
   rm -f "$filename" /tmp/file.csv
 else
  echo "`date`: file.csv import FAILED" >> $LOGTO
  #for any sake
  rm -f /tmp/file.csv
  mv -f "$filename" "$filename.failed"
 fi
Top Expert 2005

Commented:
And if You like, You may add more error checking...

Author

Commented:
like this?

#!/bin/bash
filename="/tmp/file.zip"
toimport="/tmp/import.php"
LOGTO=/var/log/myscript.log

if [ -s "$filename" ]; then
 unzip "$filename" -d /tmp #-d tells where to unzip
 [ -s "/tmp/file.csv" ] || { echo "`date`: file.csv not found in the zipfile" >> $LOGTO; exit 1; };
 echo "`date`: file.csv unzipped OK `wc -c /tmp/file.csv`" >> $LOGTO
/tmp/import.php /tmp/file.csv
 echo "`date`: file.csv imported OK" >> $LOGTO
else
 echo "`date`: $filename not found" >> $LOGTO;
 exit 1;
fi

if import-command /tmp/import.php; then
   echo "`date`: file.csv imported OK" >> $LOGTO
   rm -f "$filename" /tmp/file.csv
 else
  echo "`date`: file.csv import FAILED" >> $LOGTO
  #for any sake
  rm -f /tmp/file.csv
  mv -f "$filename" "$filename.failed"
 fi


My Question as below code..between two process, do i need to add break; between fi and if?

echo "`date`: file.csv imported OK" >> $LOGTO
else
 echo "`date`: $filename not found" >> $LOGTO;
 exit 1;
fi

if import-command /tmp/import.php; then
   echo "`date`: file.csv imported OK" >> $LOGTO
   rm -f "$filename" /tmp/file.csv
 else
  echo "`date`: file.csv import FAILED" >> $LOGTO
Top Expert 2005

Commented:
> My Question as below code..between two process, do i need to add break; between fi and if?
I'm not sure I understand Your Q. Please type 'help if' and also note, that every ';' may be replaced with newline break. That's all.

Author

Commented:
i mean is below code is correct because normally i saw script use command break;?

#!/bin/bash
filename="/tmp/file.zip"
toimport="/tmp/import.php"
LOGTO=/var/log/myscript.log

if [ -s "$filename" ]; then
 unzip "$filename" -d /tmp #-d tells where to unzip
 [ -s "/tmp/file.csv" ] || { echo "`date`: file.csv not found in the zipfile" >> $LOGTO; exit 1; };
 echo "`date`: file.csv unzipped OK `wc -c /tmp/file.csv`" >> $LOGTO
/tmp/import.php /tmp/file.csv
 echo "`date`: file.csv imported OK" >> $LOGTO
else
 echo "`date`: $filename not found" >> $LOGTO;
 exit 1;
fi

if import-command /tmp/import.php; then
   echo "`date`: file.csv imported OK" >> $LOGTO
   rm -f "$filename" /tmp/file.csv
 else
  echo "`date`: file.csv import FAILED" >> $LOGTO
  #for any sake
  rm -f /tmp/file.csv
  mv -f "$filename" "$filename.failed"
 fi
Top Expert 2005
Commented:
break exits from within 'loop', not 'if'.

#!/bin/bash
filename="/tmp/file.zip"
toimport="/tmp/import.php"
LOGTO=/var/log/myscript.log

if [ -s "$filename" ]; then
 unzip "$filename" -d /tmp #-d tells where to unzip
 [ -s "/tmp/file.csv" ] || { echo "`date`: file.csv not found in the zipfile" >> $LOGTO; exit 1; };
 echo "`date`: file.csv unzipped OK `wc -c /tmp/file.csv`" >> $LOGTO
 if import-command /tmp/import.php; then # what is the real import-command?
   echo "`date`: file.csv imported OK" >> $LOGTO
   rm -f "$filename" /tmp/file.csv
 else
  echo "`date`: file.csv import FAILED" >> $LOGTO
  #for any sake
  rm -f /tmp/file.csv
  mv -f "$filename" "$filename.failed"
 fi
else
 echo "`date`: $filename not found" >> $LOGTO;
 exit 1;
fi

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
the real import command is /usr/local/bin/php -q

/usr/local/bin/php -q /tm/import.php

the import.php will import the csv to mysql

Author

Commented:
can i do this?  > toimport="/usr/local/bin/php -q"
Top Expert 2005

Commented:
rather toimport="/usr/local/bin/php -q /tmp/import.php"
but one advice, don't keep the import.php in /tmp dir, it;s cleaned periodically.

Author

Commented:
ok thank you...
toimport="/usr/local/bin/php -q /tmp/import.php"

so finally in this line [if import-command /tmp/import.php;] i should change to :

if $toimport; then

correct?
Top Expert 2005

Commented:
correct

Author

Commented:
thank you

Author

Commented:
i need to ask one little question related to this thread.

i test the script and it works fine...i want to move the existing file.zip to /backup/. after process is finished, script will transfer to backup directory and name it by the date. for example fileDDMMYYYY.zip

is it this correct?

#for any sake
  rm -f /tmp/file.csv
  mv -f "$filename" "/backup/$filename.'date' "  <<<<<<
Top Expert 2005

Commented:
yes, it's crrect, but it's executed only, if import fails

Author

Commented:
if import not fails? the command also will move to /backup directory is it?
Top Expert 2005

Commented:
Yes, but it have to copied to the other branch of 'if' as well then (instead rm -f)

Author

Commented:
ok great...i test it and yes the file is move to folder /backup by filename.date, because of the filename is quite big...so i plan to keep it for 4 file only based on the latest date...mean after script run again in future it will do checking either the filename.date in backup directory is not more than 4. old file by date will be remove...

do i need to create new question?
Top Expert 2005

Commented:
rather try searching for nswer here, or for Yourself
hint: find -mtime

Author

Commented:
ok great...thanks... the -mtime...i got it
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.