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 09-30-2016, 05:17 PM   #1
xv_biker1
LQ Newbie
 
Registered: Sep 2012
Location: yorkshire UK
Distribution: Ubuntu server-desktop
Posts: 14

Rep: Reputation: Disabled
Scripting if statements and variables


Hi all hope someone can make sense of this!
how can i get send =1 out of the if section to another if at the bottom to impliment the action?
Send=0
if [ $temp -ge $Alert ]
then
echo "high temp on Core $Core ($temp), on server $(hostname) at $(date)" >>/tmp/tempalert
Send=1
echo working
echo "send in if $Send" # returns 1 as i want
fi
echo $Send #returns 0 - should be 1help!!!!

-----
I want the last echo to return 1 if the if statement has run but even tho the send in if returns 1 the last one STILL returns 0 as if the Send=1 statement has had no effect on the rest of the script?????
Ive tried export send but this has had effect either
help!!!!!
thanks in advance!

google skills failure!

dave
 
Old 09-30-2016, 06:15 PM   #2
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
n/m.
Vacation tomorrow!

Last edited by Habitual; 09-30-2016 at 06:17 PM.
 
Old 09-30-2016, 06:41 PM   #3
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,337

Rep: Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548
You are missing an else clause.

Send=0
if [ $temp -ge $Alert ]
then
echo "high temp on Core $Core ($temp), on server $(hostname) at $(date)" >>/tmp/tempalert
Send=1
echo working
echo "send in if $Send" # returns 1 as i want
else
echo $Send #returns 0 - should be 1help!!!!
fi
 
Old 10-01-2016, 02:13 AM   #4
xv_biker1
LQ Newbie
 
Registered: Sep 2012
Location: yorkshire UK
Distribution: Ubuntu server-desktop
Posts: 14

Original Poster
Rep: Reputation: Disabled
thanks for trying but the else doesnt really have anything to do...
Im fairly new to scripting in linux and havent done any true programming for years - never got on with java even back then.
Is there a rule that stops variable interfering with other parts of the script?

ill expand the code a bit might make more sense for you

Send=0
if [ $temp -ge $Alert ]
then
echo "high temp on Core $Core ($temp), on server $(hostname) at $(date)" >>/tmp/tempalert
Send=1
echo working
echo "send in if $Send" # returns 1 as i want
else
echo $Send #returns 0 - should be 1help!!!!
fi

if [$send -eq 1]; then
cat /tmp/tempalert > mutt -s "overheat" me@me.com
fi
 
Old 10-01-2016, 03:19 AM   #5
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
Please use [code][/code] tags around code / data.

Are you 100% sure all your send variables are typed correctly? I ask as your example in post #4 has a mix of upper and lower case 's' at the start which is an issue in bash.

I am also unable to replicate your issue so am wondering if we are seeing all the code? May I suggest you place the following at the start of your script:
Code:
set -xv
See if that shows you where your variables are being set / changed
 
Old 10-01-2016, 08:27 AM   #6
xv_biker1
LQ Newbie
 
Registered: Sep 2012
Location: yorkshire UK
Distribution: Ubuntu server-desktop
Posts: 14

Original Poster
Rep: Reputation: Disabled
Sorry bit of a newbie here
the only thing i didnt really show was that its all in a while loop
i did read something about all uppercase variables been system /env so i moved away from them. as far as i can see theyre all the same
the set -xv shows
[CODE]
Quote:
++ echo
sensors | grep 'Core' | awk '{ print $2 " " $3 }' |tr -d '+°C' | while read output;
do
temp=$(echo $output | awk '{ print $2}' | cut -d' ' -f1 | sed -r 's_\.[0-9]__' )
#echo $temp
Core=$(echo $output | awk '{ print $1 }' )
if [ $temp -ge $Alert ]; then
echo "high temp on Core $Core ($temp), on server $(hostname) at $(date)" >>/tmp/tempalert
Send=1
tester=1
echo working
echo "send in if $Send"
#mutt -s "Alert: Free space low, $usep % used on $partition" $ADMIN
else
Send=0
fi
done
++ sensors
++ grep Core
++ awk '{ print $2 " " $3 }'
++ tr -d +°C
++ read output
echo $output | awk '{ print $2}' | cut -d' ' -f1 | sed -r 's_\.[0-9]__'
+++ echo 0: 54.0
+++ cut '-d ' -f1
+++ awk '{ print $2}'
+++ sed -r 's_\.[0-9]__'
++ temp=54
echo $output | awk '{ print $1 }'
+++ echo 0: 54.0
+++ awk '{ print $1 }'
++ Core=0:
++ '[' 54 -ge 50 ']'
hostname
+++ hostname
date
+++ date
++ echo 'high temp on Core 0: (54), on server Premier.local at Sat 1 Oct 14:14:11 BST 2016'
++ Send=1
++ tester=1
++ echo working
working
++ echo 'send in if 1'
send in if 1
++ read output
echo $output | awk '{ print $2}' | cut -d' ' -f1 | sed -r 's_\.[0-9]__'
+++ echo 1: 52.0
+++ awk '{ print $2}'
+++ cut '-d ' -f1
+++ sed -r 's_\.[0-9]__'
++ temp=52
echo $output | awk '{ print $1 }'
+++ echo 1: 52.0
+++ awk '{ print $1 }'
++ Core=1:
++ '[' 52 -ge 50 ']'
hostname
+++ hostname
date
+++ date
++ echo 'high temp on Core 1: (52), on server Premier.local at Sat 1 Oct 14:14:11 BST 2016'
++ Send=1
++ tester=1
++ echo working
working
++ echo 'send in if 1'
send in if 1
++ read output
echo "send at end $Send"
++ echo 'send at end 0'
send at end 0
echo "tester $tester"
++ echo 'tester '
tester
if [ $Send -eq 1 ]; then
cat /tmp/tempalert | mutt -s "tempalert" me@me.com
fi
++ '[' 0 -eq 1 ']'
[\CODE]


