Running Multiple scripts based on file size.

Hi,

I have created 3 shell scripts which has to run one by one first two shell scripts will create a .txt files...which are used by the third shell script.Now I want to create a master script and run all these in a single script.

Please give a pseudo code on how to so the same.

master_script.sh

I want to call my scripts in the below way.

srcloc=/bin/main/scripts/
chmod 777 1.sh
chmod 777 2.sh
chmod 777 3.sh

sh 1.sh if success then call sh 2.sh
else throw an error message(1.sh failed) fail the enter master script
if sh 2.sh is success then call 3.sh
else throw an error message(2.sh failed) fail the enter master script

###if 2.sh is successfully executed it will create a blr_src.txt file before running the 3.sh I have to check if the file blr_src.txt is > 0 bytes(i.e it has some value) if yes then it has to call the 3.sh (and if file is empty then) else throw a error message that file is empty and exit the master script.

call the third script as below

if file blr_src > 0 then sh 3.sh payment blr_src.txt (payement is the variable name for 3.sh) after 3.sh is completed exit the master script.

Regards,
Deepti

Try this

#!/bin/bash

srcloc=/bin/main/scripts/
chmod 777 1.sh
chmod 777 2.sh
chmod 777 3.sh

function err
{
  ret=$1
  scr=$2
  
  if [ $ret -ne 0 ]
  then
    echo "Error : $src. Exiting..."
    exit 1
  fi
}

$srcloc/1.sh
err "$?" "1.sh failed"

$srcloc/2.sh
err "$?" "2.sh failed"

fileSize=0
if [ -f $srcloc/blr_src.txt ]
then
  fileSize=`stat -c %s $srcloc/blr_src.txt >/dev/null 2>&1`
fi

if [ $fileSize -eq 0 ]
then
  err "1" "blr_src.txt is empty"
fi

$srcloc/3.sh
err "$?" "3.sh"

echo "master.sh executed successfully..."
exit 0

regards,
Ahamed

Hi Ahamed,

Thanks for the script. can you please explain me the below part.

function err
{
  ret=$1
  scr=$2
  
  if [ $ret -ne 0 ]
  then
    echo "Error : $src. Exiting..."
    exit 1
  fi
}

And the below part

if [ $fileSize -eq 0 ]
then
  err "1" "blr_src.txt is empty"
fi

the above part is giving a error when executing the script.., the error is showing as error at line and showing this script.., if I remove this part the script is working.

Regards,
Deepti

chmod 777 1.sh
chmod 777 2.sh
chmod 777 3.sh

No no no. 777 is not the magic sledgehammer fix to all permission walls. These numbers actually mean something, and have consequences farther than what you wanted. Do you really want your script files to be world-writable -- as in, any user whatsoever on the system can modify them, even by accident, even system daemons and random passers by?

chmod 750 1.sh

function err{} - just a function to print the error message if any. Else we will have to print the error message everywhere. The arguments are $1 - return value;success 0;failure > 0 and $2 - Any message you want to print.

So when you do the below call

$srcloc/1.sh err "$?" "1.sh failed"

First argument will be the return value of the execution of the script 1.sh. Second argument will be the error message it will print if at all the script failed.

Coming to the error part - can you paste the exact error your getting.
Try this code

if [ "$fileSize" -eq 0 ] 
then   
err "1" "blr_src.txt is empty" 
fi

May be fileSize is not populated correctly. Debug it by printing the values or running it in debug mode using -x option from command line. Check the path of blr_src.txt, may thats incorrect.

regards,
Ahamed