The shell script doesn't get arguments, but parse them

Hello,
I have a simple shell script, which starts from crontab like this:

 00 03 * * 2-6 /export/applications/dte/sh/fwmarg.sh > /export/applications/dte/data/cron_log/fwmarg.cronlog.`date +\%m.\%d` 2>&1
 

The script doesn't get any argument. But inside it I see the line

 parse_args $@
 

I don't understand, why it need to parse arguments, if it doesn't get any arguments when it starts from crontab?

Thanks for contribution

We would have to see the actual parse_args function to give you a good answer. All of the scripts we run against our huge datasets have a common include shell script, i.e., another small script is sourced. Keeps our common modules easy to maintain - just look in only one place.

The argument parsing it does loops over available arguments. If none are present there are a few default values used. So it does not "care" if there are arguments or not.

So. This is what someone else does. What your script does, I do not know for sure.

The script start Perl script with several arguments. The Perl script or cerate report or update database
below is parse_args function

 parse_args () {
        echo "VARVAR $#"
        if [ $# -gt 0 ]
        then
                export THREAD_ARG="$@"
                while [ ! -z "$1" ]
                do
                        case $1 in
                                -P* | -p* )
                                        export PROCESS_DATE=$2
                                        export MM=`echo $2 | cut -c5-6`
                                        export DD=`echo $2 | cut -c7-8`
                                        export YY=`echo $2 | cut -c3-4`
                                        export PROCESS_DATE_US_MMDDYY="$MM/$DD/$YY"
                                        shift
                                        ;;
                                -OUT* | -OUt* | -Out* | -out* | -oUT* | -ouT* |-oUt* )
                                        if [ -d $2 ]
                                        then
                                                export OUTPUT_DIR=$2
                                        fi
                                        shift
                                        ;;
                                -IN* | -In* | -in* | -iN* )
                                        if [ -d $2 ]
                                        then
                                                export INPUT_DIR=$2
                                        fi
                                        shift
                                        ;;
                                -LOG* | -LOg* | -Log* | -log* | -lOG* | -loG* |-lOg* )
                                       if [ -d $2 ]
                                        then
                                                export LOG_HOME=$2
                                        fi
                                        shift
                                        ;;
                                -COO* | -COo* | -Coo* | -coo* | -cOO* | -coO* |-cOo* )
                                        if [ -d $2 ]
                                        then
                                                export COOL_DIR=$2
                                        fi
                                        shift
                                        ;;
                                -CBD* | -CBd* | -Cbd* | -cbd* | -cBD* | -cbD* |-cBd* )
                                        COOLBDAY=$2
                                        shift
                                        ;;
                                -TES* | -TEs* | -Tes* | -tes* | -tES* | -teS* |-tEs* )
                                        TEST="Y"
                                       ;;
                                -RELOAD* | -RESEND* | -reload* | -resend* | -Reload* | -Resend* )
                                        RELOAD="Y"
                                        ;;
                                SERVERMODE* | servermode* | ServerMode* )
                                        tmpmode=`echo ${1} | awk -F'=' '{print ($2)}' | tr a-z A-Z`
                                        export SERVERMODE="${tmpmode}"
                                        ;;
                                DBENV* | dbenv* | DBenv* | DBEnv* )
                                        tmpmode=`echo ${1} | awk -F'=' '{print ($2)}' | tr a-z A-Z`
                                        export DBENV="${tmpmode}"
                                        if [ -f ${DTE_WORKING_DIR}/sh/oracle.env ]; then
                                                . ${DTE_WORKING_DIR}/sh/oracle.env
                                        fi
                                        ;;
                                -DB* | -db* | -Db* | -dB*)
                                        ORACLE_USER=$2
                                        shift
                                        ;;
                                -NOPAGE* | -Nopag* | -NOPag* | -nopage* )
                                        export PAGEMODE="OFF"
                                        ;;
                                -Userdata* | -USERDATA* | -Userdata* | -userdata* | -UserData* )
                                        export _USER_DATA=$2
                                        shift
                                        ;;
                                * )
                                        ;;
                        esac
                        shift
                done
        fi
}

  
  
 

Thanks for contribution

It skips over the arguments -- if no arguments it just goes on. Now whether that is correct for job to run well, I cannot know. Your problem.

        echo "VARVAR $#"
        if [ $# -gt 0 ]
        then
          .. remove lots of code here 

       fi

$# is a count of command line arguments passed to the script.

1 Like

ok, thanks

Yikes!

Whoever wrote this has never heard of the getopts command or shell shell-built-in. Even if this (miraculously) works, you should replace it by a function using getopts .

UNIX (and systems alike like Linux) provides a parser for commandline parsing already and there is no need to replace that - and definitely not with one that works less reliable.

I hope this helps.

bakunin

this is a very old shell program, which I convert to Perl. And as I said, there is no arguments shell script receives

It would appear the author intended the script to be used in ways other than just how it's called from the cronjob. If you remove that from the script, can you guarantee it will not impact something else (e.g. where else is the script called from, and how is it called)?

I will convert several scripts to Perl and we will move them to different server. So there is no impact on others.

Thank you for your advise. I appreciate it.

I understand the part about reimplementing it, so my point about not using getopts is moot. The script not getting any arguments, though, is a keen assumption on your part: it might be that the script if called from cron doesn't get any arguments, but it might be called somewhere else (and with arguments, for that matter). You might want to give this thought some consideration, maybe by calling:

find / -type f -exec /usr/local/bin/checkforscript {} <yourscriptname> \; 2>/dev/null

Where /usr/local/bin/checkforscript looks like:

#! /bin/ksh

if file "$1" | grep -qi script ; then
     grep "$2" /dev/null "$1"
fi

exit 0

Notice that the added /dev/null in the grep-call makes grep display the filename of the files in which hits are found.

I hope this helps.

bakunin

I am doing it now.

Thank you