Last active
September 18, 2023 14:00
-
-
Save harishkannarao/5f64fb92f17e9527859b9228effc0931 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Make the script to abort if any command fails. Use set +e to change the behaviour and ignore failed command. | |
set -e | |
# Uncomment below line to print the commands as it is executed. Useful for debugging. Use set +x to disable debugging. | |
# set -x | |
while [[ $# -gt 0 ]] | |
do | |
KEY="$1" | |
case $KEY in | |
"--conditional-while") | |
OPERATION="CONDITIONAL_WHILE" | |
shift | |
CONDITIONAL_WHILE_COUNT="$1" | |
shift | |
;; | |
"--print-file") | |
OPERATION="PRINT_FILE" | |
shift | |
FILE_TO_PRINT="$1" | |
shift | |
;; | |
"--infinite-while-read") | |
OPERATION="INFINITE_WHILE_READ" | |
shift | |
;; | |
"--infinite-while-sleep") | |
OPERATION="INFINITE_WHILE_SLEEP" | |
shift | |
;; | |
"--print-array-for") | |
OPERATION="PRINT_ARRAY_FOR" | |
shift | |
ARRAY_TO_PRINT=( "$@" ) | |
shift | |
;; | |
"--sum-for-loop") | |
OPERATION="SUM_FOR_LOOP" | |
shift | |
ARRAY_TO_SUM=( "$@" ) | |
shift | |
;; | |
"--iterative-for-loop") | |
OPERATION="ITERATIVE_FOR_LOOP" | |
shift | |
ITERATION_COUNT="$1" | |
shift | |
;; | |
-h|--help) | |
OPERATION="HELP" | |
shift | |
;; | |
*) | |
shift | |
;; | |
esac | |
done | |
function print_usage() | |
{ | |
echo "Usage:" | |
echo "bash loops.sh --conditional-while 5" | |
echo "bash loops.sh --print-file /tmp/testfile.txt" | |
echo "bash loops.sh --infinite-while-read" | |
echo "bash loops.sh --infinite-while-sleep" | |
echo "bash loops.sh --print-array-for \"value 1\" \"value 2\" \"skip\" \"break\" \"value 3\"" | |
echo "bash loops.sh --sum-for-loop 3 5 60 1" | |
echo "bash loops.sh --iterative-for-loop 6" | |
} | |
# Display usage of the script | |
if [[ "$DISPLAY_HELP" == "yes" ]] | |
then | |
print_usage | |
exit 0 | |
fi | |
function conditional_while() | |
{ | |
x=1 | |
while [[ $x -le $1 ]] | |
do | |
echo "Welcome $x times" | |
x=$(( $x + 1 )) | |
done | |
} | |
function print_file() | |
{ | |
if [[ ! ( -f $1 ) && ! ( -r $1 ) ]] | |
then | |
echo "File $1 doesn't exist or doesn't have read permission" | |
exit 1 | |
fi | |
cat "$1" | while read line | |
do | |
echo "$line" | |
done | |
} | |
function infinite_while_read() | |
{ | |
while : | |
do | |
read -p "Enter two numbers ( -1 to quit, 0 to skip ) : " a b | |
if [[ $a -eq -1 ]] | |
then | |
break | |
elif [[ $a == 0 ]] | |
then | |
continue | |
fi | |
echo "$a + $b" | bc | |
done | |
} | |
function infinite_while_sleep() | |
{ | |
while : | |
do | |
echo "Sleeping for 1.5 seconds, Time before sleep" | |
date | |
sleep 1.5 | |
echo "Woke up after 1.5 seconds, Time after wake up" | |
date | |
echo "Press Ctrl+C to exit" | |
date | |
done | |
} | |
function print_array_for() | |
{ | |
for i in "$@" | |
do | |
if [[ $i == "break" ]] | |
then | |
break | |
elif [[ $i == "skip" ]] | |
then | |
continue | |
else | |
echo "Hello $i" | |
fi | |
done | |
} | |
function sum_array_for_loop() | |
{ | |
SUM=0 | |
for i in "$@" | |
do | |
SUM=$(echo "$SUM + $i" | bc) | |
done | |
echo $SUM | |
} | |
function iterative_for_loop() | |
{ | |
for (( c=1; c<=$1; c=$c+1 )) | |
do | |
echo "Welcome $c times" | |
done | |
} | |
case "$OPERATION" in | |
"CONDITIONAL_WHILE") | |
conditional_while $CONDITIONAL_WHILE_COUNT | |
;; | |
"PRINT_FILE") | |
print_file "$FILE_TO_PRINT" | |
;; | |
"INFINITE_WHILE_READ") | |
infinite_while_read | |
;; | |
"INFINITE_WHILE_SLEEP") | |
infinite_while_sleep | |
;; | |
"PRINT_ARRAY_FOR") | |
print_array_for "${ARRAY_TO_PRINT[@]}" | |
;; | |
"SUM_FOR_LOOP") | |
sum_array_for_loop "${ARRAY_TO_SUM[@]}" | |
;; | |
"ITERATIVE_FOR_LOOP") | |
iterative_for_loop $ITERATION_COUNT | |
;; | |
"HELP") | |
print_usage | |
;; | |
*) | |
echo "Unknown operation" >&2 | |
print_usage | |
exit 1 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment