I made a simple script on WSL for windows 11 to mount my pen drive. The default option is to mount it from D:, and I can specify as an argument if the pen drive is in another place.
The script is like this:
#! /bin/bash [[ $# -eq 0 ]] && sudo mount -t drvfs D: /pen || sudo mount -t drvfs ${1}: /pen [[ $? == 0 ]] && echo "Good" || echo "Bad" It works when the pen drive is connected on port D:, but when there is no pen drive connected, the first command executes both sides of the OR.
I am expecting it to execute on the same basis of al if-else statement. If the condition is true, it executes the left side, and if it is false, it executes the right side.
&& ... ||trick is not equivalent to anif ... then ... else ... fi. Don't use it, it just doesn't work. See BashPitfalls #22 and "Can't increment variable in bash shorthand if else condition".if...elseat this point, but if you insist on keeping this form, you can do:A && { B; true; } || Csudo mount -t drvfs "${1:-D}:" /penif....fiinto a single line, if you want. Statements are separated by either newlines or semicolons.A && B || Cdoes not equalif A then B else C fi, it equalsif A then if ! B then C fi; else C fi.