LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-18-2010, 10:25 PM   #1
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Rep: Reputation: 16
Question setting the final $result


Hi All,

I have a variable (call it $valAS) that gets calculated . Also, i have a files Bassu3. if $valAS= 45368 (2nd part of 2nd line in Bassu3) after calculation, then $result=45368, but if $valAS != 45368 then $result= $valAS

Bassu3:
Quote:
name.srv
name 45368 (value always changing)
name 99999
name 88888


how can i do this in Bash?

Thank you for your help.
 
Old 05-18-2010, 10:39 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I am not sure what your question is? Can you do an if, of course. Can you set a value for a variable, of course.

Would you please be a little clearer as to what your need is here?
 
Old 05-18-2010, 11:19 PM   #3
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Original Poster
Rep: Reputation: 16
sorry for not being clear.

let's say always result=$varAS. But a new condition must be added to this. the condition is if $valAS= 45368 (2nd part of 2nd line in Bassu3) then result=45368, but if $valAS != 45368 then result= $valAS. so i need to know if $valAS is equal to 2nd part of 2nd line in Bassu3,or not.

So if the 2nd part in 2nd line in file Bassu3 is not equal to valAS then result= 2nd part in 2nd line in file Bassu3 Not $varAS

Hope that is clear. thank you for your help
 
Old 05-19-2010, 12:13 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Hmmm ... seems more of the same to me. I do understand basically you want an if statement, what I am asking is, what are you having problems with?

I think it was explained before, but if not I will again. Not only do you need to ask your question about what you want to do, but you also need to supply what you have tried
and where you are stuck so that we can help. Otherwise we do the work and you don't learn what you need to. Then you come back again because you have not furthered
your knowledge.

Let me help a little:

1. You would like to write a script
2. You would like script to test the second value on the second line of a particular file
3. You would like to write an "if" statement that sets a variable based on the item found in step 2

If I am wrong, please correct me.
If I am right, use this pseudo code to write the script and let us know when you get stuck

Cheers
 
Old 05-19-2010, 09:25 AM   #5
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Original Poster
Rep: Reputation: 16
I appreciate your comments. please note i "do" the work never want to drop the load or run away from unknown. that's the main reason i have accepted to start & FINISH this project - even i have NO KNOWLEDGE about Bash- in addition to my other responsibilities ..... Anyway, i am sorry if i look like someone wants to take advantages of your kind help.

Here what i did

Quote:
2ndline=$(awk -F" " 'NR==2{print $2}' inputfile)
varas=$(awk -F" " 'NR==2{print $2}' inputfile)

if ($varas != $2ndline)
then
result=$2ndline
else
result=$varas
fi
but get i get error in my if statement.

Thank you again for your help.
 
Old 05-19-2010, 09:35 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
ok ... firstly, I didn't mean to sound like I was pointing a harsh finger. Just it is easier if we know where you are coming from

So let us look at your code:

1. The default field separator for awk is a space so your -F" " is not required.

2. Unless a freak accident were to occur that the file is updated between calls to awk, it would be impossible for either variable to not be equal. In saying that, I am not sure if you just wanted to test if your "if" works??

3. Have a look at these pages
- http://tldp.org/LDP/abs/html/nestedifthen.html
- http://tldp.org/LDP/abs/html/comparison-ops.html
and you will see why your test has failed

4. And this one is important when saying you have an error, show us the error There are many that are possbile and sometimes simply looking at your code will not provide what might have gone wrong.

Let me know how you go?
 
Old 05-19-2010, 10:13 AM   #7
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Original Poster
Rep: Reputation: 16
Thanks for your quick reply.


Quote:
it would be impossible for either variable to not be equal.
the inputfiles are different. i should have written inputfile1 & inputfile2

Thanks for the links. i will read them & find out my mistake.

The error i was getting is coming from my $varas. it says: COMMAND NOT Found

thanks again.
 
Old 05-19-2010, 10:29 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
The error i was getting is coming from my $varas. it says: COMMAND NOT Found
And that will make some sense when you look at the links.
 
Old 05-19-2010, 10:36 AM   #9
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Original Poster
Rep: Reputation: 16
Find out why i have the error. the value of $2ndline & $vara are in 192.168.0.1 format. somehow it does not like this format. i must find a way to ignore the dots. tried with cut it works but i have to cut each dot, so i have to cut 8 times which is so long to do that.

any ideas??
 
Old 05-19-2010, 10:58 AM   #10
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
Quote:
Originally Posted by NetRock View Post
Find out why i have the error. the value of $2ndline & $vara are in 192.168.0.1 format. somehow it does not like this format. i must find a way to ignore the dots. tried with cut it works but i have to cut each dot, so i have to cut 8 times which is so long to do that.

any ideas??
bash has no problem with having the likes of 192.168.0.1 as variable values. The problem is in the format of your if command, specifically the test. Clues here.
 
Old 05-19-2010, 11:11 AM   #11
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Original Poster
Rep: Reputation: 16
Thanks for your attention catkin. please note when i use "cut" my statement works find, but when i use if with my variable it does not. i have done a test at the command line with if and 192.168.0.1 got error. but when i use 19216801 i get the result.
please try & let me know the final result.

Thanks.
 
Old 05-19-2010, 11:19 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Hi Netrock

I don't think you looked closely enough at the links I provided:

Your if: (assuming you haven't changed the format)
Code:
if ($varas != $2ndline)
If's from the link I gave:
Code:
if [ "$a" -gt 0 ]
Without worrying about the variable names or comparison operator, there is a significant difference.
 
Old 05-19-2010, 12:31 PM   #13
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Original Poster
Rep: Reputation: 16
i do not know if i understand your point correctly about the link. but:
I have 3 variables:

1) $varas ## coming from inputfile2
2) $2ndline ## coming from inputfile1
3) $result ## if statement decides the value

(If $varas = $2ndline then my 3rd variable result is equal to $varas otherwise is equal to $2ndline)

But if generating error because the value of $varas is 192.168.25.36 if i change it to
1921682536 works find.

So, how does the link applies to my case?
 
Old 05-19-2010, 12:50 PM   #14
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Original Poster
Rep: Reputation: 16
just to add more info my if statement looks like this:

Quote:
if [ "192.168.15.23" != "172.56.98.21"]
and the dots creating error. because if i use with no dots it works fine. no errors

i tried double, single quote, \ .... nothing worked so far....
 
Old 05-19-2010, 01:01 PM   #15
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
Quote:
Originally Posted by NetRock View Post
i tried double, single quote, \ .... nothing worked so far....
Try a space between the final " and the ]
 
  


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
am setting radius+ldap and its giving me this result jerykwajaffa Linux - Networking 1 09-08-2008 09:17 AM
Final security checks setting up thttpd rickh Debian 1 07-01-2006 04:21 AM
smb_request: result -104, setting invalid ?? spudler Linux - Networking 0 02-07-2005 03:54 PM
smb_trans2_request: result=-104, setting invalid x2000koh Linux - Networking 0 07-08-2003 08:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:59 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