How do I use grep with a variable? {SOLVED}

Requests go here. If you fill a request, give it a new thread in the appropriate category and then link to it in the request thread.
Post Reply
Message
Author
B.K. Johnson
Posts: 807
Joined: Mon 12 Oct 2009, 17:11

How do I use grep with a variable? {SOLVED}

#1 Post by B.K. Johnson »

Hello
I could not find where to post this programming question, so apologies if I am in the wrong place. But if you can help, please do. Thanks.

Here goes:

/root/InterfacesList has the following lines
ESSID:"gynaecentre"
ESSID:"EDENVALE"
ESSID:"VALLEY HEALTH CARE SERVICES"
ESSID:"LENOX GAYLE"
ESSID:"ARRIS-7D72"
ESSID:"Nexxt_5B3BF8"
ESSID:"LIVING WATERS"
ESSID:"HP-Print-8C-ENVY 4500 series"

A script has:

Code: Select all

TheEssid="LIVING WATERS"
     if (grep "LIVING WATERS" /root/Interfaceslist -c -q);then
             echo $TheEssid found
     else
        echo $TheEssid Not found
     fi
This code finds that LIVING WATERS is included in InterfacesList - returning LIVING WATERS found
I wish to use the variable TheEssid instead of hard-coding LIVING WATERS in the grep line.
How do I do this? Neither replacing LIVING WATERS with TheEssid nor with $TheEssid works nor does $(grep .....
Last edited by B.K. Johnson on Mon 26 Sep 2016, 17:52, edited 1 time in total.
[color=blue]B.K. Johnson
tahrpup-6.0.5 PAE (upgraded from 6.0 =>6.0.2=>6.0.3=>6.0.5 via quickpet/PPM=Not installed); slacko-5.7 occasionally. Frugal install, pupsave file, multi OS flashdrive, FAT32 , SYSLINUX boot, CPU-Dual E2140, 4GB RAM[/color]

User avatar
LazY Puppy
Posts: 1934
Joined: Fri 21 Nov 2014, 18:14
Location: Germany

#2 Post by LazY Puppy »

TheEssid="LIVING WATERS"
if (grep "LIVING WATERS" /root/Interfaceslist -c -q);then
echo $TheEssid found
else
echo $TheEssid Not found
fi
TheEssid="LIVING WATERS"
if (grep "$TheEssid" /root/Interfaceslist -c -q);then
echo $TheEssid found
else
echo $TheEssid Not found
fi
RSH

"you only wanted to work your Puppies in German", "you are a separatist in that you want Germany to secede from Europe" (musher0) :lol:

No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! :wink:

B.K. Johnson
Posts: 807
Joined: Mon 12 Oct 2009, 17:11

#3 Post by B.K. Johnson »

Thanks!
[color=blue]B.K. Johnson
tahrpup-6.0.5 PAE (upgraded from 6.0 =>6.0.2=>6.0.3=>6.0.5 via quickpet/PPM=Not installed); slacko-5.7 occasionally. Frugal install, pupsave file, multi OS flashdrive, FAT32 , SYSLINUX boot, CPU-Dual E2140, 4GB RAM[/color]

Post Reply