grep returns many values into a variable error


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers grep returns many values into a variable error
# 8  
Old 03-16-2011
Yes..that would work as well, but why would this particular script blow up when more than one file exists?
# 9  
Old 03-16-2011
In post #2 it was pointed out that your "if" tests were faulty.
They become even more faulty when $FND and $STR expand to multiple words.
You have kept the faulty lines in your latest version.

Quote:
if [ $FND > '' ]
This line contains three errors.
1) $FND is not in double quotes. Thus if it contains nothing or more than one value it will break the "if".
2) > is not a valid "test" parameter. Your "if" is therefore testing the result of the command line containing the redirection (redirection caused by ">").
3) '' is a null string which is also not allowed in an "if" statement.

I prefer to use this sort of syntax because it is robust for most values of $FND and works in most Bourne-type shells. Here we are using a correct string comparison operator "=" and extending the string on both sides with and "X" character to prevent syntax errors.
The "!" means "not".
Code:
if [ ! "${FND}""X" = "X" ]



I've anticipated that you will want the list of files containing error messages to be presented in the email as a vertical list not all on the same line. This is one good reason to use a workfile not an environment variable.
# 10  
Old 03-16-2011
Sorry...I had sent you my original. Here is the one with the updates:
Code:
    FND="$(find ediout* -mmin -60)"
#   if [ $FND > '' ]; then
    if [ -n "$FND" ]; then
         STR="$(find ediout* -mmin -60 -exec grep -l "ERRORS Encountered" {} +)"
    fi
#   email the results
    if [ $STR > '' ];  then
        echo "${STR}" | mailx -s "EDI Translation Errors List" test@optonline.net 
        The following file(s): ${STR} may contain errors during translation.
MAILER
    fi

Moderator's Comments:
Mod Comment Use code tags, please...

Last edited by Scott; 03-16-2011 at 03:50 PM..
# 11  
Old 03-16-2011
Quote:
if [ $STR > '' ]; then
This line contains the same errors.

The error from the mailer suggests that the line contains single quotes not double quotes.
# 12  
Old 03-16-2011
I missed that line.....it works!Smilie
Thanks so much.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

error while updating rows in sql with values in array variable

Hi, I need to update rows in a table based on the values in an array variable. code is : while read line do error_msg="$(echo $line)" index=`expr $index+1` done <"logs/$ffile" rows_count=${#error_msg } i=0 while do echo "error msgs is... (2 Replies)
Discussion started by: RP09
2 Replies

2. Shell Programming and Scripting

Function returns a value but cannot be stored in other variable

I need help to store the value returned from the function to one variable and then use that variable. PREVIOUS_DATE_FUNCTION() { date '+%m %d %Y' | { read MONTH DAY YEAR DAY=`expr "$DAY" - 1` case "$DAY" in 0) MONTH=`expr "$MONTH" - 1` case... (1 Reply)
Discussion started by: aroragaurav.84
1 Replies

3. Shell Programming and Scripting

If test grep.... always returns 0 status

Hi all. I am trying to compare and filter two files. I have a bigfile.txt of names and ids and a smallfile.txt of ids only. What I am trying to do is use a while read loop to read the ids in the bigfile and then echo the name and id only if the id exists in the small file. Basically, I'm trying to... (5 Replies)
Discussion started by: jameswatson3
5 Replies

4. Shell Programming and Scripting

sqlplus returns leading carriage return into a variable

I am trying to generate some scripts to help manage an Oracle database. When I check the value returned from Oracle it has a leading carriage return in the variable. Is there a way to prevent this? Is there a way to easily strip out the carriage return. See code and output below. ... (7 Replies)
Discussion started by: Panzer993
7 Replies

5. Shell Programming and Scripting

Using grep returns partial matches, I need to get an exact match or nothing

I’m trying to modify someone perl script to fix a bug. The piece of code checks that the zone name you want to add is unique. However, when the code runs, it finds a partial match using grep, and decides it already exists, so the “create” command exits. $cstatus = `${ZADM} list -vic | grep... (3 Replies)
Discussion started by: TKD
3 Replies

6. Shell Programming and Scripting

Grep returns nothing

Hi all, I am trying to grep a .txt file for a word. When I hit enter, it returns back to $ The file is 4155402 in size and is named in this way: *_eveningtimes_done_log.txt I use this command, being in the same directory as the file: grep -i "invalid" *_eveningtimes_done_log.txt ... (16 Replies)
Discussion started by: DallasT
16 Replies

7. Shell Programming and Scripting

If the grep command returns any result set

my intension is to use a grep command inside the shell script and if any row is returned or not.. depending on the resultset i have to code on further. how to check this i mean.. could anyone help me out with the if condition how to use it here !! (4 Replies)
Discussion started by: gotam
4 Replies

8. Shell Programming and Scripting

Function returns wrong values - solved

Hi I have a small function which returns a wrong value. The function tries to make a connection to oracle database and tries to get the open_mode of the database in the variable status. However when a database is down the value of the status column is set to READWRITE i am not sure why. I... (0 Replies)
Discussion started by: xiamin
0 Replies

9. Shell Programming and Scripting

find/grep returns no matches

Hi all! I've faced with very unintelligible error using find/grep like this: root@v29221:~# find /var/www/igor/data/www/lestnitsa.ru | grep u28507I get nothing as a result, but: root@v29221:~# grep u28507 /var/www/igor/data/www/lestnitsa.ru/_var.inc $db_name = 'u28507';... (2 Replies)
Discussion started by: ulrith
2 Replies

10. UNIX for Dummies Questions & Answers

Grep without returns...

Is there a command where I can pipe my grep into it and it will output it with spaces rather than returns? Example I want to turn prompt$ grep blah file blah blah into this prompt$ grep blah file | someCommand blah blah (1 Reply)
Discussion started by: mrwatkin
1 Replies
Login or Register to Ask a Question