Previous Topic

Next Topic

Book Contents

Book Index

Reserved Variable $0

The $0 reserved variable can be used in a script to retrieve the name of the script, which is automatically replaced by a file name before the script runs. When a script runs as a Control-M/Server job using the –v flag it is parsed into a temporary script. For more information about Shell types, see Shell types.

Any reference to $0 in the script is resolved to the temporary script name rather than the original script name, and the name of the original script is saved in the CTM0 variable. This differentiates between a script run from the command line and a script run from a Control-M/Server job. To ensure that the $0 variable resolves to the original name when run from the command line and not the temporary script name, set the Translate_$0 flag using the ctmunixcfg utility, as described in ctmunixcfg.

Setting the flag causes Control-M/Agent to replace any occurrence of $0 in the original script with $CTM0 when it parses the original script to the temporary script. This restores the original functionality of the script as if it ran from the command line.

EXAMPLE:

The following example shows the dollar0.sh script, which is supposed to print out the script name.

#!/bin/sh

echo $0

When the script runs as part of a Control-M/Server job using the -v flag, the name of the temporary script is printed.

#! /bin/sh -v

CTM_RSVD=

CTM_RSVD_START=

CTM_RSVD_END=

CTM0='/home/ag900/dollar0.sh'

CTM00=$0

echo $0 $CTM_RSVD

/tmp/ctm/CM_SH.11939

When the script runs in a Control-M/Server job using the -v flag and the Translate_$0 flag is set, the name of the original script is printed.

#! /bin/sh -v

CTM_RSVD=

CTM_RSVD_START=

CTM_RSVD_END=

CTM0='/home/ag900/dollar0.sh'

CTM00=$0

echo $CTM0 $CTM_RSVD

/home/ag90000/dollar0.sh

Parent Topic

Shell Scripts on UNIX