Simple if script

Hi,

new to unix and scripting, and i'm trying to set up a simple "if" script to create a seperate flag file dependant on success.
So far i have the following ($5 is a variable passed to the script from the backup job)

if [ '$5' == '0' ]
  then
    touch /u03/backups/backup_ended.flag
else
    touch /u03/backups/backup_failed.flag
fi

I dont have any troubleshooting i can share, as the script can only be started at the end of the backup..

thanks

Rich

You're using the wrong quotes for this. And, a non-portable comparison. Try:

if [ "$5" = '0' ]
  then
    touch /u03/backups/backup_ended.flag
else
    touch /u03/backups/backup_failed.flag
fi

Welcome...
Remember to always give the shell you are using...( Yes syntax may vary...)
What I dont get is $5... Is it the backup job that calls this script?

its a netbackup bpend_notify script if that helps any?

at the start it says  
#! /bin/sh

Thanks for the suggestion Don, it didnt work though. I know that the bpend script is being called at the end of the backup as just having a simple "touch filename" create the file.

The variables i'm trying to use are listed as

 # This script is called by NetBackup when bpbkar is finished doing a
  # backup on the client. It is also called after backing up the files
  # for a user directed archive, but before the files are deleted.
  #
  # This script:
  #       receives 5 parameters: CLIENTNAME POLICYNAME SCHEDNAME SCHEDTYPE STATUS
  #       must be executable by the root user
  #       should exit with 0 upon successful completion

It's double equal to "=="

if [ "$5" == '0' ]
  then
    touch /u03/backups/backup_ended.flag
else
    touch /u03/backups/backup_failed.flag
fi

or, you can also try this

if [[ "$5" -eq 0 ]]
  then
    touch /u03/backups/backup_ended.flag
else
    touch /u03/backups/backup_failed.flag
fi

Probably this could be the reason you are receiving error message, Don's approach is right, whenever you are asking questions please mention OS , and shell you are using.

$ cat f
a=0
[ "$a" == '0' ] && echo 'ok'

