Raspberry pi Projects

  • Bash script loops

    Every programing language has loops and functions to make the program easy to make, today I will try to cover all the bash script loops so let get started.

    while loop

    the first loop it's the conditional while loop, "let" this command line to name a variable and it can be string or float or anything else, the "$x" it's present the variable value and the "-lt" means bigger than in bash script.
    the "do" and "done" function to keep the loop continues until complete the condition.
    #!/bin/bash
    let x=0 
    while [ $x -lt 10 ] 
    do  
    	sleep 1 
    	let x=$x+1 
    	echo $x 
    done
    

    The same loop but in a decreasing way, "-gt" means less than the x variable
    #!/bin/bash
    let x=10
    while [ $x -gt 0 ]
    do
    	sleep 1
    	let x-=1
    	echo $x
    done								 
    

    case loop

    the case loop it's for make options in the program, "read" it's command for input and put it in the variable I call it "ans" which is a sort to answer
    "case $ans in" its means compare the "ans" if the input equal "y" then print the message, "you say yes" and "n" the same, and if you enter the letter "q"
    then it will be out from the loop by increasing the variable "x" by breaking the while condition
    the "*)" its means anything else, "esac" to close the case loop.
    #!/bin/bash
    x=0
    while [ $x = 0 ]
    do
    	echo 'do like candies? (y/n)- Press "q" to quit'
    	read ans
    	case $ans in
    	y)
    	echo 'you said yes'
    	;;
    	n)
    	echo 'you said no'
    	;;
    	q)
    	echo 'Exit...'
    	x=1
    	sleep 1
    	;;
    	*)
    	clear
    	echo 'that not option'
    	;;
    	esac
    done						 
    

    select loop

    the select loop I use it to for limiting the options and it's work like the while but with out condition so I make the "quit" option to break the loop
    note: do not make spaces between the letters inside the options the program will not working example "hello world".
    #!/bin/bash
    echo 'what do you like?'
    select ans in 'Gold' 'Silver' 'Tin' 'quit'
    do
    	case $ans in
    	quit)
    	echo 'exiting...'
    	sleep 1
    	break
    	;;
    	esac
    	echo "you press:- $ans"
    done						 
    

    if loop

    the most usable loop in the bash script, as you see in the example below, you can name the variable without "let" command and the bash script will recognize it automatically if it was a string or float or integer.
    so this loop starts with "if" and between the arrows, we put the conditions, the "fi" it's command for ending of the if loop.
    #!/bin/bash
    a=0
    b=10
    c=10
    while [ $c -gt 0 ]
    do
    	let a=$a+1
            let b-=1
    	let c-=1
    	if [ $a == $b ];
    	then
    		echo " $a it's = $b"
    	fi
    
    	if [ $a != $b ];
    	then
    		echo " $a don't equal $b"
    	fi
    	sleep 1
    done			 
    

    if and else if

    this loop 'is exactly the same as if loop but with little modification, I add great than and less than and also else if.
    #!/bin/bash
    a=10;b=0;c=0
    while [ $c -lt 10 ]
    do
    	let a-=1;let b+=1;let c+=1
    	if [ $a -lt $b ];
    	then
    		echo "$a less than $b"
    	elif [ $a -gt $b ];
    	then
    		echo "$a greater then $b"
    	else
    		echo "$a it's equal $b"
    	fi
    	sleep 1
    done						 
    

    Equal-OR in the if loop

    the equal condition in the if loop writes like this "-eq" and the or write "-a", so as you see in the example below I make two conditions must be happening to break the loop.
    #!/bin/bash
    a=0;b=10
    while [ 1 ]
    do
    	if [ $a -eq 5 -a $b -eq 5 ];
    	then
    		echo "the end"
    		break
    	fi
    	let a+=1
    	let b-=1
    	echo "$a : $b"
    	sleep 0.5
    done 
    

    for loop

    the for loop it's mean the program jam in this loop until it finishes, at the beginning we name variable and input the values, the for loop use "do" "done" to do it.
    #!/bin/bash
    for var in 0 1 2 3 4 5 6 7 8 9 10
    do
    	echo $var
    	sleep 1
    done			 
    

    until loop

    the until loop it is exactly the same as while loop.
    #!/bin/bash
    a=0
    until [ ! $a -lt 10 ]
    do
    	echo $a
    	let a+=1
    	sleep 1
    done