Issue using empty variable in grep command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue using empty variable in grep command
# 1  
Old 09-29-2015
Issue using empty variable in grep command

Hi,
I'm writing a shell script and trying to grep a variable value, it works fine as long as there is a value in /tmp/list.out which is captured in $DSK but sometimes the file tends to be empty and that is where I'm having an issue while using grep which returns nothing. I know I can use something like [ -z "$DSK" ] to test if $DSK is empty but I'm trying to see if it would work this way.
Code:
#!/bin/ksh
DSK=`cat /tmp/list.out |grep disk | awk '{print $1}' |cut -c 7-`
for i in `lspv | awk '{print$1}' |grep -v "$DSK"`
 do
  echo $DSK
  if 
  then
        echo "Fail"
  else
        echo "Pass"
  fi
 done
fi

# 2  
Old 09-29-2015
Your first line of code seems to boil down to:
Code:
DSK=$( awk '/disk/ {print substr($1, 7)' /tmp/list.out )

So that seems okay to me. You can have awk call exit 1 if it does not find something:
Code:
DSK=$( awk '/disk/ {print substr($1, 7); exit 0 } END{exit 1 }' /tmp/list.out  || echo 'bad DSK')

Then you know not to go on.

I cannot fathom what you are doing with the rest of the code-
it looks like you are trying to remove something from the output of lspv
derived from /tmp/list.out like maybe a volume group or a pvid.

And what do you want to do with the "blank" if statement? By that I mean what do you expect to find that will tell you DSK is empty? Or the result is bad.

You have to have a way to signal failure for the code to tell you it failed.
with the above first line:
Code:
[ "$DSK" = "bad DSK" ] && echo failure ||
for i in `lspv | awk '{print$1}' |grep -v "$DSK"`
 do
  echo $DSK
  if ????
  then
        echo "Fail"
        break
  else
        echo "Pass"
  fi
 done
fi

What is the i variable supposed to do here, BTW?
# 3  
Old 09-30-2015
Thanks Jim for your response, here is what I'm trying to do, there is a particular disk of certain size in /tmp/list.out that I want to exclude and below is my actual script, it works fine when there is a value for variable DSK otherwise it does nothing if /tmp/list.out is empty i.e., if variable $DSK is empty and I want to continue with the script irrespective of data present in /tmp/list.out since there could other disks that has a failed path.

Code:
DSK=$( awk '/disk/ {print substr($1, 7)}' /tmp/list.out )
for i in `lspv | awk '{print$1}' |grep -v "$DSK"`
 do
  DSK_PATH=`lspath | grep -w $i | grep Ena | wc -l`
  if [ "$DSK_PATH" -lt 2 ]
  then
        echo "Failed path found"
  else
        echo "No failed path"
  fi
 done
fi


Last edited by mbak; 09-30-2015 at 12:57 AM.. Reason: added code tags
# 4  
Old 09-30-2015
Within ` ` and $( ) you can run complete (sub-)shell code.
Code:
for i in `
  if [ -z "$DISK" ]
  then
    lspv | awk '{print$1}'
  else
    lspv | awk '{print$1}' | grep -vw "$DSK"
  fi
`
do
  ..
done

--
wc -l always returns a number, but it can be prefixed with space.
Therefore better do not quote the variable:
Code:
  if [ $DSK_PATH -lt 2 ]

--
On AIX I found the following is more efficient:
Code:
  DSK_PATH=`lspath -l "$i" | grep -c "^Enabled"`


Last edited by MadeInGermany; 09-30-2015 at 09:11 AM.. Reason: added -w
# 5  
Old 09-30-2015
Issue using empty variable in grep command

Thank you MadeInGermany, your suggestion of checking variable value within these
Code:
` `

and
Code:
$( )

for (sub-)shell code helped me in getting this to work, you really made it simple and I think this is because of your Location being SimplicitySmilie
# 6  
Old 10-05-2015
Issue using empty variable in grep command

I'm trying to enhance the script i.e., if there is more than one value for DISK variable but it errors out on nested "for loop" saying "0403-057 Syntax error at line 13 : `for' is not matched"


Code:
for i in `
    for DISK in `awk '/disk/ {print substr($1, 7)}' /tmp/list.out`
    do
        if [ -z "$DISK" ]
        then
           lspv | awk '{print$1}'
        else
           lspv | awk '{print$1}' | grep -vw "$DISK"
        fi
    done
`
do
  .....
