Results 1 to 6 of 6

Thread: Why is this script working? [BASH]

  1. #1
    Join Date
    Sep 2005
    Beans
    119
    Distro
    Xubuntu 11.10 Oneiric Ocelot

    Why is this script working? [BASH]

    I was reading a book on shell scripting and found this example:

    Code:
    #!/bin/sh
    pattern="$1"
    shift
    echo "Matching against '$pattern':"
    for string
    do
    case $string in
    $pattern) echo "$string: Match." ;;
    *) echo "$string: No match." ;;
    esac
    done
    This little script works as expected and accepts multiple arguments, but how does it substitute the values of the "STRING" variable when "shift" is not inside the loop?
    I am new to shell scripting but I just hate it when I find something that works and I don't know why it works. No troubleshooting can give me an answer to this one so I thought I try the forums. Also, using "for" without some kind of a list/array is poorly documented on the internet.

  2. #2
    Join Date
    Feb 2007
    Beans
    4,045
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Why is this script working? [BASH]

    When you omit the in words part, it assumes in "$@". Run help for in an interactive bash shell to see the syntax.

    Out of curiosity, which book is it?

  3. #3
    Join Date
    Feb 2010
    Location
    Silicon Valley
    Beans
    1,898
    Distro
    Xubuntu 12.04 Precise Pangolin

    Re: Why is this script working? [BASH]

    from the dash(1) man page:
    Code:
         The syntax of the for command is
    
               for variable [ in [ word ... ] ]
               do   list
               done
    
         The words following in are expanded, and then the list is executed repeatedly with the variable set to each
         word in turn.  Omitting in word ... is equivalent to in "$@".
    So your 'for string' is the same as 'for string in "$@"', which expands to all the remaining command line arguments.

  4. #4
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: Why is this script working? [BASH]

    From the zsh manual, I expect Bash works the same way:

    for name ... [ in word ... ] term do list done
    where term is at least one newline or ;. Expand the list of words, and set the parameter name
    to each of them in turn, executing list each time. If the in word is omitted, use the posi-
    tional parameters instead of the words.
    「明後日の夕方には帰ってるからね。」


  5. #5
    Join Date
    Sep 2005
    Beans
    119
    Distro
    Xubuntu 11.10 Oneiric Ocelot

    Re: Why is this script working? [BASH]

    Ah,
    Thanks for those answers, I understand it now.

    It from the "Beginning Portable Shell Scripting: From Novice to Professional" btw

  6. #6
    Join Date
    Feb 2007
    Beans
    4,045
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Why is this script working? [BASH]

    Quote Originally Posted by neur0 View Post
    Ah,
    Thanks for those answers, I understand it now.

    It from the "Beginning Portable Shell Scripting: From Novice to Professional" btw
    Aha, in that case this thread's title is misleading. Based on the title on that book, it most likely teaches you to write sh scripts, not bash scripts.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •