Posted on 01-16-2012 10:37 AM
I am running the following command on the mac and I want to create a policy that would only run if the computer is plugged in
pmset -g ps | awk 'NR>1{exit};1'
This gives me the current power status and only the 1st line (don't want the battery info and percent)
Example I get this
Currently drawing from 'AC Power'
or
Currently drawing from 'Battery Power'
What I want is just the words AC Power or Battery Power
and then do an if statement
if Battery Power exit
if AC Power run update!
Can you guys give me a hand, my grep and awk skills suck,
Thanks guys
Solved! Go to Solution.
Posted on 01-16-2012 11:08 AM
This should give you what you are looking for. As always, TEST, TEST, TEST.
pmset -g ps | awk 'NR>1{exit};1' | awk '{print $4,$5}' | sed "s/'//g"
Steve
Posted on 01-17-2012 04:52 AM
This can be really short. You only need to know if it exists, there's no need to analyse the actual sentence. Try this, it basically says if $isAC isn't empty then run something.
isAC=pmset -g ps | grep "AC Power"
if [[ $isAC ]]
then
echo "AC DO THIS"
fi
Alternatively, you could use =~ as a 'does it contain this':
isAC=pmset -g ps | grep "^Currently drawing"
if [[ "$isAC" =~ "AC" ]]
then
echo "AC DO THIS"
fi
Posted on 01-16-2012 11:08 AM
This should give you what you are looking for. As always, TEST, TEST, TEST.
pmset -g ps | awk 'NR>1{exit};1' | awk '{print $4,$5}' | sed "s/'//g"
Steve
Posted on 01-16-2012 02:04 PM
Thanks works perfect,
GOING TO TEST TEST TEST! Thanks :-)
Also going to start a hacking your statement apart...
Posted on 01-17-2012 04:52 AM
This can be really short. You only need to know if it exists, there's no need to analyse the actual sentence. Try this, it basically says if $isAC isn't empty then run something.
isAC=pmset -g ps | grep "AC Power"
if [[ $isAC ]]
then
echo "AC DO THIS"
fi
Alternatively, you could use =~ as a 'does it contain this':
isAC=pmset -g ps | grep "^Currently drawing"
if [[ "$isAC" =~ "AC" ]]
then
echo "AC DO THIS"
fi
Posted on 01-17-2012 05:10 AM
Awesome thanks...
Posted on 01-17-2012 05:48 AM
Are you using a manual trigger policy?
Posted on 01-17-2012 06:49 AM
Yeah the macbook airs have this thunderbolt update that needs to go out and they need to be plugged in for that, so basically the logic is
Open Self Service
Login
Click Update Me / checks to see if plugged in / opens window telling students to leave computer plugged / computer does the update plus we have this iMovie issue where they can't see the iMovie window that will be fixed also / then restarts
if not plugged in, policy fails with a window telling the student to run this again when computer is plugged in.
Makes sense ??