Help Syntax Shell-Script

Hi Guys,

i�ve a question ... but it�s a litte bit tricky:

i�ve a 3 php-scripts which run�s via cron at night. These script reads an xml-file a writes it in an MySQL-DB.
I named them (for example here ) Script1 - Script3. The XML-Files i named xml1 - xml3.

Now, i�ve build a Batch-file, which will start the php-scripts an should write success or error to a log-file ...

And here�s my problem:

When Script1 starts, it reads xml1 - when xml1 is alright, script1 runs till the end and there�s no output - "echo $?" returns with "0"

Now, (error-example) when Script1 starts, it reads xml1 - when xml1 is missing or corrupt, script1 runs till the end too and there�s an output called "Can /srv/www/htdocs/_source/xml-file not read."
This is an order at the end of script1 (and script 2 & 3 of course)
When i type "echo $?" it returns "0", too.

And here�s my second problem:
my logfile should looks like this:

"Script1 was read - success" (-> when script1 ends with no errors, xml-is fine)

"Script1 was read - error" - Can /srv/www/htdocs/_source/xml-file not read." (-> when script1 ends with errors.)

Can somebody help me with the syntax to write my batch-file:
Here�s are my first steps/trials ...

#! /bin/sh
# Description
cd /srv/www/htdocs/import (here are the 3 scripts)

/usr/bin/php5 script1.php;

if [ $ ... don�t know]
then
echo ' Script1 was read - success' ;
else
echo ' Script1 was read - error' - Can /srv/www/htdocs/ ... ' ;

fi;
... and so on till Script3

Many Thanks.
Regards,
Michael

PS:
Hope, my explanations are plausible. If not, feel free to ask ...

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

(sorry for my english)

i supose every of your scripts returns a code,
0 = Ok
1 = No ok
then yo can do:

if script1.sh params
then
....your code
if script2.sh params
then
..... your code....
else
echo "ERROR in script2.sh"
fi
else
echo "ERROR in script1.sh"
fi
------------------------
example with a command:

$more file
if cat $1
then
echo "cat execute OK"
else
echo "cat execute NO-OK"
fi

No - and that is the problem.
i get return-code "0" everytime - even when the script runs till end but the xml-file is corrupt or missing ...

 
 
Here�s an example:
 
XML-File is ok:
/usr/bin/php5 script1.php; echo $? -> returns "0"
 
XML-File is corrupt:
/usr/bin/php5 script1.php; echo $? -> returns "Can /srv/www/htdocs/_source/xml-file not read".0

two ways:
1- Can you modify the sripts?
2- Redirect stdout of scripts.pp, if script is OK the file size is 0 but if script is NO OK the size is not 0 becaus the are the string "Can /srv/www/htdocs/_source/xml-file not read"
/usr/bin/php5 script1.php >/tmp/script1.$$
if [ -s /tmp/script1.$$ ]
then
echo "Error"
else

   fi
   rm -f /tmp/script1.$$

Note probe if thestring "Can /srv/www/htdocs/_source/xml-file not read" go out by stdout or by stderr, if is the last (stderr) de redirection is 2>

---------- Post updated at 09:36 AM ---------- Previous update was at 09:33 AM ----------

if you don tmp files you can probe:

buffer=`/usr/bin/php5 script1.php`
#be sure the content of buffer
echo "[$buffer]"
if [ "$buffer" ]
then
echo "Error"
else
echo "OK"
fi

Version 2 works fine ... many thanks for your help.

---------- Post updated 13-08-09 at 08:19 AM ---------- Previous update was 12-08-09 at 02:58 PM ----------

... have just seen,
when the script runs and there�s an error-message in /tmp/script1.$$
how can the error-message be displayed in the logfile, too?

 
/usr/bin/php5 script1.php >/tmp/script1.$$
if [ -s /tmp/script1.$$ ]
then
echo 'Script 1 was not read - Error - & **display error-message** '
else
echo 'Script 1 was read - Success'
fi
rm -f /tmp/script1.$$
 
echo 'Script 1 was not read - Error -\n `cat /tmp/script1.$$`  '
echo 'Script 1 was not read - Error "
cat /tmp/script1.$$
 
/usr/bin/php5 script1.php >/tmp/script1.$$
if [ -s /tmp/script1.$$ ]
then
echo 'Script 1 was not read - Error'
cat /tmp/script1.$$
else
echo 'Script1 read - Success'
fi
rm -f /tmp/script1.$$
 
/usr/bin/php5 script2.php >/tmp/script2.$$
if [ -s /tmp/script2.$$ ]
then
echo 'Script2 was not read - Error'
cat /tmp/script2.$$
else
echo 'Script2 was read - Success'
fi
rm -f /tmp/script2.$$
 
/usr/bin/php5 script3.php >/tmp/script3.$$
if [ -s /tmp/script3.$$ ]
then
echo 'Script3 was not read - Error'
cat /tmp/script3.$$
else
echo 'Script3 was read - Success'
fi
rm -f /tmp/script3.$$

and my Log:

 
Thu Aug 13 12:34:09 CEST 2009
Script 1 was not read - Error
Can /srv/www/htdocs/_source/xml-file not read.Script2was read - Success
Script3 was read - Success

Is there a chance that the formatting is:

 
Script 1 was not read - Error - Can /srv/www/htdocs/_source/xml-file not read.
Script2 was read - Success
Script3 was read - Success

I tried a few versions, but none of them matched ...

You only need echo "\n" after the cat comand

cat /tmp/script1.$$
echo "\n"
......
cat /tmp/script2.$$
echo "\n"

try this, i think is more clean and optimal code :

for num in 1 2 3
do
          /usr/bin/php5 script${num}.php >/tmp/temp_scrip.$$
    if [ -s /tmp/temp_scrip.$$ ]
    then
        echo 'Script${num} was not read - Error'
        cat /tmp/temp_scrip.$$
        echo "\n" 
    else
        echo 'Script${num} was read - Success'
    fi
done
rm /tmp/temp_scrip.$$

Sorry, but this doesn�t work ... neither "\n" nor "-n" ...

Script:

 
/usr/bin/php5 Script1.php >/tmp/script1.$$
if [ -s /tmp/script1.$$ ]
then
        echo "Script1 was not read - Error"
        cat /tmp/script1.$$
        echo "\n"
else
        echo "Script1 was read - Success"
fi
rm -f /tmp/script1.$$

...
...
...

logfile:

 
Thu Aug 13 15:46:22 CEST 2009
Script1 was not read - Error
Can /srv/www/htdocs/_source/xml-file not read.\n
Script2 was read - Success

Th problem is than echo comand print de characters \n and you want that interpret this characters, you need -e option in echo :wink:

man:
-e enable interpretation of backslash escapes

try this :
echo -e "\n"