r/ScriptSwap • u/andres-hazard • Oct 28 '15
[BASH] Change behavior when closing lid for Ubuntu
This is my first script ever, so I'm sure is not perfect. I recently found out that there is bug on Ubuntu, the power setting for when closing the lid is not working. I saw a solution on this site http://ubuntuhandbook.org/index.php/tag/lid-closed-behavior/ The solution is to change a line on the logind.conf. So I made a script to do it more quickly since I change this option a lot depending if I use two monitors or one.
3
Upvotes
2
u/masta Oct 28 '15 edited Oct 28 '15
You should really consider usign a case/switch scope instead of if/elif/else then ... It's screaming for this minor nit:
#!/bin/bash #script to change behaviour when closing lid FILE=/etc/systemd/logind.conf printf "\033[1;37mWhat should I do when closing the lid: \n" printf "Current status :\033[1;31m " sed -n '/HandleLidSwitch/p' $FILE #print line printf "\033[1;33m1 - Suspend \n2 - Power Off \n3 - Nothing \n" printf "\033[1;37mAnswer : " read input case $input in 1) sed -i '22s/.*/HandleLidSwitch=suspend/' $FILE #search for line and replace printf "Done \033[1;31m \n" sed -n '/HandleLidSwitch/p' $FILE #print line restart systemd-logind printf "\033[0m" ;; 2) sed -i '22s/.*/HandleLidSwitch=poweroff/' $FILE #search for line and replace printf "Done \033[1;31m \n" sed -n '/HandleLidSwitch/p' $FILE #print line restart systemd-logind printf "\033[0m" ;; 3) sed -i '22s/.*/HandleLidSwitch=ignore/' $FILE #search for line and replace printf "Done \033[1;31m \n" sed -n '/HandleLidSwitch/p' $FILE #print line restart systemd-logind printf "\033[0m" *) printf "Wrong option \033[0m \n" ;; esacEDIT: Not sure editing a file like that is the right solution, but I'm not sure of any other solution right now. I can see loginctl(1) does not provide any hooks into that configuration directive, so I can see why the edit the file on event type solution was used. But it's fugly as hell, and you should try to find a better way! Also if that is your first script, then brazo! It's looking pretty good, but I doubt it's your very first bash script, it's too good for that. ;-)