LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-20-2011, 02:45 AM   #1
Basher52
Member
 
Registered: Mar 2004
Location: .SE
Distribution: Arch
Posts: 401

Rep: Reputation: 22
Question how to compare $var with content of file


This is a simple thing but I just can't get it to work.

I got a file with a list of subnets like this:

10.50
10.51
10.62
10.121

...then I cnf=`ifconfig` thus giving me the config of the NICs

...after that I want to compare the $cnf to see if the value of it is
listed in the file and if it is do things.
There might also be something better to use then the 'ifconfig' but it worked so I just stuck to it

First I just had one subnet but now it's starting to grow and I wanna make a list instead of having them all listed in the if-statement.

I know this gotta be possible since it's Linux but whatever I do won't work

Last edited by Basher52; 05-20-2011 at 05:04 AM.
 
Old 05-20-2011, 02:54 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Please post your script and show how it does not work
 
Old 05-20-2011, 03:00 AM   #3
Basher52
Member
 
Registered: Mar 2004
Location: .SE
Distribution: Arch
Posts: 401

Original Poster
Rep: Reputation: 22
well I can't post it since I haven't really save it
I just "test" it at the command prompt before I do.
I can post this at least:

Code:
if [ `ifconfig | grep -ci 10.200` != 0 ]; then
        /bin/eject /dev/cdrom ; reboot -f
elif [ `ifconfig | grep -ci 10.70` != 0 ]; then
        /bin/eject /dev/cdrom ; reboot -x
else echo Unknown network. Please tell me what to do...
and now each of the if statement will now have more then just one, so far we're up to 12 of subnets and it's still growing therefore I wanna put them in a file.
 
Old 05-20-2011, 03:11 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
If you have a list of subnets in a file you can reverse the logic and see if a specific subnet (from the ifconfig command) is listed into the file itself. Example with a while read loop taking input from the output of ifconfig parsed with awk:
Code:
while read subnet
do
  if grep -qw $subnet file
  then
    echo $subnet
  fi
done < <(/sbin/ifconfig | awk '/inet addr:/ && ! /127.0.0.1/{sub(/inet addr:[0-9]+\.[0-9]+\./,""); print $1}')
where file in the grep command is your saved list. The awk line excludes the lo address from the search and extract the subnet (two last octets of the IP address). Hope this helps.
 
Old 05-20-2011, 03:52 AM   #5
Basher52
Member
 
Registered: Mar 2004
Location: .SE
Distribution: Arch
Posts: 401

Original Poster
Rep: Reputation: 22
thx that was fast
wish I was familiar with 'awk' as you are. Love that command but I can't handle it

Last edited by Basher52; 05-20-2011 at 04:32 AM.
 
Old 05-20-2011, 04:20 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
/sbin/ifconfig | awk '
/inet addr:/ && ! /127.0.0.1/ {
  sub(/inet addr:[0-9]+\.[0-9]+\./,"")
  print $1
}'
Actually not so complicated: you have two expressions to match your input line concatenated by the logical AND operator &&. It matches the regular expression /inet addr:/ and doesn't match (that is excludes, see the negation operator !) the loopback address /127.0.0.1/.

The rules is quite simple: the sub fuction substitutes the text matched by /inet addr:[0-9]+\.[0-9]+\./ with the null string. In other words it removes the string
Code:
inet addr:XXX.YYY.
from the input, leaving only the last two octets of the IP address into the first field. Hope it's a bit more clear.

If you want to learn more about awk I suggest to read the following:
http://www.gnu.org/software/gawk/manual/ the official GNU awk guide
http://www.grymoire.com/Unix/Awk.html the awk tutorial at grymoire's site

Last edited by colucix; 05-20-2011 at 04:26 AM.
 
Old 05-20-2011, 04:39 AM   #7
Basher52
Member
 
Registered: Mar 2004
Location: .SE
Distribution: Arch
Posts: 401

Original Poster
Rep: Reputation: 22
Doesn't seem to 'want' this tho:

Code:
while read subnet

 do if grep -qw $subnet ./subnet.adm; then
        /bin/eject /dev/cdrom ; reboot -x
 elif if grep -qw $subnet ./subnet.utb; then
        /bin/eject /dev/cdrom ; reboot -f
 else echo Unknown subnet: $subnet. Script canceled and eagerly awaits instructions...
fi

done < <(/sbin/ifconfig | awk '/inet addr:/ && ! /127.0.0.1/{sub(/inet addr:[0-9]+\.[0-9]+\./,""); print $1}')
I get a "syntax error near unexpected token 'done'"
Is there no possibility to have this in a if-else statement?

I gotta read up on 'awk' and understand that command
 
Old 05-20-2011, 04:53 AM   #8
Basher52
Member
 
