variable joining


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting variable joining
# 1  
Old 03-09-2010
variable joining

I really need help on this problem.

The story:

My VAR1 and VAR2 works fine and able to get the value. I want to do as "if VAR1 or VAR2 is bigger than max_loadavg then the code will run...then if VAR1 or VAR2 is lesser than min_loadavg then the other code will run...

The problem:
The script run but seems works at max_loadavg and not the min_loadavg line. Please help

p/s: thanks pludi to add the taqs


Part of the Code:

Code:
max_loadavg=50.0
min_loadavg=10.0

VAR1=$(cat /proc/loadavg | awk '{print $1}')
VAR2=$(ssh web2 -C cat /proc/loadavg | awk '{print $1}')

if [ $(echo "$VAR1 >= $max_loadavg && $VAR2 >= $max_loadavg"|bc) -eq 1 ]

execute the code

fi

if 

if [ $(echo "$VAR1 <= $min_loadavg && $VAR2 <= $min_loadavg"|bc) -eq 1 ]

execute the code

fi


Last edited by hezry79; 03-09-2010 at 08:52 AM.. Reason: code tags, please...
# 2  
Old 03-09-2010
Code:
if [ $VAR1 -gt $max_loadavg ] || [ $VAR2 -gt $max_loadavg ] ; then
 echo code will run
elif [ $VAR1 -lt $min_loadavg ] || [ $VAR2 -lt $min_loadavg ] ; then
 echo code will run
else
 echo code wont run
fi


I am not clear with the conditions you provided.
you wrote "or" in explanation and used "&&" in code. max and min name are also seems to be reverse..

by the way, you can handle that with you own.
use && instead || for "AND".
le and ge for adding ( less then or equal to or greater then or equal to )
# 3  
Old 03-09-2010
If anyone of the values of variable VAR1 or VAR2 is greater than max value or less than min value,then you need to use || operator instead of && operator in your script.If you want both the values should be greater than max value or less than min value,then only you need to use && operator.

Below is the sample code for you to better understanding.

Code:
val=5

max=10

min=50

if [[ $val -gt $max || $val -gt $max ]];then
echo "Maximum Matched and its Value:$val"
elif [[ $val -lt $min || $val -lt $min ]];then
echo "Minimum Matched and its Value:$val"
fi

# 4  
Old 03-09-2010
Below is my full previous code

PHP Code:
#!/bin/bash

# Load average ALERT value.

# Maximum load injection trigger
max_loadavg=15.0

# Minimum load injection trigger
min_loadavg=10.0

loadavg
=$(cat /proc/loadavg awk '{print $1}')

# Grab Web1 current LOAD
VAR1=$(cat /proc/loadavg awk '{print $1}')

# Grab Web2 current LOAD
VAR2=$(ssh web2 -C cat /proc/loadavg awk '{print $1}')


# To check current LOAD for both Database value and trigger the injection
if [ $(echo "$VAR1 >= $max_loadavg && $VAR2 >= $max_loadavg"|bc) -eq 1 ]

then

EXECUTE THE CODE

fi

# To check current LOAD for both Database value and trigger the injection
if [ $(echo "$VAR1 <= $min_loadavg && $VAR2 <= $min_loadavg"|bc) -eq 1 ]



then

EXECUTE THE CODE

fi

exit 


---------- Post updated at 08:26 AM ---------- Previous update was at 08:19 AM ----------

I modify my code as below but the error come out:

