Grep to return a code from accessing variable file name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep to return a code from accessing variable file name
# 1  
Old 02-05-2009
Grep to return a code from accessing variable file name

All I want to do is find out if the string 'NO' is in a file by checking for a return code of 0 if there is a match. The grep works fine on the command line directly naming the file but I am obviously not using the correct syntax within the shell script as I consistently get the error message

rep: 0652-033 Cannot open .

Please could you advise what brackets or other symbols need to be added or changed where?

vNoCheck= `grep 'NO' "$vOracount"`
if [ "$?" -ne "0" ]; then
echo 'No reconciliation needed'
fi

$vOraCount contains the file name, which I have checked by using echo i.e. $vOraCount= /badata/mtdata/extracts/RECON_HIST_PPP.txt

By the way I have just made a posting asking for recommended reading material so I can train myself in the mysteries of Unix punctuation and only go to forums with really difficult issues.

Thanks for your help,

Susan


# 2  
Old 02-05-2009
Code:
grep -q 'NO' "$vOracount" > /dev/null
if [ "$?" -ne 0 ]; then
  echo 'No reconciliation needed'
fi

# 3  
Old 02-05-2009
Code:
if grep -q NO "$vOracount"
then
   echo 'No reconciliation needed'
fi

# 4  
Old 02-06-2009
Sorry the file is still not being recognised

Thanks to both of you but unfortunately I am still getting the error message "grep: 0652-033 Cannot open ." for both of your options. I tried ${vOracount} but that made no difference.

I also tried

if `grep -q NO ${vOracount}`
then
echo 'No reconciliation needed'

This did not give me an error message but is not working - I changed the values in the file and it does not recognise the difference. I also tried

if `grep -q 'NO' ${vOracount}`

and again no error message but does not recognise different values in the file.

Advice would be gratefully received. Particularly after a 2.5 hour journey to work (walk, train, taxi instead of a 47 minutes car journey) - we Brits just can't cope with a few inches of snow

Many thanks,

Susan
# 5  
Old 02-06-2009
Fixed

Sorry, a typo on my part was causing the problem.

Thanks for your help,

Susan
# 6  
Old 02-06-2009
I don't think it was your typo, Susan. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using grep and a parameter file to return unique values

Hello Everyone! I have updated the first post so that my intentions are easier to understand, and also attached sample files (post #18). I have over 500 text files in a directory. Over 1 GB of data. The data in those files is organised in lines: My intention is to return one line per... (23 Replies)
Discussion started by: clippertm
23 Replies

2. Shell Programming and Scripting

Unzipping the empty file sends a non Zero return code

Hi, I have a shell script where I am trying to unzip bunch of files zip files one by one. But out of many zip files, if any one zip file is empty, the unzip command sends a non-zero return code and fails the script. I am trying to see if I can get a success error code even if the script... (2 Replies)
Discussion started by: Saanvi1
2 Replies

3. Shell Programming and Scripting

Accessing a variable from another file

HI all, How can i access a variable that is defined in another file as: $$Name= "abhinav; in my script. The catch is that it has 2 $s behind it... Thnaks ---------- Post updated at 10:36 AM ---------- Previous update was at 10:29 AM ---------- the file from which i have to... (0 Replies)
Discussion started by: abhinav192
0 Replies

4. UNIX for Dummies Questions & Answers

| help | unix | grep - Can I use grep to return a string with exactly n matches?

Hello, I looking to use grep to return a string with exactly n matches. I'm building off this: ls -aLl /bin | grep '^.\{9\}x' | tr -s ' ' -rwxr-xr-x 1 root root 632816 Nov 25 2008 vi -rwxr-xr-x 1 root root 632816 Nov 25 2008 view -rwxr-xr-x 1 root root 16008 May 25 2008... (7 Replies)
Discussion started by: MykC
7 Replies

5. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

6. Programming

Accessing environmet variable in xml file

Hi I have certain environmenet variables defined in .profile: export DBNAME="mydb" export DBHOST="devbox.us.abc.com" export DBPORT="1111" export PASSWORD="mypwd" export MMUSER="myid" I also have an xml file defined like: <CONFIG> <DEFAULTS operator="oraread"> <PROPERTY... (2 Replies)
Discussion started by: neeto
2 Replies

7. Shell Programming and Scripting

COMPARING STRING VALUES TO A VARIABLE and return code dignostics

Hey Folks and Gurus there I am trying to replicate this logic in bash : (1) if ; then function1 parameters & function2 parameters & fi Is there an easy method esp one with regex that can do this comparision. e.g. $var is compared to this list ( case regardless ) (... (1 Reply)
Discussion started by: sieger007
1 Replies

8. Shell Programming and Scripting

Return code of command assigned to variable

How do I evaluate the result of a command assigned to a variable?? Example: var1=`cmd` rc=$? rc will be the result of the assignment rather than cmd since it executes after. How do I evaluate the result of the command itself? Cheers..:confused: (2 Replies)
Discussion started by: browndr
2 Replies

9. UNIX for Dummies Questions & Answers

to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 's

Hi All, Can anyone please let me know the syntax / how to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 'system()' function and '${?}'. I am in a process to send the mail automatically with an attachment to bulk users. I have used 'Mailx' and 'Unencode'... (0 Replies)
Discussion started by: manas6
0 Replies

10. UNIX for Dummies Questions & Answers

accessing shell script variable in file

Hi, I have a shell script in which there is a file conn_$temp where $temp has the pid of the shell script. in this shell script i have an embedded awk script that must read the file while ((getline < "conn_$temp") > 0) However due to the "$temp" in the file name, the awk script is... (6 Replies)
Discussion started by: HIMANI
6 Replies
Login or Register to Ask a Question