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

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • 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. Endpoint is not Created for Service in Kubernetes
  2. “su: Authentication failure” – in Docker
  3. How to use command redirection under Linux
  4. How To Change The Time Zone For A Docker Container
  5. How to check the status and space used by images and containers
  6. Examples of “shift” Command in Shell Scripts
  7. “docker dead but subsys locked” – error while starting docker
  8. 10 Sed (Stream Editor) Command Examples
  9. Kubernetes Command Line Reference (Cheatsheet)
  10. How To Access Kubernetes Dashboard Externally

You May Also Like

Primary Sidebar

Recent Posts

  • JavaFX ComboBox: Set a value to the combo box
  • Nginx load balancing
  • nginx 504 gateway time-out
  • Images preview with ngx_http_image_filter_module

© 2022 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright