Link to home
Start Free TrialLog in
Avatar of KellyCraig
KellyCraig

asked on

Bad Grep Variable Syntax

Ok, so I have this in a script, and I was told $? returns 0 or 1 with grep.
But when I run the script, I get this error.

19 boston:/net/adm/scripts-> sudo ./printreport-beta
0
Variable syntax
20 boston:/net/adm/scripts->

Does anyone see a mistake in the scripting?
PS: If I grep -c the file /tmp/print.log, I get no results, and thats what I want.
Those who printed return the logs, and for me, I did not print, so nothing returns.
Thsi script should NOT send mail to those who return no lines from /tmp/print.log
but mail those who do return lines.

Help?

Script:
--
#!/bin/csh

##############################################
#Print report tool for EOL - made by (kcraig)#
#Gets what they have been printing and mails  #
#it to each user.                            #
##############################################

#Set the variables
set prefix="/var/lurker/logs"
set searchstring="Print:"
set tmpfile="/tmp/nightmail/mailbod"
set date=`date +%m/%d/%y`

#check if correct host
if ($HOST != "boston") then
        echo ""
        echo printreport must be run from boston/lurker
        echo ""
        exit
endif

#set the user list of names
set siggroup="kcraig"

#make the directory
mkdir /tmp/nightmail

#cd to the directory
cd $prefix

#create print file
grep $searchstring authlog >& /tmp/print.log


#begin getting and sending mail to SIG-group
foreach user ($siggroup)
       grep -w -c $user /tmp/print.log
       if ($? == "1")
       exit
       endif
       echo "This is an automatically generated E-Mail to remind you CIT\($user) of your print jobs for $date.">& $tmpfile-$user.tmp
       echo "Please pick them up from the specified printer if you have not already done so.">>& $tmpfile-$user.tmp
       echo "Note: Only jobs from CIT (Windows) based machines will appear below.">>& $tmpfile-$user.tmp
       echo "--------------------------------">>& $tmpfile-$user.tmp
       echo "">>& $tmpfile-$user.tmp
       grep -w $user /tmp/print.log >>& $tmpfile-$user.tmp
       mailx -r "printserver" -s "Your EOL Print Report for $date"  $user@eol.ucar.edu < $tmpfile-$user.tmp
       rm -rf $tmpfile-$user.tmp
end


#clean up
rm -rf /tmp/nightmail
rm -f /tmp/print.log
--
Avatar of KellyCraig
KellyCraig

ASKER

PS: It never makes it to cleanup.
if I ls /tmp after I get the error those two files are there and didnt get removed.

clean up:
--
#clean up
rm -rf /tmp/nightmail
rm -f /tmp/print.log
--
It's been a while since I touched csh but it seems like you have a syntax error somewhere

Replace
#!/bin/csh

With
#!/bin/csh -x

And run it again to give you more debugging output....Go Sox!
Ok, now I get this.

--

2 boston:/net/adm/scripts-> sudo ./printreport-beta
Password:
set prefix=/var/lurker/logs
set searchstring=Print:
set tmpfile=/tmp/nightmail/mailbod
set date=`date +%m/%d/%y`
date +%m/%d/%y
if ( boston != boston ) then
set siggroup=kcraig
mkdir /tmp/nightmail
cd /var/lurker/logs
foreach user ( kcraig )
grep -w -c kcraig /tmp/print.log
2
Variable syntax
3 boston:/net/adm/scripts->

--
Okay it sees a syntax problem with your next line the one after grep

You have...
if ($? == "1")

Try changing that to...
if ( "$?" == "1" ) then

I'm not a csh expert but that line is where your problem is

Hmm..

Still get this.

6 boston:/net/adm/scripts-> sudo ./printreport-beta
set prefix=/var/lurker/logs
set searchstring=Print:
set tmpfile=/tmp/nightmail/mailbod
set date=`date +%m/%d/%y`
date +%m/%d/%y
if ( boston != boston ) then
set siggroup=kcraig
mkdir /tmp/nightmail
cd /var/lurker/logs
foreach user ( kcraig )
grep -w -c kcraig /tmp/print.log
2
Variable syntax
7 boston:/net/adm/scripts->
ASKER CERTIFIED SOLUTION
Avatar of James0628
James0628

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you're using csh, there you get the status in $status not in $?
If I change it to $status as shown below, I get this error.

Error:
--
2 boston:/net/adm/scripts-> sudo ./printreport-beta
0
if: Empty if
3 boston:/net/adm/scripts->



Code:
--
#begin getting and sending mail to SIG-group
foreach user ($siggroup)
       grep -w -c $user /tmp/print.log
    if ("$status" == "1")
    exit
    else
       echo "This is an automatically generated E-Mail to remind you CIT\($user) of your print jobs for $date.">& $tmpfile-$user.tmp
       echo "Please pick them up from the specified printer if you have not already done so.">>& $tmpfile-$user.tmp
       echo "Note: Only jobs from CIT (Windows) based machines will appear below.">>& $tmpfile-$user.tmp
       echo "--------------------------------">>& $tmpfile-$user.tmp
       echo "">>& $tmpfile-$user.tmp
       grep -w $user /tmp/print.log >>& $tmpfile-$user.tmp
       mailx -r "printserver" -s "Your EOL Print Report for $date"  $user@eol.ucar.edu < $tmpfile-$user.tmp
       rm -rf $tmpfile-$user.tmp
    endif
end
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
NAILED!

thank you all so very much!
I am splitting the poitns between James and veedar!

--Kelly
Glad I could help.

 James