0

I have the following shell script, which works perfectly:

#!/bin/bash
set -exuo pipefail

event="/dev/input/$(cat /proc/bus/input/devices \
    | sed '1,/Name="SZH usb keyboard"/d' \
    | grep Handlers= \
    | head -n1 \
    | tr '=' ' ' \
    | tr ' ' '\n' \
    | grep -E '^event[0-9]+$' \
    | head -n1)"

echo "Enabling scrolllock for $event ..."
"$(dirname "$0")"/ledToggler.py "$event" scrolllock scrolllock 1 1

However, when I replace the /bin/bash with /bin/busybox ash, it stops working. I get an exit code of 141 and the following output:

+ cat /proc/bus/input/devices
+ sed '1,/Name="SZH usb keyboard"/d'
+ grep 'Handlers='
+ head -n1
+ tr ' ' '\n'
+ grep -E '^event[0-9]+$'
+ head -n1
+ tr '=' ' '
+ event=/dev/input/event4

Can you tell me what is the problem why it doesn't work with busybox ash?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
msrd0
  • 7,816
  • 9
  • 47
  • 82
  • 1
    Does this answer your question? [Why exit code 141 with grep -q?](https://stackoverflow.com/questions/19120263/why-exit-code-141-with-grep-q) – β.εηοιτ.βε May 01 '23 at 08:04
  • @β.εηοιτ.βε Not it doesn't. I'm not using `grep -q` at all – msrd0 May 01 '23 at 08:05
  • Did you read the accepted answer: _Another common place where you see this behaviour is with `head`._? – β.εηοιτ.βε May 01 '23 at 08:06
  • @β.εηοιτ.βε Ok but then why does it work with bash but not with busybox? Do they treat the pipe status different? – msrd0 May 01 '23 at 08:07
  • Ah looks like GNU grep tolerates being interrupted and busybox grep doesn't – msrd0 May 01 '23 at 08:11
  • 2
    No, it's because of `pipefail`. It's the `grep Handlers=` command that's exiting with status 141, but since that's not the last command in the pipeline, the only way it matters is if `pipefail` is set. – Gordon Davisson May 01 '23 at 08:59
  • @GordonDavisson Yes, but I'm using pipefail with both bash and busybox ash. – msrd0 May 01 '23 at 09:43
  • I'm almost sure `pipefail` is unique to the `bash` shell. I can remember when it was added. `ash` is a minimalist shell. Hopefully you can find documentation for `ash` and check whether `pipefail` is part of `ash` or not. I don't think it is. Good luck. – shellter May 01 '23 at 18:05
  • Your monstrous pipeline looks like it wants to be reimplemented as a simple Awk script, or else scour the world eating brains. – tripleee May 02 '23 at 07:20
  • Or even just `sed '1,/Name="PC Speaker"/d' /proc/bus/input/devices | grep -m1 Handlers= | tr '= ' ' \n' | grep -m1 -E '^event[0-9]+$'` – tripleee May 02 '23 at 07:30
  • I can confirm that Busybox `ash` supports `pipefail`; if I try to pass in a different option it complains that it is unsupported. – tripleee May 02 '23 at 07:31

0 Answers0