Failure: if grep "$Var" "$line" inside while read line loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Failure: if grep "$Var" "$line" inside while read line loop
# 1  
Old 10-05-2018
Failure: if grep "$Var" "$line" inside while read line loop

Hi everybody,

I am new at Unix/Bourne shell scripting and with my youngest experiences, I will not become very old with it Smilie

My code:
Code:
#!/bin/sh

set -e
set -u

export IFS=

optl="Optl"
LOCSTORCLI="/opt/lsi/storcli/storcli"

($LOCSTORCLI /c0 /vall show | grep RAID | cut -d " " -f-5 | sed 's/   / /1') > /opt/lsi/storcli/output.txt

 while read line
 do
    echo "$line" > /opt/lsi/storcli/templine.txt                  #only to test "grep" in "if" with a file
    if grep -qv "$optl" "$line"                                   # Testfile-Location /opt/lsi/storcli/templine.txt;   
        then
            fehler="Failure on: Controller 0"
        else
            fehler="1"
    fi
 done < "/opt/lsi/storcli/output.txt"

Some background information

content of output.txt
Code:
0/0 RAID1 Optl              <-- as seen in Notepad++ there are CR and LF "signs at the end of the line, maybe this information is important
1/1 RAID1 Optl                <-- as seen in Notepad++ there are CR and LF "signs at the end of the line

content of templine.txt at the first time of the while-loop:
Code:
0/0 RAID1 Optl

content of templine.txt at the second time of the while-loop:
Code:
1/1 RAID1 Optl


My Problem with the code

Code:
"if grep -qv "$optl" "$line"

doesn't work --> Error messages: "grep: 0/0 RAID1 Optl: No such file or directory" and "grep: 1/1 RAID1 Optl: No such file or directory"
[/CODE]
BUT

Code:
"if grep -qv "$optl" /opt/lsi/storcli/templine.txt

works as expected. But I would prefer to work with the $line-variable.

What did I wrong. Where is the/my mistake? I spend the last 3 nights for this simple code and did a lot of google research :-(


Than you very much for support!!!
# 2  
Old 10-05-2018
Quote:
Originally Posted by Subsonic66
Code:
"if grep -qv "$optl" "$line"

doesn't work --> Error messages: "grep: 0/0 RAID1 Optl: No such file or directory" and "grep: 1/1 RAID1 Optl: No such file or directory"

BUT

Code:
"if grep -qv "$optl" /opt/lsi/storcli/templine.txt

works as expected. But I would prefer to work with the $line-variable.
Sorry, but you set yourself up for that: it usually works as soon as you do it right. ;-))

Seriously: grep expects its arguments in the form (i simplify to the relevant here): grep <pattern> <file>. What you fed it instead, though, is: grep <pattern> <variable, that is: string which to search for pattern>. This is why grep treated the string you wanted to be searched as a filename - and of course this filename is not found, which is why you got the error of "file not found".

There is in fact another form of grep-invocation, where you can feed grep something from <stdin>, like this:

Code:
some_process | grep <pattern>

To get your string there is possible but you first have to create a process with the string as output. Can you guess which process that could be?

Yes, of course: echo "$variable". Therefore, here is your solution:

Code:
echo "$variable" | grep <pattern>

Picture grep as a machinery which has two different intakes: a file or <stdin>. Just like a coffee machine might have two intakes, one for ground coffee and one for whole beans. Which one is selected depends on how it is invoked. But you cannot pour your data into the wrong intake and then expect grep to do it right: put your beans into the opening for ground coffee and the coffee machine will make all sorts of things but probably not coffee.

I hope this helps.

bakunin

PS: CR/LF line endings are indeed important and will confuse a lot of UNIX utilities. I suggest to correct them to UNIX newlines first thing. There are unix2dos and dos2unix, two utilities which change back and forth from/to UNIX-style line breaks from/to DOS-style line breaks, sed-scripts and many other ways of achieving the same.

PPS: you do not need to create a temporary file at all in your code: as i have shown you you can directly work with the variable content.

Last edited by bakunin; 10-05-2018 at 06:59 PM..
This User Gave Thanks to bakunin For This Post:
# 3  
Old 10-05-2018
Welcome to the forum.


The command structure is
Code:
grep -options pattern file

That means the $line variable needs to hold a filename which it obviously doesn't. You could "grep" (pls. note the quotes - there may be better ways to check) a variable's contents like
Code:
echo $var | grep pattern

.
This User Gave Thanks to RudiC For This Post:
# 4  
Old 10-06-2018
I salute to Bakunin and RudiC SmilieSmilieSmilie


Code:
echo "$variable" | grep <pattern>

works perfect! Smilie


Thank you so much for the solution. A solution I never would have found by myself. Smilie


Bankunin, its to early in the morning in Germany, so I only could test with $line and grep. Later on this weekend I will test the same solution with the temporary file.
# 5  
Old 10-06-2018
Often shell-builtins are the better alternative.
Code:
  case "$line" in
  *"$optl"*)
    fehler="1"
  ;;
  *)
    fehler="Failure on: Controller 0"
  ;;
  esac


Last edited by MadeInGermany; 10-06-2018 at 05:16 AM.. Reason: swapped order to match the grep -v in post#1
# 6  
Old 10-08-2018
Bankunin, now I have corrected the code, so that I also don't need a file anymore. Thanks again for your support!


Subsonic
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml"

Dear Ladies & Gents, I have a requirement to delete all the log files in /var/log/test directory that are older than 10 days and their first line begin with "MSH" or "<?xml" or "FHS". I've put together the following BASH script, but it's erroring out: for filename in $(find /var/log/test... (2 Replies)
Discussion started by: Hiroshi
2 Replies

2. Shell Programming and Scripting

Bash - How to do a "read -p" inside a while loop?

Hi there guys! I was trying to do: while read line; do if ; then read -p "Press Enter to continue..." cont=0 fi echo $line let cont++ done < file.txt However, I have read that the read -p would not work in a while loop... I was wondering if there is any other way to... (2 Replies)
Discussion started by: rplae
2 Replies

3. Shell Programming and Scripting

Move a line containg "char" above line containing "xchar"

Okay, so I have a rather large text file and will have to process many more and this will save me hours of work. I'm not very good at scripting, so bear with me please. Working on Linux RHEL I've been able to filter and edit and clean up using sed, but I have a problem with moving lines. ... (9 Replies)
Discussion started by: rex007can
9 Replies

4. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

5. Shell Programming and Scripting

Find lines with "A" then change "E" to "X" same line

I have a bunch of random character lines like ABCEDFG. I want to find all lines with "A" and then change any "E" to "X" in the same line. ALL lines with "A" will have an "X" somewhere in it. I have tried sed awk and vi editor. I get close, not quite there. I know someone has already solved this... (10 Replies)
Discussion started by: nightwatchrenba
10 Replies

6. Programming

fgets read file line with "\n" inside

Hi, I have a string like this, char str ="This, a sample string.\\nThis is the second line, \\n \\n, we will have one blank line"; if I want to use strtok() to seperate the string, which token should I use? I tried "\n", "\\n", either not working. peter (1 Reply)
Discussion started by: laopi
1 Replies

7. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

8. Shell Programming and Scripting

ps -ef | grep "string1" "string2" " "string3"

Hi all, can any one suggest me the script to grep multiple strings from ps -ef pls correct the below script . its not working/ i want to print OK if all the below process are running in my solaris system. else i want to print NOT OK. bash-3.00$ ps -ef | grep blu lscpusr 48 42 ... (11 Replies)
Discussion started by: steve2216
11 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question