+ Reply to Thread
Results 1 to 2 of 2

Thread: Flow control in shell scripts

  1. #1
    DarkLordoftheMonkeys's Avatar
    DarkLordoftheMonkeys is offline Programming Professional
    Join Date
    Oct 2009
    Location
    Massachussets
    Posts
    255
    Blog Entries
    56
    Rep Power
    11

    Flow control in shell scripts

    This is my second tutorial on shell scripting. Before, I talked about shell variables. Now I will go into another important topic which is flow control.

    There are six types of control structures in bash: if/then, for, case, while, until, and select.

    The if/then statement is much like the equivalent statement in other languages, and is used to test if a condition is true:

    Code:
    if [ condition ]
    then
    	commands;
    elif [ condition ]
    then
    	commands;
    else
    	commands;
    fi
    if [ condition ]; then literally means "If condition is true, then do what comes next." elif (the equivalent of else if) means "If not that but this...". Whatever comes after else is executed if none of the other conditions are true. fi (if spelled backwards) terminates the statement. Note that the elif and else sections are optional, but if, then, and fi are required.

    The while loop repeats a series of commands for the period of time that a certain statement is true.

    Code:
    while [ condition ]
    do
    	commands;
    done
    The until loop is basically the opposite of a while loop. It repeats a series of commands as long as a condition is false.

    Code:
    until [ condition ]
    do
    	commands;
    done
    Example:

    Code:
    #!/bin/bash
    declare -i int=0;
    until [ $int -eq 10 ]
    do
    	echo $int;
    	int++;
    done
    Outputs:
    Code:
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    declare -i int initializes an integer variable int. -eq is the Shell notation for "equals" (for strings you use the = sign). ++ is the operator to increase an integer's value by 1.

    The for loop is like the while loop, but rearranged. Instead of:

    Code:
    initialization;
    while condition
    do
    	commands;
    	increment;
    done
    You have:

    Code:
    for((initialization; condition; increment))
    do
    	commands;
    done
    This will be familiar to programmers with experience in languages like C, C++, and Java. Note, however, that this loop uses double parenthesis. This is to provide extra protection for the expressions inside.

    The for loop can also loop through variables in a list, repeating an action for each list item. For instance:

    Code:
    #!/bin/bash
    for param in $*
    do
    	echo $param;
    done
    If this script is called like this:

    Code:
    $ source forscript "snake" "camel"
    It will output:

    Code:
    snake
    camel
    The case statement is similar to the switch-case statement of C-like languages, but with one major difference. Instead of testing the value of a variable, it tests to see whether a string matches a certain pattern. In Shell, these patterns are known as globs.

    Code:
    case $file in
    	*.html )
    		echo "HTML file";;
    	*.css )
    		echo "CSS stylesheet";;
    	*.js )
    		echo "Javascript script";;
    	*
    		echo "Another type of file";;
    esac
    Here, *.html, *.css, etc. are the globs. The * means any string of characters, so *.html would match either browser-stats.html or OS-stats.html.

    The last flow control statement is the select statement, which creates a menu of items in a list.

    Code:
    #!/bin/bash
    select file in *
    do
    	echo $file;
    	break;
    done
    This script will create a menu of files in the current directory. When the user selects one of the menu items, the name of the file will be output. break is necessary because it prevents the statement from looping indefinitely.
    Life's too short to be cool. Be a nerd.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Flow control in shell scripts

    Nice +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Talking to a running MC server using shell scripts ??
    By bbqroast in forum Linux Applications
    Replies: 6
    Last Post: 05-12-2011, 02:26 PM
  2. I've committed a ridiculous abuse of control flow.
    By rocketboy9000 in forum C and C++
    Replies: 1
    Last Post: 03-31-2011, 12:20 AM
  3. Java:Tutorial - Flow Control
    By John in forum Java Tutorials
    Replies: 1
    Last Post: 03-08-2011, 02:14 PM
  4. I/O in shell scripts
    By DarkLordoftheMonkeys in forum Linux Tutorials, Guides and Tips
    Replies: 2
    Last Post: 11-15-2009, 08:11 AM

Tags for this Thread

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