Help get this topic noticed by sharing it on Twitter, Facebook, or email.

Is this an appropriate to ask questions about bash programming?

1 person has
this question
  • Hi t. crook,

    Feel free to ask your questions here. We're most likely to be able to help if your questions are related to one of our books or products.

    Rachel James
    O'Reilly Media, Inc.
    • Rachelj,

      I'm having trouble with exercise 3.b., page 194, from "Learning the bash shell", 3rd edition. I've tried several different things, none of them satisfactory... Do you have any ideas? Do you have any way of reaching the authors? I've repeated the problem statement below. Any help with this would be appreciated. Thank you.

      You could use the above technique together with other eval tricks to implement new control structures for the shell. For example, see if you can write a script that emulates the behavior of a for loop in a conventional language like C or Pascal, i.e., a loop that iterates a fixed number of times, with a loop variable that steps from 1 to the number of iterations (or, for C fans, 0 to iterations-1). Call your script loop to avoid clashes with the keywords for and do.
    • Rachel,

      The following session shows, so far, my best attempt at this... Is there any way to improve on this? The question implies that indirect expansion should be used - which this example doesn't... Also, the use of $loopcnt is awkward as that could conflict with another variable. Then, the dependence on single quotes is clumsy. Really would like to know what I'm missing here - there's gotta be a better way. Thank you.

      tcrook@tcrook820 /c/acme/dirx/ltbs/7-3-3-b
      $ cat shortexample
      #! /bin/bash

      loop='let i=0; while [ 1 ]; do'
      pool='if [ $((++i)) -eq $loopcnt ]; then break; fi done'

      eval $loop loopcnt=2 '
      echo "loop \$i: $i *"
      echo "pi day:" `date +%m%d`
      ' $pool

      tcrook@tcrook820 /c/acme/dirx/ltbs/7-3-3-b
      $ ./shortexample
      loop $i: 0 *
      pi day: 0314
      loop $i: 1 *
      pi day: 0314
  • (some HTML allowed)
    How does this make you feel?
    Add Image

    e.g. kidding, amused, unsure, silly sad, anxious, confused, frustrated happy, confident, thankful, excited indifferent, undecided, unconcerned