Bash - While

Bash Liste Des Attaques Ovh

About

This article is dedicated to the while syntax in Bash.

Example

Sequence

Bash - Sequence

count=0  
while [ $count -le 10 ]  
do  
    echo "$count"
    count=$(( $count + 1 ))
done  

Infinite loop

done=0
while : ; do
  ...
  if [ "$done" -ne 0 ]; then
      break
  fi
done

where:

  • : is the no-op command; its exit status is always 0

Basic

The command:

ls -l | while read -r lineVariable; do
    echo Line: $lineVariable
done
Line: -rw-r--r-- 1 root root 1849173 May 13 2015 unixODBC-2.3.2.tar.gz
Line: -rwxr-xr-- 1 oracle oinstall 64 May 16 2017 whileDemo.sh

Process monitoring

Linux process monitoring (replace pid with your pid)

pid=22018
count=0
while kill -0 $pid 2> /dev/null; do
    count=$(( $count + 1 ))
    echo "${count} - Process is running"
    sleep 10
done
echo "${count} - Process has exited"

Stream / Output parsing

ls -l | while read -r permission child owner group size monthDate dayDate rest; do
    echo Line: 
    echo '    - Permission: '$permission
    echo '    - Child: '$child
    echo '    - Owner: '$owner
    echo '    - Group: '$group
    echo '    - Size: '$size
    echo '    - Month of Date: '$monthDate
    echo '    - Day of Date: '$dayDate
    echo '    - Rest: '$rest
done
Line:
    - Permission: -rw-r--r--
    - Child: 1
    - Owner: root
    - Group: root
    - Size: 1849173
    - Month of Date: May
    - Day of Date: 13
    - Rest: 2015 unixODBC-2.3.2.tar.gz
Line:
    - Permission: -rwxr-xr--
    - Child: 1
    - Owner: oracle
    - Group: oinstall
    - Size: 457
    - Month of Date: May
    - Day of Date: 16
    - Rest: 2017 whileDemo.sh

Csv to Xml

How to converse a Csv to XML 1)

file_in="simple.csv"
file_out="simple.xml"
echo '<?xml version="1.0"?>' > $file_out
echo '<Customers>' >> $file_out
while IFS=$',' read -r -a arry
do
  echo '  <Customer>' >> $file_out
  echo '    <Name>'${arry[0]}'</Name>' >> $file_out
  echo '    <Age>'${arry[1]}'</Age>' >> $file_out
  echo '    <Country>'${arry[2]}'</Country>' >> $file_out
  echo '  </Customer>' >> $file_out
done < $file_in
echo '</Customers>' >> $file_out

Syntax

while list; do list; done

where you can also use the break and continue loop control statement.

  • Exit: The while command continuously executes the do list as long as the last command in list returns an exit status of zero.
  • Exit status: The exit status of the while and until commands is the exit status of the last do list command executed, or zero if none was executed.





Discover More
Bash Liste Des Attaques Ovh
Bash - (Argument|Positional Parameter)

An argument is a parameter given: to a command to a function or to the bash shell They are referenced by position. A positional parameter is a parameter denoted: by one or more digits, ...
Bash Liste Des Attaques Ovh
Bash - Break

A flow command that exit from within: a for, while, until, or select loop. where: n is the break level. n must be ≥ 1. If n is greater than the number of enclosing loops, all...
Bash Liste Des Attaques Ovh
Bash - Continue

A control flow statement where: n resume at the nth enclosing loop. n must be ≥ 1. If n isgreater than the number of enclosing loops, the last enclosing loop (the ‘‘top-level’’...
Bash Liste Des Attaques Ovh
Bash - Loop

The loop structures in Bash are: for while select until You can use the break and continue statement to control the loop behavior. When calling a loop, quote the variable otherwise bash...
Bash Liste Des Attaques Ovh
Bash - Sequence

A sequence expression is a construct that creates sequence of numbers or characters. It's a brace expansion and takes the form where x and y are either: integers or single characters. A correctly-formed...
Bash Liste Des Attaques Ovh
Bash - Set (of Bash Options)

The set builtin command can be specify shell option. When options are specified, they set or unset shell attributes. Without options, the name and value of each shell variable are displayed in a...
Bash Liste Des Attaques Ovh
Bash - Until

The until syntax in bash The until command is identical to the while command, except that the test isnegated; the do list is executed as long as the last command in list returns a non-zero exit status....
Bash Liste Des Attaques Ovh
How to read a CSV or a INI file with Bash

How to parse a CSV or property (ini) in bash where: the first FOR iterate over a list of ini file in the current directory. the while read: reads a line of ${FILE} until it finds an EOF parse...
Linux - Process

in linux With the ps utility How to get the PID (process identification number) of a process ( $$ pgrep Function example Priority gives the process more or less CPU time than...



Share this page:
Follow us:
Task Runner