Registered: Mar 2004
Location: .SE
Distribution: Arch
Posts: 401

Original Poster
Rep: Reputation: 22


'... elif if...'

lol
 
Old 05-20-2011, 05:11 AM   #9
Basher52
Member
 
Registered: Mar 2004
Location: .SE
Distribution: Arch
Posts: 401

Original Poster
Rep: Reputation: 22
Code:
while read subnet

 do if grep -qw $subnet ./subnet.adm; then
        /bin/eject /dev/cdrom ; reboot -x
 elif grep -qw $subnet ./subnet.utb; then
        /bin/eject /dev/cdrom ; reboot -f
 else echo Unknown subnet: $subnet. Script canceled and eagerly awaits instructions...
fi

done < <(/sbin/ifconfig | awk '/inet addr:/ && ! /127.0.0.1/{sub(/inet addr:[0-9]+\.[0-9]+\./,""); print $1}')
I removed the error I created but then I get error at another place:

Code:
/bin/ftl: line 19: syntax error near unexpected token `<´
/bin/ftl: line 19: `  done < <(/sbin/ifconfig | awk ´/inet addr:/ && ! /127.0.0.1/{sub(/inet addr:[0-9]+\.[0-9]+\./,""); print $1}´)´
These lines are typed manually so I might have written some stuff wrong

Last edited by Basher52; 05-20-2011 at 05:12 AM.
 
Old 05-20-2011, 05:37 AM   #10
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Hi,

I ran your script and did not get a syntax error. Which shell are you using?

Last edited by crts; 05-20-2011 at 05:42 AM.
 
Old 05-20-2011, 05:55 AM   #11
Basher52
Member
 
Registered: Mar 2004
Location: .SE
Distribution: Arch
Posts: 401

Original Poster
Rep: Reputation: 22
It's at work for a BootCD from Novell ZENworks 10.3.3 using SUSE

UPDATE: Linux (none) 2.6.32.19-0.3-default

Last edited by Basher52; 05-20-2011 at 05:59 AM.
 
Old 05-20-2011, 06:07 AM   #12
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by Basher52 View Post
It's at work for a BootCD from Novell ZENworks 10.3.3 using SUSE

UPDATE: Linux (none) 2.6.32.19-0.3-default
No, what I meant are you using bash, csh or something else? AFAIK not every shell does support process substitution.
 
Old 05-20-2011, 06:09 AM   #13
Basher52
Member
 
Registered: Mar 2004
Location: .SE
Distribution: Arch
Posts: 401

Original Poster
Rep: Reputation: 22
bash

Last edited by Basher52; 05-20-2011 at 06:10 AM.
 
Old 05-20-2011, 06:17 AM   #14
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by Basher52 View Post
bash
Are you sure? And did you post the complete script?

I copy+pasted your script and ran it twice with different sha-bangs.
Code:
#!/bin/bash
while read subnet
...
done < <(/sbin/ifconfig | awk '/inet addr:/ && ! /127.0.0.1/{sub(/inet addr:[0-9]+\.[0-9]+\./,""); print $1}')
This did not return a syntax error.

This, however,
Code:
#!/bin/sh
while read subnet
...
done < <(/sbin/ifconfig | awk '/inet addr:/ && ! /127.0.0.1/{sub(/inet addr:[0-9]+\.[0-9]+\./,""); print $1}')
returned the error
Code:
$ ./sh.scr
./sh.scr: 12: Syntax error: redirection unexpected
So can you post the complete script, including the sha-bang?
 
Old 05-20-2011, 06:25 AM   #15
Basher52
Member
 
Registered: Mar 2004
Location: .SE
Distribution: Arch
Posts: 401

Original Poster
Rep: Reputation: 22
This script doesn't have a sha-bang since it's just a subscript called from another one that's inside the 'initrd' when it boots up.

Maybe I can try to use that #!/bin/bash in THIS script and see what happens.

I tried that and now no error other then that the subnet files was in the wrong place
Gonna change that and test again.

btw, what's the difference between bash and csh?
http://www.unix.com/shell-programmin...n-bash-sh.html

thx for the help so far, hadn't much knowledge about the difference between cs, bash, csh and tsch

Last edited by Basher52; 05-20-2011 at 07:21 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] cannot compare two directories, and print files that have same name and content apanimesh061 Fedora 3 09-17-2010 10:17 AM
full content of /var/log directory was deleted cccc Debian 1 04-13-2010 04:04 PM
how to compare to dir's with some equal content re007 Linux - Newbie 2 04-30-2007 02:49 PM
questionable content in /var/log/messages sovietpower Linux - Security 1 05-28-2005 01:08 PM
Is there any *FILE CONTENT COMPARE* tool in Linux? yuzuohong Linux - General 3 07-14-2002 06:11 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration