From df1d67ec1a5f04173c4819a66bdcff70cc05cc2b Mon Sep 17 00:00:00 2001 From: ancadm Date: Fri, 4 Sep 2020 18:35:19 -0400 Subject: [PATCH 1/3] Create 12-monkeys.sh --- 12-monkeys.sh | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 12-monkeys.sh diff --git a/12-monkeys.sh b/12-monkeys.sh new file mode 100644 index 00000000..fe5540b6 --- /dev/null +++ b/12-monkeys.sh @@ -0,0 +1,20 @@ +# exercise: implement another counting song (such as 12 days of Christmas) +# using loops and if statements. + +echo "Now, let's sing monkey jumping on the bed." +echo "How many monkeys are jumping on the bed?" +read number +while [ $number -ge 0 ]; do + if [ $number -ge 2 ]; then + echo "$number little monkeys jumping on the bed. One fell off and bumped its head." + echo "Momma called the doctor and the doctor said, 'No more Monkeys jumping on the bed!'" + elif [ $number -ge 1 ]; then + echo "$number little monkeys jumping on the bed. One fell off and bumped its head." + echo "Momma called the doctor and the doctor said, 'No more Monkeys jumping on the bed!'" + else + echo "No more monkeys jumping on the bed." + fi + + ((number = number - 1)) +done + #statements From 856680f65416b4e25d24d5218ee09f7f2b399316 Mon Sep 17 00:00:00 2001 From: ancadm Date: Fri, 4 Sep 2020 19:07:49 -0400 Subject: [PATCH 2/3] Create 13-RNG.sh --- 13-RNG.sh | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 13-RNG.sh diff --git a/13-RNG.sh b/13-RNG.sh new file mode 100644 index 00000000..869621f6 --- /dev/null +++ b/13-RNG.sh @@ -0,0 +1,26 @@ +#!/bin/bash +echo "Taisann Kham" +echo "Hello, welcome to Taisann's RNG guessing game" +echo "Please choose a number between 0 - 9" +RANGE=10 +rand=0 +rand=$RANDOM +let "rand %= $RANGE" +read input +loop=false +while [[ $loop != q ]]; do + if [[ $input -lt $rand ]]; then + echo "Your number is smaller than the correct number" + fi + if [[ $input -gt $rand ]]; then + echo "Your number is bigger than the correct number" + fi + if [[ $input -eq $rand ]]; then + loop=q + echo "Congratulations! You guessed the correct number, $rand!" + fi + if [[ $loop != q ]]; then + echo "Please choose a new number" + read input + fi +done From bcbe291ca2899d6ccd71451d3dbf2b6a05fc4e98 Mon Sep 17 00:00:00 2001 From: May Date: Mon, 23 Nov 2020 22:15:23 -0500 Subject: [PATCH 3/3] did bash exercises --- 01-read_input_ADM.sh | 2 +- 01-read_input_May.sh | 21 ++++++++++++++ 02-add_nums.sh | 9 ++++++ 03-happy.sh | 8 ++++++ 04-life.sh | 9 ++++++ 05-grade.sh | 13 +++++++++ 06-infinite.sh | 15 ++++++++-- 07-beer.sh | 62 +++++++++++++++++++++++++++++++----------- 08-count.sh | 15 +++++++--- 09-name.sh | 16 +++++++---- 10-greeter.sh | 12 +++++--- 11-song.sh | 38 ++++++++++---------------- May/eating-reminder.sh | 11 ++++++++ bears | 0 14 files changed, 175 insertions(+), 56 deletions(-) create mode 100644 01-read_input_May.sh create mode 100644 May/eating-reminder.sh create mode 100644 bears diff --git a/01-read_input_ADM.sh b/01-read_input_ADM.sh index 314c287b..1451088f 100644 --- a/01-read_input_ADM.sh +++ b/01-read_input_ADM.sh @@ -16,6 +16,6 @@ read filename echo "You want $filename" echo "Creating $filename ..." touch $filename -echo "$filename creted" +echo "$filename created" ls echo "Bye,bye" diff --git a/01-read_input_May.sh b/01-read_input_May.sh new file mode 100644 index 00000000..ac75a61e --- /dev/null +++ b/01-read_input_May.sh @@ -0,0 +1,21 @@ +#!/bin/bash +# read the name of the user and print hello + +echo "Hello! What is your name" +read name +echo "Welcome, $name" + +# single quotes prevent the expansion of the variable +echo 'Your name was stored in $name' + +# exercise: write a script that asks the user for a +# filename and create an empty file named after it + +echo "Hey $name, What is your filename?" +read filename +echo "You want $filename" +echo "Creating $filename ..." +touch $filename +echo "$filename created" +ls +echo "Bye,bye May" \ No newline at end of file diff --git a/02-add_nums.sh b/02-add_nums.sh index 2fd13cb5..32706778 100644 --- a/02-add_nums.sh +++ b/02-add_nums.sh @@ -13,3 +13,12 @@ echo "The sum is $sum" # exercise: ask the user for the width and height and present total # number of pixels +echo "Enter width" +read width +echo "Enter height" +read height + +total=$(( width * height )) +echo "The total is $total" + + diff --git a/03-happy.sh b/03-happy.sh index 8095d30e..fd661ccc 100644 --- a/03-happy.sh +++ b/03-happy.sh @@ -14,3 +14,11 @@ fi # exercise: write a script that prints whether today is # the weekend or not +echo "What day is it?" +read day + +if [ "$day" = "Saturday" -o "$day" = "Sunday" ]; then + echo "it's the weekend" +else + echo "it's not the weekend" +fi \ No newline at end of file diff --git a/04-life.sh b/04-life.sh index 537da386..0ddb91c6 100644 --- a/04-life.sh +++ b/04-life.sh @@ -14,3 +14,12 @@ fi # exercise: write a script that prints whether it is # morning or not +echo "Is it morning?" +read timeofday + +if [ "$timeofday" -eq 'Yes' -o "$timeofday" -eq 'yes']; then + echo "Good morning" +else + echo "Good afternoon/night" +fi + diff --git a/05-grade.sh b/05-grade.sh index c148449a..21edfecc 100644 --- a/05-grade.sh +++ b/05-grade.sh @@ -19,3 +19,16 @@ fi # that prints "it's cold" if the temperature is < 40 # it's chilly if < 60, it's okay if < 70 and, it's hot for # everything else + +echo "What is the current temperature?" +read temperature + +if [ $temperature -lt 40 ]; then + echo "It's cold." +elif [ $temperature -lt 60 ]; then + echo "It's chilly" +elif [ $temperature -lt 70 ]; then + echo "It's ok" +else + echo "It's hot" +fi \ No newline at end of file diff --git a/06-infinite.sh b/06-infinite.sh index 953bb98b..369756f4 100644 --- a/06-infinite.sh +++ b/06-infinite.sh @@ -3,10 +3,19 @@ # the if statement had, if-then-fi # while loop has, while-do-done # true and false are also Unix commands -while [ true ]; do - echo "infinite number of beer on the wall" -done +#while [ true ]; do + #echo "infinite number of beer on the wall" +#done # exercise: write a script that continues to look up # the ip address of a given hostname (using nslookup) until # the user decides to stop + +STARTING=true +while [ "$STARTING" == true ];do +nslookup https://www.google.com/ +echo "keep going?" +read STARTING +echo $STARTING +done + diff --git a/07-beer.sh b/07-beer.sh index c03aded6..7924ccec 100644 --- a/07-beer.sh +++ b/07-beer.sh @@ -1,24 +1,54 @@ #!/bin/sh -echo "Let's sing a beer song" -echo "How many bottles?" -read count - -while [ $count -ge 0 ]; do - if [ $count -ge 2 ]; then - echo "$count bottles of beer on the wall, $count bottles of beer" - echo "Take one down pass it around" - elif [ $count -eq 1 ]; then - echo "$count bottle of beer on the wall, $count bottles of beer" - echo "Take one down pass it around" - else - echo "no more bottles of beer on the wall" - fi +#echo "Let's sing a beer song" +#echo "How many bottles?" +#read count +#DAY=1 +#echo $(( $DAY + 5 )) +#while [ $count -ge 0 ]; do + #if [ $count -ge 2 ]; then + #echo "$count bottles of beer on the wall, $count bottles of beer" + #echo "Take one down pass it around" + #elif [ $count -eq 1 ]; then + # echo "$count bottle of beer on the wall, $count bottles of beer" + # echo "Take one down pass it around" + #else + # echo "no more bottles of beer on the wall" + #fi # the following statement is equivalent to: let "count=count-1" - ((count = count - 1)) + # ((count = count - 1)) -done +#done # exercise: implement another counting song (such as 12 days of Christmas) # using loops and if statements. + +echo "Let's sing a Christmas" +echo "which day" +read DAY +while [ $DAY -gt 0 ]; do + if [ $DAY = 1 ]; then + echo "On the first day of Christmas my true love gave to me +a partridge in a pear tree" + elif [ $DAY = 2 ]; then + echo "On the second day of Christmas my true love gave to me +$DAY turtle doves and a partridge in a pear tree" + elif [ $DAY = 3 ]; then + echo "On the third day of Christmas my true love gave to me +$DAY French hens, $DAY-1 turtle doves and a partridge in a pear tree" +elif [ $DAY = 4 ]; then + echo "On the fourth day of Christmas my true love gave to me +$DAY calling birds, $DAY-1 French hens +$DAY-2 turtle doves and a partridge in a pear tree" + else + echo "On the fifth day of Christmas my true love gave to me +$DAY gold rings, $DAY-1 calling birds, $DAY-2 French hens +$DAY-3 turtle doves and a partridge in a pear tree" +fi + + + # the following statement is equivalent to: let "count=count-1" + ((DAY = DAY - 1)) + echo "$DAY" +done diff --git a/08-count.sh b/08-count.sh index c09d7cae..2d679a24 100644 --- a/08-count.sh +++ b/08-count.sh @@ -5,13 +5,20 @@ # count how many lines each shell script contains -for file in *.sh; do +#for file in *.sh; do # backticks are used for command substitution # the command in the backticks are executed and # the output is returned back - lines=`wc $file | tr -s ' ' | cut -f2 -d' '` - echo "$file has $lines lines" -done + #lines=`wc $file | tr -s ' ' | cut -f2 -d' '` + #echo "$file has $lines lines" +#done # exercise: Loop over some type of files and use the # "grep" UNIX command to find snippets of strings in them. + +for file in *.sh; do + # backticks are used for command substitution + # the command in the backticks are executed and + # the output is returned back + grep -c 'for' $file +done diff --git a/09-name.sh b/09-name.sh index b9b330d0..ada3a51c 100644 --- a/09-name.sh +++ b/09-name.sh @@ -2,13 +2,19 @@ # look up ip addresses of various search engines -servers="yahoo.com google.com dogpile.com wolframalpha.com" +#servers="yahoo.com google.com dogpile.com wolframalpha.com" -for server in $servers; do - nslookup $server - echo "----------------------------" -done +#for server in $servers; do + # nslookup $server + #echo "----------------------------" +#done # exercise: Change the list of servers and also the # operation applied to them. For instance, use ping, # traceroute, or nslookup with other options. +servers="bing.com twitter.com facebook.com linkedin.com" + +for server in $servers; do + ping $server + echo "----------------------------" +done \ No newline at end of file diff --git a/10-greeter.sh b/10-greeter.sh index 824d2393..c990f838 100644 --- a/10-greeter.sh +++ b/10-greeter.sh @@ -4,26 +4,30 @@ function weekend_greet { - echo "Enjoy the weekend! ${1}!" + echo "Enjoy the weekend and don't worry about ${2}! ${1}!" } function weekday_greet { - echo "Hope you are working hard, ${1}!" + echo "Hope you are working hard on that ${2}, ${1}!" + } echo "Please enter your name" read name +echo "Please enter one task you're going to do today" +read task day=$(date | cut -d' ' -f1) # you can use this form instead of backticks if [ $day = "Sat" ] || [ $day = "Sun" ]; then - weekend_greet $name + weekend_greet $name $task else - weekday_greet $name + weekday_greet $name $task fi # exercise: Add a second argument to the function # and make it give a greeting based on the new # argument. + diff --git a/11-song.sh b/11-song.sh index c07bb776..9fbe1bfc 100644 --- a/11-song.sh +++ b/11-song.sh @@ -1,36 +1,31 @@ #!/bin/sh + +# exercise: Change the input question and case +# statement to another theme. + while [ true ]; do # you can show a prompt with the read command - read -p "Do rae mi fa so la ti do? (q to end) > " note + read -p "f u n d (q to end) > " letter -case $note in +case $letter in # each case matches a pattern - do|Do) - echo "Doe a deer a female deer" + f|F) + echo "F is for friends who do stuff together" ;; - rae|Rae) - echo "Ray a drop of golden sun" - ;; - mi|Mi) - echo "Me a name a call myself" + u|U) + echo "U is for you and me" ;; - fa|Fa) - echo "Far a long long way to run" + n|N) + echo "N is for anywhere and anytime at all" ;; - so|So) - echo "So a note that follows fa" - ;; - la|La) - echo "La a note that follow so" - ;; - ti|ta) - echo "Tea I drink with jam and bread" + d|D) + echo "Down here in the deep blue sea" ;; q) - echo "Hope you enjoyed the sound of music" + echo "Hope you enjoyed the Spongebob Song" exit 0 ;; *) @@ -39,6 +34,3 @@ case $note in esac done - -# exercise: Change the input question and case -# statement to another theme. diff --git a/May/eating-reminder.sh b/May/eating-reminder.sh new file mode 100644 index 00000000..d42601db --- /dev/null +++ b/May/eating-reminder.sh @@ -0,0 +1,11 @@ + +echo "what is your name?" +read name +echo "did you eat today??, $name" +read answer + +if [ "$answer" = "yes" ]; then + echo "Awesome :)" +else + echo "Remember to eat!" +fi \ No newline at end of file diff --git a/bears b/bears new file mode 100644 index 00000000..e69de29b