Bash if and question

/etc/hotplug.d/block/20-sleep

#!/bin/bash

logger -t hotplug "action='$ACTION' type='$DEVTYPE' major='$MAJOR' devname='$DEVNAME'"

if [ "$ACTION" = "add" ] && [ "$DEVTYPE" = "disk" ] && [ "$MAJOR" = "8" ]; then
  output=$(/sbin/hdparm -B 1 -S 60 /dev/$DEVNAME)
  logger -t hotplug "$output"
fi

Why is [ "$ACTION" = "add" ] && [ "$DEVTYPE" = "disk" ] && [ "$MAJOR" = "8" ] condition not working?

What's the logger output?

1 Like
Sat Dec  9 15:23:23 2023 user.notice hotplug: action='add' type='disk' major='8' devname='sdb'
Sat Dec  9 15:23:24 2023 user.notice hotplug: action='add' type='partition' major='8' devname='sdb1'

it's just if condition doesn't meet for a reason. I know Python but not Bash.

Hi

please try to remove this line

hotplug scripts are sourced and not executed

This will not make a difference because the she-bang will be ignored if the file is sourced.

1 Like

Did you accidentally use windows newlines?

3 Likes

Besides the fact that on a default OpenWrt there is not bash but ash, I don't spot any obvious error. As far as I can tell, all this is POSIX compliant.
shellcheck also does not see any issue with your if statement.

Hopefully OP installed bash.

... and maybe hdparm in located elswhere ?
maybe in /usr/sbin ??? like snmpd & lldpd & many others ?

It might be the single quotes in logger, afaict those are literal quotes
eg:

action='$ACTION' is not the same as action="$ACTION"

If the variables are literal then they will never change and thus never pass the condition.
Try:

logger -t hotplug "action=\"$ACTION\" type=\"$DEVTYPE\" major=\"$MAJOR\" devname=\"$DEVNAME\""

Single quotes in double quoted strings have no specific meaning, they’re just ordinary characters.

1 Like

Unbelievable...
Windows newlines gave me headaches in the past in Python too.
I had used MobaXterm's editor. Thank you!

2 Likes

dos2unix FTW.

1 Like

Maybe not here in the logger example, but they definitely do in shell script.

$ a=cheese
$ b='$a'
$ c="$a"
$ echo $a
cheese
$ echo $b
$a
$ echo $c
cheese
$ 

Yeah but echo "'$a'" is not the same as echo '$a'

1 Like

In these type of errors, i always forget (or take as impossible) that someone using winscp or whatever only to transfer 10 words long file from PC to OWRT

There is a VI on OWRT, and it feels so natural to use it for 1-2KB scripts

1 Like

I'm kinda offended by your post. I said I don't know Bash :face_with_raised_eyebrow:
If you want to stick with VI, Nano etc good for you. I never use VI for coding.
There's a reason why we have VSCode, IntelliJ, VStudio etc in these days.
You sound like an Assembly guy trying to humiliate C people.

Don't be :smile: I think he means, you could have copy and pasted it in 'vi' (using putty). Your method works but has some drawbacks.

1 Like

RE: vi or vim; it is at least useful to know that i gets you into insert mode, with ESC you got back to "command mode" or how ever it is called, and with :wq you write and quit. Its fine to navigate with the arrow keys. It does not hurt to know at least this little bit. And yeah kidda lol you got hit by line encodings, I would have not guessed this, because half your script worked :smiley:

I know vi and I use it time to time for just small edits.
But for programming? No function parameter hints, no autocomplete, no syntax highlighting etc. Hell no.