Operating System - Linux
1752590 Members
4489 Online
108788 Solutions
New Discussion юеВ

Usage error within script with grep/egrep

 
SOLVED
Go to solution
erics_1
Honored Contributor

Usage error within script with grep/egrep

Hello all,

I have what hopefully will be an easy fix but I haven't been able to resolve the problem yet. I have the following line in a script that gives a usage error when runnning with set -x:

ABORTED_OBJECTS=`omnirpt -report session_objects -session $SESSIONS|egrep -i 'FileSystem|RawDisk'|grep $HOST_NAME|wc -l`

I can run this omnirpt command from the command line without issue but the script shows a usage error. Any insight would be greatly appreciated.

Thanks,
Eric
8 REPLIES 8
erics_1
Honored Contributor

Re: Usage error within script with grep/egrep

A bit more clarification....

The usage error comes from egrep. Here's the output from replacing egrep with grep -E:

+ grep -e FileSystem -e RawDisk
+ wc -l
+ grep
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvx] -e pattern_list...
[-f pattern_file...] [file...]
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvx] [-e pattern_list...]
-f pattern_file... [file...]
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvx] pattern [file...]
ABORTED_OBJECTS=0

Thanks,
Eric
Denver Osborn
Honored Contributor
Solution

Re: Usage error within script with grep/egrep

Howdy Eric,

Is the usage error from omnirpt or one of the greps?

check that SESSIONS and HOST_NAME variables aren't null.

I ran a similar script on a box for testing, and it went w/ out error.

#!/bin/sh

SESSIONS="2005/10/20-1"
HOST_NAME=nodename

ABORTED_OBJECTS=`omnirpt -report session_objects -session $SESSIONS|egrep -i 'FileSystem|RawDisk'|grep $HOST_NAME|wc -l`

echo Aborted = $ABORTED_OBJECTS


hope this helps,
-denver
Bill Hassell
Honored Contributor

Re: Usage error within script with grep/egrep

This is a bit simpler and replaces the deprecated construct using grave accents with $() and splits each filter onto a separate line:

set -u
ABORTED_OBJECTS=$(omnirpt -report session_objects -session $SESSIONS \
| grep -i -e FileSystem -e RawDisk \
| grep $HOST_NAME|wc -l)

egrep (grep -E) can be used with multiple search terms but -e is a simpler method and doesn't need quoting to protect simple terms. The set -u is always desirable to protect against spelling errors. The shell will silently replace undefined variables with a null value, whereas set -u will abort the script indicating that an undefined variable was used as an argument ($HOST_NAME not spelled right or not assigned?)


Bill Hassell, sysadmin
Denver Osborn
Honored Contributor

Re: Usage error within script with grep/egrep

ahh... that looks like $HOST_NAME in the 2nd grep is null.

-denver
Pat Lieberg
Valued Contributor

Re: Usage error within script with grep/egrep

Do you want the output of the command assigned to the variable? If so, it should be like this:

ABORTED_OBJECTS=$(omnirpt -report session_objects -session $SESSIONS|egrep -i 'FileSystem|RawDisk'|grep $HOST_NAME|wc -l)

Jean-Luc Oudart
Honored Contributor

Re: Usage error within script with grep/egrep

If the variable exists but is empty you would get such message
May be you could put the command into a vaiable and display it before you try to evaluate it :
cmd="omnirpt -report session_objects -session $SESSIONS|egrep -i 'FileSystem|RawDisk'|grep $HOST_NAME|wc -l"
echo $cmd

Regards
Jean-Luc
fiat lux
erics_1
Honored Contributor

Re: Usage error within script with grep/egrep

Good call Denver,

This script had been in place for some time. Due to the addition of some rawdisk backups, it had to be modified. Sometimes you get convinced where the problem lies and that's where you waste a lot of time. HOST_NAME hadn't been set yet which I believe will fix the usage errors. Thanks to all for your replies and time!

Best Regards,
Eric
erics_1
Honored Contributor

Re: Usage error within script with grep/egrep

See above