Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Bash while loop

farenda 2015-06-19 0

Problem:

while loop is a basic construct in many programming languages. Bash also allows us to use it to iterate over many things. Learn here how!

Solution:

The syntax of while loop is the following:

while test-commands; do consequent-commands; done

The loop is executed as long as test-commands has zero exit status. Return status of the loop is return status of the last command or zero if no command was executed.

Here’s an example:

#!/bin/bash

echo "Number of params passed to the script: $#"

function param_printer() {
    # $# is number of params passed to the function!
    while [ $# -gt 0 ]; do
        echo "Parameter inside a function: ${1}"
        # shift parameters by 1 effectively dropping the first one:
        shift
    done
}

# Pass all script parameters to the function:
param_printer $@

# $# contains number of parameters
while [ $# -gt 0 ]; do
    # print the first parameter
    echo "Parameter: ${1}"
    # shift parameters by 1 effectively dropping the first one:
    shift
done

Note that function param_printer operates on a copy of parameters and shift doesn’t change the original script parameters!

Run the script and pass different number of arguments to how it works:

$> ./while-loop.sh
Number of params passed to the script: 0
$> ./while-loop.sh a b c
Number of params passed to the script: 3
Parameter inside a function: a
Parameter inside a function: b
Parameter inside a function: c
Parameter: a
Parameter: b
Parameter: c

The while loop is very often used in Bash to process script parameters. We’ll cover that in a future post. Stay tuned! :-)

Share with the World!
Categories Bash Tags bash
Previous: Gradle System.out err print
Next: Spock Framework Mock

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored