Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Issue with passing variable to Grep in a shell script

Shell Programming and Scripting


Tags
bash, grep

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 08-25-2011
racshot65 racshot65 is offline
Registered User
 
Join Date: Aug 2011
Last Activity: 10 December 2011, 5:18 PM EST
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
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:


Code:
grep: function: No such file or directory
grep: import($zipfile): No such file or directory
grep: function: No such file or directory
grep: export($backup_file: No such file or directory
grep: =: No such file or directory
grep: null): No such file or directory

It looks like grep is trying to use the pattern I'm giving it as the file ? I'm not sure why though

I've made the line where I think the issue is bold

For reference these are the two lines in WorkingClass.txt


Code:
	public function import($zipfile) {
	public function export($backup_file = null) {

and these are the two lines in WorkingInterface.txt


Code:
    public function import($zipfile);
	public function export($backup_file = null);



Code:
#!/bin/bash
#set -x 

# Navigate to the model directory
cd `dirname $0`/../../webapp/_lib/model

#Get the number of lines in the class We need to pipe the output of WC to awk as it prints the name of the file also) 
classLength=$(wc -l WorkingClass.txt | awk '{print $1}')

# Variable to keep track of which line were on
counter=0

# Variable to track if we find the method in the interface
errors=false


#For each method in the class
while [ $classLength -ne $counter ]
do

# Method were looking for
methodToCheck=$(head -1 WorkingClass.txt)
methodToCheck=${methodToCheck/'{'/''}
#echo 'Looking for' $methodToCheck

#Check its in interface
grep -q $methodToCheck WorkingInterface.txt

#echo $?

if [ $? = "0" ]; then
	#remove the top line of working class and increment counter
	sed -i -e "1d" WorkingClass.txt
	#echo 'Found ' $methodToCheck 

else
	#echo $methodToCheck ' not present in ' $interfaceToCheck
	errors=true
	sed -i -e "1d" WorkingClass.txt
fi


#Get the next method
counter=`expr $counter + 1`


done

#echo 'Problems = ' $errors


Any idea how to fix the problem ?

Thanks Linux

Last edited by pludi; 08-25-2011 at 11:04 AM..
Sponsored Links
    #2  
Old Unix and Linux 08-25-2011
itkamaraj's Unix or Linux Image
itkamaraj itkamaraj is offline Forum Advisor  
Registered User
 
Join Date: Apr 2010
Last Activity: 8 December 2016, 3:28 AM EST
Posts: 3,130
Thanks: 43
Thanked 687 Times in 663 Posts
$methodToCheck --> there may be multiline value in this variable


Code:
grep -q "$methodToCheck" WorkingInterface.txt



---------- Post updated at 07:18 PM ---------- Previous update was at 07:15 PM ----------


Code:
$ a="one two three" 
$ echo $a
one two three

$ echo "one two three four" > mytest

$ grep $a mytest 
grep: two: No such file or directory
grep: three: No such file or directory
mytest:one two three four

$ grep "$a" mytest 
one two three four

Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Passing a variable from shell script to mysql query? kero Shell Programming and Scripting 2 06-10-2009 02:37 PM
Passing parameters to Shell script for GREP command sud.tech Shell Programming and Scripting 1 12-23-2008 08:32 AM
Passing a regexp to grep via a shell script rogersed Shell Programming and Scripting 2 11-05-2008 12:28 AM
error in passing a variable to sqlplus from a shell script kripssmart Shell Programming and Scripting 3 07-08-2008 08:01 PM
passing awk variable to the shell script bcheaib Shell Programming and Scripting 3 07-21-2004 11:00 AM



All times are GMT -4. The time now is 07:54 PM.