I am using below shell script for getting certail data
test.sh
adb shell dumpsys battery | awk '/Battery current/{print}'
I get below output in terminal
Battery current : -286
How to check if Battery current value is > -1000 then execute an command "Command1" else execute "Command 2"
Please help
CodePudding user response:
Just use awk
to extract the field you want:
adb shell dumpsys battery | awk -F: '/Battery current/{print $2}'
If you want to put that value in a variable:
battery_current="$(adb shell dumpsys battery | awk -F: '/Battery current/{print $2}')"
And now you can use that in an if
statement:
if [ "$battery_current" -gt -1000 ]; then
...do something...
else
...do something else...
fi
CodePudding user response:
Using awk
, you can use the system
function which executes cmd and returns its exit status
$ adb shell dumpsys battery | awk '/Battery current/ {current=$NF}{if (current > -1000) system("command.."); else system("comand..")}'
CodePudding user response:
The generic template would be like this :
jot 5 | {m,g}awk '{ system( ( $_)%3 ? "date" : "gdate \47%s.%6N\47") }'
Fri Jun 17 09:19:18 EDT 2022
Fri Jun 17 09:19:18 EDT 2022
1655471958.225290
Fri Jun 17 09:19:18 EDT 2022
Fri Jun 17 09:19:18 EDT 2022
Your specific case would be like :
adb shell dumpsys battery |
{m,g}awk '!_<NF { system(-1E3< $NF ? _cmd1_ :_cmd_2_) }' FS='^ *Battery current[: ] '