More Mental Model Missteps

   
Recent changes
Table of contents
Links to this page
FRONT PAGE / INDEX

Subscribe!
@ColinTheMathmo

My latest posts can be found here:
Previous blog posts:
Additionally, some earlier writings:
I mentioned when talking about an oddity in vim that the behaviour of the command "<n>J" didn't match my mental model. Sometimes software does that, it had behaviour that's totally reasonable when you think about it one way, but there's another way of thinking that causes you to be surprised.

Looping over the parameters:
 
  for n in $*
  do
    <use parameter $n>
  done

Using them one-by-one:
 
  while [[ -n ""$1 ]]
  do
    <use parameter $1>
    shift
  done
Here's another as reported to me by a friend.

In shell scripting in *nix we can write a script that takes a collection of parameters. Sometimes the script will deal with them one at a time, and can loop over them.

On the other hand, sometimes we want to deal with the first, and iterate until we are done.

The shift command deletes parameter $1 and shifts each of the remaining parameters down by one.

So far so reasonable.

 
shift ; shift ; shift
Sometimes in scripting we want to shift by more than one place, so we execute multiple shift statements, as shown here.

If there are a lot, or we need it to vary, we can put that in a loop, like this:

 
  for i in $( seq $n ); do shift ; done

All that does is execute the shift command $n times. So if you want 10 shifts (which does, admittedly, seem a touch excessive) you can do this:

 
  for i in 10 ; do shift ; done

Checking the man page for shift we see that there's a short-cut for this, because shift takes a parameter to say how many arguments to shift by:

 
 shift <n>


So we think these are identical:

 
shift
shift
shift
 
shift 3

Well, they aren't.

Here are two scripts:

 
 #!/bin/bash
 # Script ShiftThreeTimes

 echo "Executing 'shift ; shift ; shift'"
 shift ; shift ; shift
 echo Resulting first parameter: $1
 
 #!/bin/bash
 # Script Shift3

 echo "Executing 'shift 3'"
 shift 3
 echo Resulting first parameter: $1

Executing these:

 
 ./ShiftThreeTimes 1 2 3 4
 
 ./Shift3 1 2 3 4

OK, so they both shift out the first three parameters and print the fourth one, printing "Resulting first parameter: 4".

No surprise ... let's try again:

 
..../ShiftThreeTimes 1 2
 
..../Shift3 1 2

Different results.

OK, so a more careful reading of the man shift page explains why this is so. The definition of shift &lt;n&gt; is not the same as executing shift "n" times. It mostly is, but in fact, it isn't.

So there we are, another of my mental models out the window.


<<<< Prev <<<<
An Oddity In VIM
:
>>>> Next >>>>
Tesseract Teaser ...


https://mathstodon.xyz/@ColinTheMathmo You can follow me on Mathstodon.



Of course, you can also
follow me on twitter:

@ColinTheMathmo


Send us a comment ...

You can send us a message here. It doesn't get published, it just sends us an email, and is an easy way to ask any questions, or make any comments, without having to send a separate email. So just fill in the boxes and then

Your name :
Email :
Message :


Contents

 

Links on this page

 
Site hosted by Colin and Rachel Wright:
  • Maths, Design, Juggling, Computing,
  • Embroidery, Proof-reading,
  • and other clever stuff.

Suggest a change ( <-- What does this mean?) / Send me email
Front Page / All pages by date / Site overview / Top of page

Universally Browser Friendly     Quotation from
Tim Berners-Lee
    Valid HTML 3.2!