May 01, 2017

bash if shift-key pressed

shift-key

I had to write a custom bash launch script for a local machine, but wanted it to do one thing if the shift-key was pressed and another thing if it was not.

After searching, i found that bash is NOT capable of such a thing. However, there was a short and simpe C implementation found here: https://forums.gentoo.org/viewtopic-p-2455159.html#2455159

So after compiling the code (gcc shift_state.c -o shift_state ; chmod +x shift_state) and explicitly running it under sudo (required to access /dev/console), i found it did exactly what was needed.

So the only problem remaining was i didn’t want to run my bash script with sudo. To circumvent such, i ran sudo visudo and added the line myusername ALL=(ALL) NOPASSWD: /home/myusername/scripts/shift_state which would allow me to run sudo ~/scripts/shift_state without entering my password.

Subsequently, it was easy to implement a bash script as needed.

Code:

But wait, there's more!

Such could also be used to customize your XFCE Panel-based launch-bar. (Or any launch-bar for that matter.) For instance, In the past, I've created a panel item for Sublimetext. This is a "Launcher" item with two sublimetext commands, one launches and another launches with the -n parameter for a new window. However, it looks ugly and a bit cumbersome to launch:

With the shift_state method, I have replaced the Launcher commands with a single command: bash -c "if ! (( $(sudo ~/scripts/shift_state) )) ; then /opt/sublime_text/sublime_text %F ; else /opt/sublime_text/sublime_text -n %F ; fi". Now it looks better without a secondary command-arrow, and when I shift-click to launch, it provides me the same function in a quicker workflow way.

~~~

As Always, Good Luck! You can thank me with bitcoin.   

Written with StackEdit.

No comments:

Post a Comment

Comments, Suggestions or "Thank you's" Invited! If you have used this info in any way, please comment below and link/link-back to your project (if applicable). Please Share.
I accept Bitcoin tips of ANY amount to: 1GS3XWJCTWU7fnM4vfzerrVAxmnMFnhysL
I accept Litecoin tips of ANY amount to: LTBvVxRdv2Lz9T41UzqNrAVVNw4wz3kKYk