Is my bash broken? (Solved)

For discussions about programming, and for programming questions and advice


Moderator: Forum moderators

Post Reply
disciple
Posts: 18
Joined: Fri Dec 18, 2020 5:54 am

Is my bash broken? (Solved)

Post 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.

Last edited by disciple on Tue Dec 13, 2022 9:10 am, edited 1 time in total.
ozsouth
Posts: 1545
Joined: Sun Jul 12, 2020 2:38 am
Location: S.E. Australia
Has thanked: 241 times
Been thanked: 684 times

Re: Is my bash broken?

Post by ozsouth »

@disciple - I think *3* within square backets alone needs to be inside single quotes.

disciple
Posts: 18
Joined: Fri Dec 18, 2020 5:54 am

Re: Is my bash broken?

Post 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
Burunduk
Posts: 250
Joined: Thu Jun 16, 2022 6:16 pm
Has thanked: 7 times
Been thanked: 126 times

Re: Is my bash broken?

Post by Burunduk »

disciple wrote: Tue Dec 13, 2022 7:24 am

Is 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.

disciple
Posts: 18
Joined: Fri Dec 18, 2020 5:54 am

Re: Is my bash broken?

Post by disciple »

Burunduk wrote: Tue Dec 13, 2022 8:43 am
disciple wrote: Tue Dec 13, 2022 7:24 am

Is 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"
#
Post Reply

Return to “Programming”