[CODE]
ADMIN="me@me.com"
# set alert-level 56 standard
Alert=50
Send=0
export Send
echo "To: me@me.com" >/tmp/tempalert
echo "From: me@me.com" >>/tmp/tempalert
echo "Subject: tempalert - Premier">>/tmp/tempalert
echo >>/tmp/tempalert
sensors | grep 'Core' | awk '{ print $2 " " $3 }' |tr -d '+°C' | while read output;
do
temp=$(echo $output | awk '{ print $2}' | cut -d' ' -f1 | sed -r 's_\.[0-9]__' )
Core=$(echo $output | awk '{ print $1 }' )
if [ $temp -ge $Alert ]; then
echo "high temp on Core $Core ($temp), on server $(hostname) at $(date)" >>/tmp/tempalert
Send=1
tester=1
echo working
echo "send in if $Send"
else
Send=0
fi
done
echo "send at end $Send"
echo "tester $tester"
if [ $Send -eq 1 ]; then
cat /tmp/tempalert | mutt -s "tempalert" me@me.com
fi
[\CODE]

Last edited by xv_biker1; 10-01-2016 at 08:29 AM.
 
Old 10-01-2016, 09:20 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Actually, it's [code] and [/code]

Your code contains a redirection into a while-loop which runs in sub-shell, so the variable-assignment won't affect the parent-shell.

In bash, it could be something like this:

Code:
while read output; do
...
done < <(sensors ...)
or

Code:
sensors ... >tmpfile

while read output; do
...
done <tmpfile

Last edited by NevemTeve; 10-01-2016 at 09:43 AM.
 
1 members found this post helpful.
Old 10-01-2016, 10:55 AM   #8
xv_biker1
LQ Newbie
 
Registered: Sep 2012
Location: yorkshire UK
Distribution: Ubuntu server-desktop
Posts: 14

Original Poster
Rep: Reputation: Disabled
thanks! thats what i thought was going on.
id pinned down that the variable for send wasnt getting passed back but didnt know how to make it to. Didnt think it was the while loop just thought it was within the IF clause. Very sorry to everyone else that i didnt show the code for that.

so if anyones interested the final code is

Code:
ADMIN="me@me.com"
# set alert-level 56  standard
Alert=60
Send=0

echo "To: me@me.com" >/tmp/tempalert
echo "From: me@me.com" >>/tmp/tempalert
echo "Subject: tempalert - Premier">>/tmp/tempalert
echo >>/tmp/tempalert
#sensors | grep  'Core' | awk '{ print $2 " " $3 }' |tr -d '+°C' | while read output;

while read output; do
  temp=$(echo $output | awk '{ print $2}' | cut -d' ' -f1 | sed -r 's_\.[0-9]__' )
  Core=$(echo $output | awk '{ print $1 }' )
  if [ $temp -ge $Alert ]; then
    echo "high temp on Core $Core ($temp), on server $(hostname) at $(date)" >>/tmp/tempalert
    Send=1
  else
    Send=0
  fi
done < <(sensors | grep  'Core' | awk '{ print $2 " " $3 }' |tr -d '+°C')

if [ $Send -eq 1 ]; then
  cat /tmp/tempalert | mutt  -s "tempalert" me@me.com
fi
rm /tmp/tempalert
Thanks very much guys! <happy>
 
Old 10-01-2016, 11:21 AM   #9
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
Good to see you got there in the end

Here are some things you can look into:

1. Although it can be considered a preference, bash generally keeps all variables in lower case except for environment variables which are all upper case

2. Look up here documents ... this will reduce the need for calling echo so many times

3. awk is capable of performing all your grep/sed/tr/cut tasks so no need to use multiple commands piped together

4. Use (()) when performing arithmetic tests

5. Prefer [[]] over []

Here is a site you should slowly work through if you are keen on improving your bash skills
 
1 members found this post helpful.
Old 10-01-2016, 04:01 PM   #10
xv_biker1
LQ Newbie
 
Registered: Sep 2012
Location: yorkshire UK
Distribution: Ubuntu server-desktop
Posts: 14

Original Poster
Rep: Reputation: Disabled
Also just noticed that when trying to set this to run as a cron job ive had to add /bin/bash /tempalert as the command because of a syntax error.
My own fault for missing off the
Code:
#!/bin/bash
line at the beginning - never seen that before

Last edited by xv_biker1; 10-01-2016 at 04:03 PM. Reason: still having problems with [code] and [\code] not recognsing unless i copy / paste?
 
Old 10-01-2016, 04:07 PM   #11
xv_biker1
LQ Newbie
 
Registered: Sep 2012
Location: yorkshire UK
Distribution: Ubuntu server-desktop
Posts: 14

Original Poster
Rep: Reputation: Disabled
Thanks Grail!
i usually know what i need to do to get there but usually fall out over some syntax or quirk im not expecting.
 
  


Reply

Tags
bash, linux, script, variables



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
Help with scripting, aliases, and test statements. ThatCyberGuy Linux - Newbie 4 11-14-2015 02:58 PM
BASH scripting Condition Statements Xyaib Linux - General 1 01-23-2013 02:18 PM
bash scripting problem with nested if statements in while loop error xskycamefalling Programming 4 05-11-2009 03:14 PM
PHP if statements and variables antken Programming 4 09-24-2003 12:45 PM
Shel scripting: variables pointing to variables and case Dark_Helmet Programming 5 06-08-2003 11:07 AM

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

All times are GMT -5. The time now is 06:17 PM.

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