Page 1 of 1
Is my bash broken? (Solved)
Posted: Tue Dec 13, 2022 7:24 am
by disciple
qpdf has some options which test various things about a pdf file and output a return code for easy scripting.
The manual for qpdf suggests that I can use code like this:
Code: Select all
if [ qpdf --is-encrypted *3* ]; then echo "yes"; else echo "no"; fi
It does not work (see second to last command below). Is the syntax in the manual wrong, or is there something weird about my bash or something in my environment?
Code: Select all
# qpdf --is-encrypted *2*
# echo $?
2
# qpdf --is-encrypted *3*
# echo $?
0
# qpdf --is-encrypted *2*
# if [ $? = 0 ]; then echo "yes"; else echo "no"; fi
no
# qpdf --is-encrypted *3*
# if [ $? = 0 ]; then echo "yes"; else echo "no"; fi
yes
# if [ qpdf --is-encrypted *3* ]; then echo "yes"; else echo "no"; fi
-bash: [: --is-encrypted: binary operator expected
no
# if [ $(qpdf --is-encrypted *3*) ]; then echo "yes"; else echo "no"; fi
no
Thanks.
Re: Is my bash broken?
Posted: Tue Dec 13, 2022 8:06 am
by ozsouth
@disciple - I think *3* within square backets alone needs to be inside single quotes.
Re: Is my bash broken?
Posted: Tue Dec 13, 2022 8:40 am
by disciple
Perhaps I'm not following what you mean - neither of these syntaxes work:
Code: Select all
# if [ qpdf --is-encrypted '*3*' ]; then echo "yes"; else echo "no"; fi
-bash: [: --is-encrypted: binary operator expected
no
# if [ 'qpdf --is-encrypted *3*' ]; then echo "yes"; else echo "no"; fi
yes
# if [ 'qpdf --is-encrypted *2*' ]; then echo "yes"; else echo "no"; fi
yes
Re: Is my bash broken?
Posted: Tue Dec 13, 2022 8:43 am
by Burunduk
disciple wrote: ↑Tue Dec 13, 2022 7:24 amIs the syntax in the manual wrong, or is there something weird about my bash or something in my environment?
The syntax in the manual is wrong.
qpdf manual wrote:Use with the --password option to specify the password to test.
The choice of exit status 0 to mean that a password is required is to enable code like
Code: Select all
if [ qpdf --requires-password file.pdf ]; then
# prompt for password
fi
[↑]
qpdf
here is not a command but a first positional parameter to [
a.k.a. test
:
Code: Select all
root# if [ whoami ]; then echo "yes"; fi # whoami command doesn't run
yes
root# if whoami ; then echo "yes"; fi # whoami command runs
root
yes
It should be (without [...]
):
Code: Select all
if qpdf --requires-password file.pdf ; then
# prompt for password
fi
Use if [ $? -eq 2 ]; then
if you need to test for a specific return value.
Re: Is my bash broken?
Posted: Tue Dec 13, 2022 9:44 am
by disciple
Burunduk wrote: ↑Tue Dec 13, 2022 8:43 am
disciple wrote: ↑Tue Dec 13, 2022 7:24 amIs the syntax in the manual wrong, or is there something weird about my bash or something in my environment?
The syntax in the manual is wrong.
Great, thanks.
Of course, for the potential benefit of others, if I only want to confirm that the exit code is zero, or only want to confirm that the exit code is not zero, it is probably more normal just to use && or ||
Code: Select all
# qpdf --is-encrypted *x*2* && echo "test"
# qpdf --is-encrypted *x*2* || echo "test"
test
# qpdf --is-encrypted *x*3* && echo "test"
test
# qpdf --is-encrypted *x*3* || echo "test"
#