Sponsored Content
Top Forums Shell Programming and Scripting Failure: if grep "$Var" "$line" inside while read line loop Post 303024305 by Subsonic66 on Friday 5th of October 2018 05:28:09 PM
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!!!
 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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. 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

7. 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

8. 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

9. 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
All times are GMT -4. The time now is 06:24 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy