Csh script with Undefined variable error

hi there

I have this C shell script that was migrated from AIX to Linux, could someone please help me, I checked the syntax numerous times but I can't find out where the error is. The script is meant to find files older than 27 days and delete it

#!/usr/bin/csh
#

LOGFILE=/Mailbox/Control/bin/printers/log/`date +%d``hostname`prt.log

#
# Check if logfile exists
#
{
if [ \! -f $LOGFILE ]; then
    find `dirname $LOGFILE` -name '*'`hostname`prt.log -mtime +27 -exec rm {} \;
fi
} >/dev/null 2>/dev/null

Error after running script:

up1as00:/Mailbox/Control/bin/printers # ./sunprint.sh
LOGFILE=/Mailbox/Control/bin/printers/log/12up1as00prt.log: Command not found.
{: Command not found.
LOGFILE: Undefined variable.

I created a dummy file but the error persists

up1as00:/Mailbox/Control/bin/printers # ls -ld /Mailbox/Control/bin/printers/log/12up1as00prt.log
-rw-r--r-- 1 root root 0 Jun 12 11:15 /Mailbox/Control/bin/printers/log/12up1as00prt.log
 

Am I missing something here? (in terms of syntaxing?)

It is not a csh script, but a sh script, so the shebang is wrong. My guess is, it has always been executed as sh /path/to/script , in which case the shebang is treated as comment and is disregarded.. Try with #!/bin/sh

2 Likes

@Scrutinizer, thanks alot, that fixed it!

Not sure since I don't use the 'csh' shell, but try this:

#!/usr/bin/csh
#
set LOGFILE="/Mailbox/Control/bin/printers/log/`date +%d``hostname`prt.log"

#
# Check if logfile exists, if log file does not exist then find other log files older than 27 days and remove them.
#
if ( ! -f $LOGFILE ) then
  find `dirname $LOGFILE` -name "*`hostname`prt.log" -mtime +27 -exec rm -f {} \;
fi

Hi spacebar

I am getting this error

up1as00:/tmp # ./a.sh
fi: Command not found.

---------- Post updated at 02:24 PM ---------- Previous update was at 01:59 PM ----------

@Scrutinizer, I realise you are right, SAP is using sh /dir/dir/xxx.sh to run the script

Another question, does "set -x" work in Linux?

I have another 500++ lines of script and thats also giving me the same error, tho this time I used sh /dir/dir/script.sh to run it and its calling #!/usr/bin/sh

 # /Mailbox/Control/bin/printers/sendsapftp.sh: line 65: set: -
set: usage: set [--abefhkmnptuvxBCHP] [-o option] [arg ...]

[1]+  Exit 2                  sh /Mailbox/Control/bin/printers/sendsapftp.sh
  

Ill paste whats in lines stated in the error message:

set -x

I am suppose to get a file produced out of this, a printer list.txt which of course isn't generated

It should, what is in sendsapftp.sh around line 65?

# GLOBAL Variables
#
set -x

TRANS=/Mailbox/Control/bin

#PATH=.:/export/home/p01adm:/usr/sap/P01/SYS/exe/run:/oracle/P01/bin:/usr/bin:.:/usr/ccs/bin:/usr/ucb

That not line 65. Try

sed -n 60,70p file

<script removed>

What does

sed -n 65p sendsapftp.sh | od -c 

produce?

Im getting this

up1as00:/Mailbox/Control/bin/printers # sed -n 65p sendsapftp.sh | od -c
0000000   s   e   t       -   x  \r  \n
0000010

Somehow your file got converted to DOS format. Try removing the CR characters first:

tr -d '\r' < infile > outfile

Also your shebang should be on line 1 at the first character position, or it will not be used...

Thanks for pointing that out! I am still getting loads of errors when I run the script, its actually gotten worse :frowning:

I am getting strings of this, and I have to hit Enter to stop it

4096
4096
4096
4096
1163 bytes.'
+ '[' '' '!=' '' ']'
+ count=0
++ head -1


[6]+  Stopped                 sh /tmp/sendsapftp.sh

up1as00:/tmp # more sendsapftp.sh
#!/usr/bin/sh
#Directory:  /Mailbox//Control/bin/printers
#Name:       sendsapftp.sh

The script needs 3 parameters, did you provide them on the command line?

yes, I did

up1as00:/tmp # sh /tmp/sendsapftp.sh &P &C &F

What do those mean? & is a special character, a control operator to run the preceding command in the background...
So what probably would happen is that first this gets executed without parameters:

sh /tmp/sendsapftp.sh &

then

P &

then

C &

all of these in the background
and then

F

in the foreground

That I think is the input paramater for printer variables, because someone sent a screenshot (from SAP GUI) and the command used was "ksh <script> &P &C &F

Those are replacement characters in SAP. It also shows that the shell is ksh, not csh, nor sh...

i agree with you, I just changed all the 'shebang' back to /usr/bin/ksh but its still nor running alright, I am getting tons of error

up1as00:/Mailbox/Control/bin/printers # ksh /Mailbox/Control/bin/printers/sendsapftp.sh &
[3] 6584
up1as00:/Mailbox/Control/bin/printers # + TRANS=/Mailbox/Control/bin
+ GREP=/usr/bin/grep
+ EGREP=/usr/bin/egrep
+ ZIPCMD=/usr/bin/zip
+ FTPdir=/Mailbox/Control/bin/printers/ftp
+ file=''
+ basename
basename: missing operand
Try `basename --help' for more information.
+ file1=''
+ idfile=/Mailbox/Control/bin/printers/sendsapftp.ident
+ ctlfile=/Mailbox/Control/bin/printers/sendsapftp.ctl
+ filterfile=/Mailbox/Control/bin/printers/sendsapftp.filter
+ LOGFILE=/Mailbox/Control/bin/printers/log/sendsapftp.log
+ MAILADM=ausalarm@xymon.sca.se
+ FTPLOG=/Mailbox/Control/bin/printers/tmp/sendsapftp_6584.log
+ notify=0
+ spawn=no
+ ls -l
+ awk '{ print $5 }'
+ initial_size=$'\n4096\n188\n183\n10190\n25033\n11896\n18500\n19055\n1032\n1258'
+ [ '' '=' sendsapftp ]
+ LOGFILE=/Mailbox/Control/bin/printers/log/sendsapftp_.log
+ date
+ msg='\n===================== Wed Jun 13 10:31:00 MYT 2012 ======================'
+ LOG_IT
+ DATE=''
+ echo '\n===================== Wed Jun 13 10:31:00 MYT 2012 ======================'
+ 1>> /Mailbox/Control/bin/printers/log/sendsapftp_.log
+ [ '' '!=' '' ]
+ msg='sendsapftp script is called by "" unix queue.'
+ LOG_IT
+ DATE=''
+ echo 'sendsapftp script is called by "" unix queue.'
+ 1>> /Mailbox/Control/bin/printers/log/sendsapftp_.log
+ [ '' '!=' '' ]
+ msg='This is a nested (recursive) call to this script.  Handling locally.'
+ LOG_IT
+ DATE=''
+ echo 'This is a nested (recursive) call to this script.  Handling locally.'
+ 1>> /Mailbox/Control/bin/printers/log/sendsapftp_.log
+ [ '' '!=' '' ]
+ msg=$'Initial (unzipped) file size is \n4096\n188\n183\n10190\n25033\n11896\n18500\n19055\n1032\n1258 bytes.'
+ LOG_IT
+ DATE=''
+ echo $'Initial (unzipped) file size is \n4096\n188\n183\n10190\n25033\n11896\n18500\n19055\n1032\n1258 bytes.'
+ 1>> /Mailbox/Control/bin/printers/log/sendsapftp_.log
+ [ '' '!=' '' ]
+ count=0
+ head -1
[4] 6594

[3]+  Stopped                 ksh /Mailbox/Control/bin/printers/sendsapftp.sh

That script still needs 3 parameters and why are you testing it in the background?