recoverwebload: line 22: [[2.28: command not found
recoverwebload: line 22: [0.20: command not found
recoverwebload: line 36: [[2.28: command not found
recoverwebload: line 36: 0.20: command not found


PHP Code:
#!/bin/bash

# Load average ALERT value.

# Maximum load injection trigger
max_loadavg=15.0

# Minimum load injection trigger
min_loadavg=10.0

loadavg
=$(cat /proc/loadavg awk '{print $1}')

# Grab Web1 current LOAD
VAR1=$(cat /proc/loadavg awk '{print $1}')

# Grab Web2 current LOAD
VAR2=$(ssh web -C cat /proc/loadavg awk '{print $1}')


# To check current LOAD for both Database value and trigger the injection

if [[$VAR1 -gt $max_loadavg || [$VAR2 -gt $max_loadavg]]

then 

EXECUTE THE CODE

fi

# To check current LOAD for both Database value and trigger the injection

if [[$VAR1 -lt $min_loadavg || $VAR2 -lt $min_loadavg]]

then
EXECUTE THE CODE

fi

exit 
# 5  
Old 03-09-2010
I don't know why you're echoing the value and comparing with max value and passing it to bc.You simply take my example script and do your script easily.

You change your condition as follows.

Code:
if  [[ $VAR1 -gt $max_loadavg || $VAR2 -gt $max_loadavg]];then
execute the code
elif [[ $VAR1 -lt $min_loadavg || $VAR2 -lt $min_loadavg]];then
execute another code
fi

You change the >= operator to -gt which is used for numeric comparison and <= into -lt.
# 6  
Old 03-09-2010
i already ammend it but below is the error:

Code:
recoverwebload: line 19: syntax error in conditional expression: unexpected token `;'
recoverwebload: line 19: syntax error near `;t'
recoverwebload: line 19: `if  [[ $VAR1 -gt $max_loadavg || $VAR2 -gt $max_loadavg]];then'


Last edited by Scott; 03-09-2010 at 04:02 PM.. Reason: Added code tags
# 7  
Old 03-09-2010
which shell do you have?
if [[ $VAR1 -gt $max_loadavg || $VAR2 -gt $max_loadavg]];then

put space on the both side of "[[" and "]]".

try:
Code:
[ ... ] || [ ... ]

#remember space.

also, use integer values for max_loadavg and min_loadavg, or use the same bc method as you were doing previously.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Please help me in joining two files

I have two files with the below contents : sampleoutput3.txt 20150202;hostname1 20150223;hostname2 20150716;hostname3 sampleoutput1.txt hostname;packages_out_of_date;errata_out_of_date; hostname1;11;0; hostnamea;12;0; hostnameb;11;0; hostnamec;95;38; hostnamed;440;358;... (2 Replies)
Discussion started by: rahul2662
2 Replies

2. Shell Programming and Scripting

Joining lines in different way

Hi all, I'm excited to the part of unix.com forum, and noob to it. I have an query, where I have an file and it contains data like this use thread when posting do no I was expecting the result as use thread thread when when posting posting do do no use thread when thread when... (6 Replies)
Discussion started by: Jose Nirmal
6 Replies

3. Shell Programming and Scripting

Joining

I do have many files of the following format. File_1.txt 1 24 2 25 3 27 4 28 5 29 6 1 File_2.txt 2 5 3 8 4 9 5 10 File_3.txt (3 Replies)
Discussion started by: Lucky Ali
3 Replies

4. Shell Programming and Scripting

Joining two files into one

Hi experts, I'm quite newbie here!! I have two seperate files. Contents of file like below File 1: 6213019212001 8063737 File:2 15703784 I want to join these two files into one where content will be File 3: 6213019212001 8063737 15703784 Regards, Ray Seilden (1 Reply)
Discussion started by: RayanS
1 Replies

5. Shell Programming and Scripting

Need help joining lines

Hi All, I need the command to join 2 lines into one. I found lots of threads but none give me the sollution. Probably because unix scripting is one of my best features ;) I got a logfile where line 2 needs to be joined with line 1, lines 4 needs to be joined with line 3 etc If you need... (16 Replies)
Discussion started by: rene21976
16 Replies

6. Shell Programming and Scripting

joining two lines

Hi , I want to join two lines in a file, where the second line contain query string. if it doesn't contain that string i don't want to join e.g. Input file is as following: name fame game none none none name fame game cat eat mice I need output file as name fame game none none... (2 Replies)
Discussion started by: ashrafonics
2 Replies

7. Shell Programming and Scripting

Joining two arrays and then creating a variable

Hello all... I'd like to create a variable from an array element from two arrays. In my search for answers I found this code for bash that joins two arrays and then started to work with it. I had got to work once and then foolishly without saving the code, I started to edit it for ksh and... (4 Replies)
Discussion started by: carlos25
4 Replies

8. UNIX for Dummies Questions & Answers

joining variable to the end of a file name

hi all i have a directory which contain file 20060101-66666-09-08-0.tif 20060101-77777-11-12-0.tif 20051231-54221-66-55.tif 20051231-54221-66-44.tif as you can see the name of the two last files is shorter then the first ones i want to take all the files with the shorter name and to add to... (7 Replies)
Discussion started by: naamas03
7 Replies

9. Shell Programming and Scripting

Help with joining two files

Greetings, all. I've got a project that requires I join two data files together, then do some processing and output. Everything must be done in a shell script, using standard unix tools. The files look like the following: File_1 Layout: Acct#,Subacct#,Descrip Sample: ... (3 Replies)
Discussion started by: rjlohman
3 Replies

10. UNIX for Dummies Questions & Answers

joining files

Hi, Could anyone help me ? I'm trying to join two files, but no common field are on them. So I think on generate \000\ sequence to add for each line on both files, so then will be able to join these files. Any idea? Thanks in advance, (2 Replies)
Discussion started by: Manu
2 Replies
Login or Register to Ask a Question