LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 10-06-2019, 10:00 AM   #1
end
Member
 
Registered: Aug 2016
Posts: 236

Rep: Reputation: Disabled
why this not working


i whant when percent of battery reach 20% break but he do notthing when reach 20

Code:
batt=`cat /sys/class/power_supply/BAT0/capacity`

while [ $batt -le "100" ];
do
     if [ $batt -eq "20" ]; then
          break
     fi
done
 
Old 10-06-2019, 10:16 AM   #2
orbea
Senior Member
 
Registered: Feb 2015
Distribution: Slackware64-current
Posts: 1,771

Rep: Reputation: Disabled
The $batt variable is only set once and doesn't ever get updated, you want it inside of the while loop's conditional.

Also quoting the numbers doesn't help you, you want the quotes around $batt instead to prevent any potential word-splitting which might not be a problem in this case.
 
2 members found this post helpful.
Old 10-06-2019, 10:32 AM   #3
end
Member
 
Registered: Aug 2016
Posts: 236

Original Poster
Rep: Reputation: Disabled
hi

now is working. i put
Code:


while [ `cat /sys/class/power_supply/BAT0/capacity` -le "100" ];
do
     if [ `cat /sys/class/power_supply/BAT0/capacity` -eq "20" ]; then
          break
     fi
done
but im confused. if i put $batt variable to command `cat /sys/class/power_supply/BAT0/capacity` and then i call that variable while [$batt -le 100] isnt while always read $batt
 
Old 10-06-2019, 11:36 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,070

Rep: Reputation: 4132Reputation: 4132Reputation: 4132Reputation: 4132Reputation: 4132Reputation: 4132Reputation: 4132Reputation: 4132Reputation: 4132Reputation: 4132Reputation: 4132
yes, it will read the value stored in variable batt. But you did not change that value any more.
It was set in the first line and [probably] you need to set it in that while loop again.

You did not put the command into the variable, but the result of that command, which is just a single value.

If you wish to store a command in a variable you need to do it differently:
Code:
batt="cat /sys/class/power_supply/BAT0/capacity" # store the command itself

while [ $($batt) -le "100" ];        # execute the command and evaluate the result
do
     if [ $($batt) -eq "20" ]; then  # and again
          break
     fi
done
 
2 members found this post helpful.
Old 10-06-2019, 11:43 AM   #5
end
Member
 
Registered: Aug 2016
Posts: 236

Original Poster
Rep: Reputation: Disabled
thank you very mouch. Mouch cleary now.
 
Old 10-06-2019, 01:07 PM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,860

Rep: Reputation: 1344Reputation: 1344Reputation: 1344Reputation: 1344Reputation: 1344Reputation: 1344Reputation: 1344Reputation: 1344Reputation: 1344Reputation: 1344
If you don't add a `sleep` comand into it, your scripts itself will consume the energy from the battery.
Also it would be safer to use `-le 20` instead if `-eq 20`.

Last edited by NevemTeve; 10-06-2019 at 01:13 PM.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
LXer: Why not the best? Why not Linux Mint? LXer Syndicated Linux News 0 04-04-2018 11:23 PM
SSH not working with PuTTy. Why not? How to debug it? mark4asp Linux - Security 7 11-14-2017 07:27 PM
why why why? mrlucio79 Debian 12 02-13-2006 03:52 PM
Cheops, Mandrake, and wondering why why why? DuMBDuMBSTooPiD Linux - Newbie 3 08-27-2004 08:56 AM

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

All times are GMT -5. The time now is 11:01 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration