From 357184cdc8658f54b5d95ab38ec86d5d7a14f262 Mon Sep 17 00:00:00 2001 From: Dao Hieu Trung <Dao2.Trung@live.uwe.ac.uk> Date: Sat, 26 Oct 2024 19:10:28 +0100 Subject: [PATCH] practice 3 --- helloworld.sh | 8 ++++++++ week3/exercise3_compare.sh | 17 +++++++++++++++++ week3/exercise3_if.sh | 8 ++++++++ week3/exercise3_loop.sh | 9 +++++++++ 4 files changed, 42 insertions(+) create mode 100755 helloworld.sh create mode 100755 week3/exercise3_compare.sh create mode 100755 week3/exercise3_if.sh create mode 100755 week3/exercise3_loop.sh diff --git a/helloworld.sh b/helloworld.sh new file mode 100755 index 0000000..bf3f7c5 --- /dev/null +++ b/helloworld.sh @@ -0,0 +1,8 @@ +#/bin/bash +lsOutput=$(ls -lrt) +echo $lsOutput + +echo "Enter a number " +read number +echo "It's $number" + diff --git a/week3/exercise3_compare.sh b/week3/exercise3_compare.sh new file mode 100755 index 0000000..698cf70 --- /dev/null +++ b/week3/exercise3_compare.sh @@ -0,0 +1,17 @@ +echo "Enter a number: " +read num1 +echo "Enter another number: " +read num2 + +if [ "$num1" = "$num2" ]; then + echo "$num1 equals to $num2" +else + if [ "$num1" -gt "$num2" ]; then + echo "$num1 is greater than $num2" + else + echo "$num1 is smaller than $num2" + fi +fi + +sumNum=$((num1 + num2)) +echo "$sumNum" \ No newline at end of file diff --git a/week3/exercise3_if.sh b/week3/exercise3_if.sh new file mode 100755 index 0000000..2492fbb --- /dev/null +++ b/week3/exercise3_if.sh @@ -0,0 +1,8 @@ +echo "Enter a number: " +read num + +if [ $((num % 2)) -eq 0 ]; then + echo "$num is even" +else + echo "$num is odd" +fi \ No newline at end of file diff --git a/week3/exercise3_loop.sh b/week3/exercise3_loop.sh new file mode 100755 index 0000000..1a50ddd --- /dev/null +++ b/week3/exercise3_loop.sh @@ -0,0 +1,9 @@ +numbers=() +sum=0 +for ((i=1; i<=10; i++)); do + read -p "Enter number $i: " num + numbers+=("$num") + sum=$((sum+num)) +done + +echo "The sum of the array is $sum" \ No newline at end of file -- GitLab