Solved

Bad Grep Variable Syntax

Posted on 2006-06-30
11
316 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

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

Expert Comment

by:James0628
ID: 17033014
Glad I could help.

 James
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Fine Tune your automatic Updates for Ubuntu / Debian
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

689 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