[haiku-development] Re: Weird Bootscript scripting problem

  • From: Donn Cave <donn@xxxxxxxxxxx>
  • To: haiku-development@xxxxxxxxxxxxx
  • Date: Wed, 22 Apr 2009 14:09:13 -0700 (PDT)

Quoth "=?utf-8?q?Fran=C3=A7ois?= Revol" <revol@xxxxxxx>,
>> > ($? = 0 if the first button was pressed, 1 for the second and 2 for 
>> > the 
>> > third)
>> 
>> Thanks a lot, this works more reliable when there is some debug 
>> output 
>
> Yes it also won't break if you change the button names :)

But it will break if you change the order, and it isn't as easy to
read and understand.  Maybe more ideal,

  IOPT="Installer"
  DOPT="Desktop"
  opt=$(/bin/alert "Run Installer, or boot to Desktop?" "$IOPT" "$DOPT")
  case $opt in
  $IOPT)
      echo 'Launching ...
      ...
      exit 0
      ;;
  $DOPT)
      ;;
  *)
      echo "WTF is \"$opt\"?"
      ;;
  esac

On the other hand, if you happen have a yes/no convention where yes is
always presented on the left, and no on the right, then you can use
"if" to test the exit status directly:

  if /bin/alert "yes or no" "yes" "no"
  then
      ... yes
  else
      ... no
  fi

> single quotes disable variable substitutions:

(and `backtick` substitution, which in modern shells can also be written $(),
syntactically more robust.)

Speaking of Bourne shell syntax weaknesses - I couldn't find anything
wrong with the script, so I could only guess that the actual Bootscript
you were running was slightly different from the patch.  And I'm guessing
the difference was 

  if [ "$installer" =    <- works

vs.

  if ["$installer" =    <- fails the way you describe

"[" is actually a `command', in Bourne shell syntax - it isn't punctuation
or a metacharacter or whatever you want to call it, it's just like any
alphabet character, and classically it invokes /bin/[  -- the same command
as /bin/test, but with a pro forma requirement for a trailing ']'.

Bash may substitute a builtin, I forget, but syntactically it's bound to
follow the same rules:  invoke the test command, and note its exit status.
If you omit the space after [, it will try to invoke [Install.

"case" is different - it's syntactically native to the shell language,
so for example you don't have to quote the argument.

        Donn


Other related posts: