?
Solved

Bad Grep Variable Syntax

Posted on 2006-06-30
11
Medium Priority
?
322 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

 
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
 
LVL 35

Accepted Solution

by:
James0628 earned 1000 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 1000 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 35

Expert Comment

by:James0628
ID: 17033014
Glad I could help.

 James
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.:
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

770 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