Score:2

How to use a bash command as a custom shortcut?

cn flag

I have a command to turn on/off a touchpad (that works in terminal):

param_name=TouchpadOff; touchpadoff_value=$(synclient -l | awk -v param_name=$param_name '$1==param_name {print $(NF)}'); (( tmp_value =  $touchpadoff_value - 1 )); synclient TouchpadOff=${tmp_value#-}

and I'd like to set it as a custom shortcut.

I tried to use sh -c:


sh -c "param_name=TouchpadOff; touchpadoff_value=$(synclient -l | awk -v param_name=$param_name '$1==param_name {print $(NF)}'); (( tmp_value =  $touchpadoff_value - 1 )); synclient TouchpadOff=${tmp_value#-}"

but it still didn't work properly - it was able to only turn on the touchpad.

I was also trying to add quoting marks for variables ("$param_name") but it changed nothing.

Where am I mistaken?
I'd like not to create any script containing the command.

waltinator avatar
it flag
`sh` is a link to `/bin/dash`, a simpler shell which doesn't understand the `$(...)` construction. Use `bash -c ...`.
in flag
That is the point! You should have delivered this as a solution!
hr flag
`dash` understands `$(...)` just fine - it's [part of the POSIX specification](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_03). It's the bare arithmetic expression `((...))` that's the problem.
maciejwww avatar
cn flag
Ye, `bash -c` hasn't worked as well.
Score:2
cn flag

I tried to debug the command, so I created a script containing it, made it executable (chmod +x script.sh) and run debugging mode: sh -x script.sh.

I got:

+ param_name=TouchpadOff
+ synclient -l
+ awk -v param_name=TouchpadOff $1==param_name {print $(NF)}
+ touchpadoff_value=1
+ tmp_value = 1 - 1
mmm: 1: tmp_value: not found
+ synclient TouchpadOff=

and noticed the mathematical expression is a problem here.

So, I replaced it ((( tmp_value = $touchpadoff_value - 1 ))) with let tmp_value=$touchpadoff_value-1.

Unfortunately, let doesn't work with sh and I used bash, and here is my working solution I pasted as the custom shortcut's command:

bash -c "param=TouchpadOff; touchpadoff_value=$(synclient -l | awk -v param_name=$param '$1==param_name {print $(NF)}'); let tmp_value=$touchpadoff_value-1; synclient TouchpadOff=${tmp_value#-}"
in flag
I tried the original commands (put one command per line to the file `q.sh`) in order to try out `bash -x ./q.sh`. The debug output showed me that `TouchpadOff` toggles its value between 0 and 1 and the touchpad toggles between enabled and disabled. Replacing the original line was not necessary. - Your solution was helpful for me because I could learn from it how to debug a script. The post taught me doing arithmetic enclosing expressions in in double parentheses like `(( <arith. expr.> ))`.
hr flag
You don't really need shell arithmetic here at all, just let awk do it for you i.e. print `$NF - 1` instead of `$NF`
Score:0
na flag

You could try to make an alias. Do: man alias to get helping the exact way. They are probably in: /home/[user]/.bashrc There is probably a complex example. An easy one is: alias ll='ls -alF'

pierrely avatar
cn flag
and in the shortcut as bash -i -c "ll"
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.