$ sh f
f: 5: [: 0: unexpected operator

$ bash f
ok

$ cat f1
a=0
# with or without single quote works..
[ "$a" -eq '0' ] && echo 'ok'

$ sh f1
ok

---------- Post updated at 09:03 PM ---------- Previous update was at 08:50 PM ----------

@SriniShoo

if[ "$a" = '0' ]

OR

if[ "$a" == '0' ] 

It depends on shell being used, for example

$ cat f
a=a
[ "$a" = 'a' ] && echo 'ok'
[ "$a" == 'a' ] && echo 'ok'

$ sh f
ok
f: 5: [: a: unexpected operator

$ bash f
ok
ok

if i do an

echo $SHELL

it comes back with

/sbin/sh

if that changes anything?

Like Don mentioned: the proper syntax is a single = sign [ "$5" = 0 ] . Even though some shells tolerate == , it is not portable.

Not really, other than now we can make an educated guess and say that you are using a Solaris system.

To know what shell you're using for this script, we need to know how the script was invoked, and, unless it was invoked by giving its filename as an operand to a command interpreter, we need to see the 1st few lines of the script itself. Since you said earlier that the 1st line of the script is:

#! /bin/sh

so this script is probably being run by a Bourne shell.

Saying:

doesn't help us help you. How did you determine that it didn't work? Did it touch the wrong file? Did it issue a diagnostic message? Did it fail to touch either file?

Please get into the habit of supplying details about what happened and about what didn't work so the volunteers here can more accurately diagnose what is going on and help you find a solution to your problems expeditiously.

Using a Bourne shell:

if [ "$5" == '0' ]
     and
if [ '$5' == '0'

should both issue a diagnostic message,
but:

if [ "$5" = '0' ]
     and
if [ "$5" -eq '0' ]

should both work correctly and produce the same results as long as $5 expands to an integer that fits into a int or a long depending on the vintage of the shell. I prefer the 1st form because it will also correctly test for equality even if the given argument is an empty string, a number that is out of the range supported for arithmetic operations by your shell, or contains text instead of numeric value.

Please add the following statement as a new line of code just before your if statement:

printf 'Before if: $5 is "%s"\n' "$5"

run the script again, and tell us exactly what happened.

It could also be the statically linked version of the POSIX shell on HPUX .

Don was right, it is Solaris.

ive attached an unmodified version of the bpend script that i am trying to put my if statement into, if that helps.
ive not been able to run Don's test yet as the server is having some patches applied.

this script runs as part of a backup using Netbackup. so the backup will run, and when it completes, it will trigger this script if it exists (it doesnt have to exist, its used for running post backup commands).
so all i'm trying to do is to get it to create a flag file that says "successful" is the backup status is 0, and "failed" if it isnt 0

---------- Post updated at 04:49 PM ---------- Previous update was at 04:46 PM ----------

ah, it wont let me upload, i will paste it in instead

#! /bin/sh
# $Header: bpend_notify.sh,v 1.3 2003/08/13 14:11:54 $
#
#bcpyrght
#***************************************************************************
#* $VRTScprght: Copyright 2013 Symantec Corporation, All Rights Reserved $ *
#***************************************************************************
#ecpyrght
#
# bpend_notify.sh
#
# This script is called by NetBackup when bpbkar is finished doing a
# backup on the client. It is also called after backing up the files
# for a user directed archive, but before the files are deleted.
#
# This script:
#     receives 5 parameters: CLIENTNAME POLICYNAME SCHEDNAME SCHEDTYPE STATUS
#     must be executable by the root user
#     should exit with 0 upon successful completion
#
# If this script will not complete within a few seconds, you should set
# the BPEND_TIMEOUT in the /usr/openv/netbackup/bp.conf file on the server.
# You should also be aware that the time taken by this script will slow
# down other backups that are waiting for this client to complete.
#
# This script should be installed with mode 555 so that user directed
# backups and archives will be able to execute this script.
#
# CAUTION:  writing anything to stdout or stderr will cause backup problems
#



# --------------------------------------------------------------------
# main script starts here
# --------------------------------------------------------------------

umask 022

if [ "$#" -ne 5 ]
then
      exit 1
fi

if [ "$4" = "FULL" -o "$4" = "INCR" -o "$4" = "CINC" ]
then
        OUTF=/usr/openv/netbackup/bin/BPEND_CALLED

      # You may want to delete the output file elsewhere in order to
      # accumulate successful backup information.
      # If so, comment out the following 4 lines. 
      if [ -s $OUTF ]
      then
            /bin/rm -rf $OUTF
      fi

      if [ ! -f $OUTF ]
      then
            touch $OUTF
      fi

      case "$4"
      in
            "FULL")
                  echo `date` full backup finished on $1 - policy $2 schedule $3. Exit status = $5 >> $OUTF
                  ;;
            "INCR")
                  echo `date` differential incremental backup finished on $1 - policy $2 schedule $3. Exit status = $5 >> $OUTF
                  ;;
            "CINC")
                  echo `date` cumulative incremental backup finished on $1 - policy $2 schedule $3. Exit status = $5 >> $OUTF
                  ;;
      esac

      #
      # might want to mail this info to someone
      #
      # cat $OUTF | mail -s "NetBackup backup finished" someone_who_cares
      #
      # CAUTION:  some platforms do not allow the -s parameter on mail
      #
fi

exit 0

Can you show us the contents of /usr/openv/netbackup/bin/BPEND_CALLED from the last run?

Tue Apr 8 16:17:20 BST 2014 cumulative incremental backup finished on hadn-ppof-001 - policy PPF_CTC_ORA_IL3_daily schedule DailyInc. Exit status = 0

Given what we now know, I see no reason why:

if [ "$5" = '0' ]
then
        touch /u03/backups/backup_ended.flag
else
        touch /u03/backups/backup_failed.flag
fi

should not work if you insert those lines just before the exit statement at the end of your script.

Please also add the lines:

set -xv
exec 2>/usr/openv/netbackup/bin/BPEND_TRACE

just before the above code and show us the contents of /usr/openv/netbackup/bin/BPEND_TRACE after the script runs again.