1. The while loop
The while loop is TRUE until its condition becomes FALSE.Example 1: Counting
Create the following script loop_while1.sh and execute it.#!/bin/bash
COUNT=10
while [ $COUNT -gt 0 ]; do
echo Value of count is: $COUNT
let COUNT=COUNT-1
done
Example 2: Read from a text file
Create the following script loop_while2.sh and execute it with a file input../loop_while2.sh > hello.sh
#!/bin/bash
let count=0
while read LINE; do
echo $LINE
((count++))
done
echo "Total lines: $count"
Example 3: Loop condition in brackets
Create the following script loop_while3.sh and execute it.#!/bin/bash
while ((i<=10)) ; do
let COUNT++
echo $COUNT
if [ $COUNT -gt 10 ]; then
let COUNT=0
fi
done
Example 4: Infinite loop with true
Edit script loop_while3.sh and execute it with a file input.#!/bin/bash
while true; do
let COUNT++
echo $COUNT
if [ $COUNT -gt 10 ]; then
let COUNT=0
fi
done
2. Array variables
An array provide users with a way to process many data (elements) within a single variable name.Example 1: Declare array
Following declares an array, assigns it to a variable, counts number of elements and display individual elements.Create the following script var_array1.sh and execute it.
#!/bin/bash
# Declare an array
ARRAY=( 'blue sky' 'red ocean' green grass )
# Get number of elements in the arrays
ELEMENTS=${#ARRAY[@]}
echo "There were $ELEMENTS elements"
# echo each element in array
echo ${ARRAY[0]}
echo ${ARRAY[1]}
echo ${ARRAY[2]}
echo ${ARRAY[3]}
3. Read from a file
The read command together with an input redirect "<" allows a script to read line by line of a file.
Create the following script file_read.sh and execute it.
#!/bin/bash
# Reset counter to zero
count=0
# Read line by line of the file in argument 1
while read LINE; do
((count++))
echo "$count: $LINE"
done < $1
# Print total lines
echo "Total lines: $count"
4. The for loop
This loop is controlled by the values from an expression or from a list of items.Example 1: Loop while variable is more than 50
Create the following script loop_for.sh and execute it.#!/bin/bash
for ((COUNT=100; COUNT>50; COUNT--))
do
echo "> $COUNT "
done
Example 2: Loop until all items are used
Edit the script loop_for.sh and execute it.#!/bin/bash
# Use 3 items
for FILENAME in 1 2 3; do
echo $FILENAME
done
# Use 1 item
for FILENAME in "1 2 3"; do
echo $FILENAME
done
Example 3: Loop items resulting from a shell command
Edit the script loop_for.sh and execute it.#!/bin/bash
for FILENAME in $( ls /var/ ); do
echo $FILENAME
done
Example 4: Loop a range of items
Edit the script loop_for.sh and execute it.#!/bin/bash
for I in {1..10}
do
echo "> $I "
done
5. The until loop
Similar to the while loop, but it will only loop until the expression list is true.
Create the script loop_until.sh and execute it.
#!/bin/bash
let COUNT=0
until [ $COUNT -gt 10 ] ; do
let COUNT++
echo $COUNT
done
6. Find plain text files
The find command can be used to process selected files. Example, convert the filenames to upper case.Create the script findfile.sh and execute it.
#!/bin/bash
DIR="."
find $DIR -type f | while read file
do
echo $file |tr '[a-z]' '[A-Z]' |cut -c 3-
done
7. Quick menu input with select
A simple way to create menu and accept user input as integer. It uses the variable PS3 to prepare the question text and loops until there is a break.Create the script cond_select.sh and execute it.
#!/bin/bash
PS3="YOUR CHOICE:"
select OPTION in "Mon" "Tue" "Wed" "Thu" "Fri"; do
echo "You choose: $OPTION"
echo "Press Ctrl and c to quit"
done
8. The case to create a menu
If user questions needs to be formatted and user input might be a string, then the case statement can be used.Create the script cond_case.sh and execute it.
#!/bin/bash
S1="Which day to rest (Mon,Tue,Wed,Thu,Fri)?"
echo $S1
read OPTION
case $OPTION in
"Mon"| "mon") echo "Yesterday was just Sunday";;
"Tue") echo "You choose Tuesday";;
"Wed") echo "You choose Wednesday";;
"Thurs") echo "You choose Thursday";;
"Fri") echo "Friday is a good choice";;
*) echo "Invalid option";;
esac
9. Formatted printing with printf
The printf allows some formatting of output. Through the use of placeholders such as;- %d integer numbers
- %s a string
- %f floating numbers
The length of string or numbers and alignment can be assigned in printf.
- %10s Length of 10 characters
- %.3s Take only 3 characters
- %-10s Left align length of 10 characters
Example 1: Print string placeholders.
Create the script printf.sh and execute it.#!/bin/bash
S1="httpd"
printf "Process %s\n" $S1
printf "have PID : %s \n" `pidof $S1`
Example 2: Insert padding and use first 4 characters
Edit the script printf.sh and execute it.#!/bin/bash
S="Monday Tuesday Wednesday Thursday Friday Saturday"
for I in $S; do
printf "%12s Study\n" $(printf "%.4s" "$I")
done
Example 3: Align padding left
#!/bin/bash
S="Monday Tuesday Wednesday Thursday Friday Saturday"
for I in $S; do
printf "%-12.3s Study\n" "$I"
done
Example 4: Preformat padding as strings
Edit the script printf.sh and execute it.#!/bin/bash
divider===============================
divider=$divider$divider
header="\n %-35s %8s %10s\n"
format=" %-35s %08d %10s\n"
width=56
printf "$header" "COURSE" "INST ID" "ENROLLMENT"
printf "%${width}.${width}s\n" "$divider"
printf "$format" \
"Mathematic Form 3 (2014)" 13 20 \
"Physics Form 5 (2014)" 89 130 \
"English Language Form 3 (2014)" 3551 30
10. Print a line full length of screen
I found an interesting script to draw a line and have put the basics here.Create the script print_ruler.sh and execute it.
#!/bin/bash
rule () {
printf -v _hr "%*s" $(tput cols) && echo ${_hr// /${1--}}
}
rule "."
[Tutorial Main]
No comments:
Post a Comment