done

# 7  
Old 10-05-2015
Swap line 2 and 3 in above.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue executing grep command on Solaris

more jdbc.xml <name>Fast_ds/DataSource</name> <property> <name>user</name> <value>COL_USER</value> </property>Command 1: grep -A1 '<name>user</name>' jdbc.xml|grep -v '<name>user</name>'|sed 's/\(<value>\|<\/value>\)//g'| sed -e 's/^*//'Output: Command 2: grep... (5 Replies)
Discussion started by: mohtashims
5 Replies

2. Shell Programming and Scripting

Variable value substitution issue with awk command issue

Hi All, I am using the below script which has awk command, but it is not returing the expected result. can some pls help me to correct the command. The below script sample.ksh should give the result if the value of last 4 digits in the variable NM matches with the variable value DAT. The... (7 Replies)
Discussion started by: G.K.K
7 Replies

3. Shell Programming and Scripting

Issue in using variable within sed command

Hi All, I am trying to use a variable within the sed command but I am not able to get the output. When I am using the following command (without variable) its working fine: sed -n '/2011\/12\/10 18:11:11./,$p' < Log.txt > Delta_Log.txt But when I am putting the value 2011\/12\/10... (4 Replies)
Discussion started by: acoomer
4 Replies

4. Shell Programming and Scripting

CUT Command and grep issue

I am trying to grep the oracle erros evry day from the logs file. My problem is : -rw-r----- 1 tibcolm tibco 17438361 Apr 5 11:59 RetryService-RetryService.log -rw-r----- 1 tibcolm tibco 245303 Apr 5 12:00 ResponseService-ResponseService.log -rw-r----- 1 tibcolm tibco 2122654 Apr 5 12:00... (4 Replies)
Discussion started by: neeraj617
4 Replies

5. UNIX for Dummies Questions & Answers

Grep command issue

How am I supposed to print with one Unix command the names (and only the names) of company JMT? Problems with scandinavian letters? By typing grep JMT url | egrep --only-matching ']+'I still have company name and + marks I should get rid of. I'd be very grateful of your help bacause I am new... (1 Reply)
Discussion started by: jht
1 Replies

6. HP-UX

Performance issue with 'grep' command for huge file size

I have 2 files; one file (say, details.txt) contains the details of employees and another file (say, emp.txt) has some selected employee names. I am extracting employee details from details.txt by using emp.txt and the corresponding code is: while read line do emp_name=`echo $line` grep -e... (7 Replies)
Discussion started by: arb_1984
7 Replies

7. Shell Programming and Scripting

command to grep the variable from file

Hi, I am looking for the following: 1. A command to 'grep' the variable from a file 2. And check whether the entered variable is empty in that file I have a file as given below: IAGLOBAL_USERID=admin I want to check with a whether the variable contains a value, say here its admin if... (2 Replies)
Discussion started by: dbashyam
2 Replies

8. Shell Programming and Scripting

Issue with passing variable to Grep in a shell script

Hi, I'm trying to check if methods specified in a class have been added to the corrosponding interface. My code below is giving me the following errors: grep: function: No such file or directory grep: import($zipfile): No such file or directory grep: function: No such file or... (1 Reply)
Discussion started by: racshot65
1 Replies

9. Shell Programming and Scripting

issue with grep command

i am using 'psu' to grep the status of a process like below :- psu|grep -i agentf 29559 29552 0 00:53:05 ? 0:57 gn1avm_agent -n AGENTF1 28946 1 0 00:51:31 ? 0:31 gnavm_ewd -b gn1avm_agent -n AGENTF1 -c -n AGENTF1 29552 28946 0 00:53:05 ? 0:00 /bin/ksh... (6 Replies)
Discussion started by: deepakiniimt
6 Replies

10. UNIX for Dummies Questions & Answers

Variable for -name causing issue in Find command

Hi there, I'm trying to find files that are greater then 30 days old, zip them and move to a different directory. I'm encountering an issue passing a variable (FilesToFind) to name within the find command. Here's the code I'm running: #! /usr/bin/sh FileDir=/home/ariba... (2 Replies)
Discussion started by: ParNone
2 Replies
Login or Register to Ask a Question