• 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. How to change the default IP address of docker bridge
  2. gradle: command not found
  3. Docker Basics – Expose ports, port binding and docker link
  4. How to convert text files to all upper or lower case
  5. What is the purpose of .bash_profile file under User Home Directory In Linux
  6. Endpoint is not Created for Service in Kubernetes
  7. The Ultimate Guide To Use VS Code With Windows Subsystem for Linux (WSL)
  8. How to resolve Docker Search Command Error – “getsockopt: no route to host” in CentOS / RHEL / Fedora
  9. How to Use External Python modules in MySQL Shell
  10. How to change the audit log path in the MySQL Docker

You May Also Like

Primary Sidebar

Recent Posts

  • diffstat: Create a histogram from the output of the diff command
  • diffoscope: Compare files, archives, and directories
  • diff-pdf: Tool for comparing two PDFs
  • dict: Command line dictionary using the DICT protocol

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright