Solved

Bad Grep Variable Syntax

Posted on 2006-06-30
11
302 Views
Last Modified: 2008-02-01
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
--
0
Comment
Question by:KellyCraig
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 4

Author Comment

by:KellyCraig
ID: 17019949
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
--
0
 
LVL 15

Expert Comment

by:veedar
ID: 17020282
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!
0
 
LVL 4

Author Comment

by:KellyCraig
ID: 17020534
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->

--
0
 
LVL 15

Expert Comment

by:veedar
ID: 17021276
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

0
 
LVL 4

Author Comment

by:KellyCraig
ID: 17021463
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->
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 34

Accepted Solution

by:
James0628 earned 250 total points
ID: 17023941
I've never used csh, but it looks like the problem is the $?.  In the Bourne or Korn shell (sh or ksh), that gives you the status of the last command, but it looks like in csh you use a variable named status.  I think the "Variable syntax" message is because csh doesn't understand $?.

 Try

 if ($status == 1) then


 James
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 17025062
you're using csh, there you get the status in $status not in $?
0
 
LVL 4

Author Comment

by:KellyCraig
ID: 17031187
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
0
 
LVL 15

Assisted Solution

by:veedar
veedar earned 250 total points
ID: 17031404
Change it to...

if ("$status" == "1") then
0
 
LVL 4

Author Comment

by:KellyCraig
ID: 17031482
NAILED!

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

--Kelly
0
 
LVL 34

Expert Comment

by:James0628
ID: 17033014
Glad I could help.

 James
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Fine Tune your automatic Updates for Ubuntu / Debian
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now