• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • VCS
  • Interview Questions
  • Database
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to use until loop in Shell Scripts

by admin

The until Loop

The until loop is very similar to the while loop, except that the until loop executes as long as the command fails. After the command succeeds, the loop exits and execution of the script continues with the statement following the done statement. The syntax for the until loop is:

until control_command 
do
    statement1
    ...       
    statementN
done

The control_command can be any command that exits with a success or failure status. The statements can be any utility commands, user programs, shell scripts, or shell statements. If the control_command fails, the body of the loop (all the statements between do and done) execute, and the control_command executes again. As long as the control_command continues to fail, the body of the loop continues to execute. As soon as the control_command succeeds, the statement following the done statement executes; for example:

$ cat until.ksh 
#!/bin/ksh

# Script name: until.ksh

num=1

until (( num == 6 )) 
do
        print "The value of num is: $num"
        (( num = num + 1 ))
done
print "Done."

Here is the output of the script:

$ ./until.ksh  
The value of num is: 1 
The value of num is: 2 
The value of num is: 3 
The value of num is: 4 
The value of num is: 5 
Done.
“while” Loop Examples in Shell Scripts
Bash for loop Examples

Filed Under: DevOps, Shell Scripting

Some more articles you might also be interested in …

  1. “docker machine” Command Examples
  2. How to convert text files to all upper or lower case
  3. My Development Environment Set up on Windows to use Python for Web Dev & Data Science
  4. 2to3 – Automated Python 2 to 3 code conversion
  5. How to Download Ext JS GPL
  6. “docker system” Command Examples
  7. How to Map Static IP to your Domain (with GoDaddy example)
  8. How to Create a Public/Private Repository in Docker Hub and connect it remotely using command line
  9. How to update/add a file in the Docker Image
  10. Understanding Positional Parameters (Passing Parameters/Arguments to Shell script)

You May Also Like

Primary Sidebar

Recent Posts

  • gml2gv Command Examples
  • glow Command Examples
  • glib-compile-resources Command Examples
  • glances Command Examples

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright