Solved

Bad Grep Variable Syntax

Posted on 2006-06-30
11
299 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
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…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

760 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

20 Experts available now in Live!

Get 1:1 Help Now