I have this exercise:

Write a script, which uses if-then-else-fi and accept sequence of parameters -a and -b with values "true" and "false", while for every pair writes negation of expression -a <=> -b,


-a       -b        writes

true     true      false

true     false     true

false    true      true

false    false     false

For example:

./script -a false -b false -a true -b true




My question is how to make script accept parameters?

I just struggle with the input. Everything else should be fine.

  • Is this a shell script? Bash? Python? Something else? – Wildcard Apr 11 '16 at 20:28
  • 1
    You may want to check out the getopts tutorial. – Wildcard Apr 11 '16 at 20:28
  • Yes, it is bash – Mafi Apr 11 '16 at 20:32
  • Also, your spec is missing some behavior—what does ./script -a false -a false do? Or ./script -a false -b false -a true? – derobert Apr 11 '16 at 20:40
  • That enters boolean values into the script – Mafi Apr 11 '16 at 20:42
if [ $( $2 or 0 ) xor $( $4 or 0 ) ]; then echo true; else echo false; fi


Based on the logic table that is an XOR operation so using $2 and $4 (the second and fourth arguments which have the data we need to do this operation) of course this doesn't account for when someone switches -a and -b.

  • I didn't realize you were trying to get it to process multiple sets. for that you're going to need a for loop. – Teale Sloterdijk Apr 12 '16 at 1:25
  • Is this intended to be pseudo-code? The shell certainly does not handle this the way you think it does. Hint: it always echoes true – glenn jackman Apr 12 '16 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.