From 748344f23db1fe2a57e9b952ccb3bb54f43b0634 Mon Sep 17 00:00:00 2001 From: miguel Date: Mon, 16 Jan 2023 17:59:02 +0000 Subject: [PATCH] fix(append-output): fixing test readme and solution --- sh/tests/append-output/songs.txt | 55 --------------- sh/tests/append-output_test.sh | 93 ++++++++++++++++++++++--- sh/tests/solutions/append-output.sh | 3 +- subjects/devops/append-output/README.md | 13 ++-- 4 files changed, 87 insertions(+), 77 deletions(-) delete mode 100644 sh/tests/append-output/songs.txt diff --git a/sh/tests/append-output/songs.txt b/sh/tests/append-output/songs.txt deleted file mode 100644 index 25d3fceae..000000000 --- a/sh/tests/append-output/songs.txt +++ /dev/null @@ -1,55 +0,0 @@ -"Breathe" - Faith Hill -"It Wasn't Me" - Shaggy featuring Ricardo "RikRok" Ducent -"Hanging by a Moment" - Lifehouse -"Shape of My Heart" - Backstreet Boys -"Thank You" - Dido -"I'm Like a Bird" - Nelly Furtado -"Family Affair" - Mary J. Blige -"Fallin'" - Alicia Keys -"All for You" - Janet Jackson -"I Wanna Know" - Joe -"U Remind Me" - Usher -"U Got It Bad" - Usher -"I'm a Believer" - Smash Mouth -"Get the Party Started" - Pink -"Wherever You Will Go" - The Calling -"In the End" - Linkin Park -"Loser" - Beck -"Get Ur Freak On" - Missy Elliott -"I'm Real" - Jennifer Lopez -"Butterfly" - Crazy Town -"Crawling" - Linkin Park -"I'm a Slave 4 U" - Britney Spears -"Elevation" - U2 -"Lady Marmalade" - Christina Aguilera, Lil' Kim, Mya, Pink -"Play" - Jennifer Lopez -"I'm Just a Kid" - Simple Plan -"Imitation of Life" - R.E.M. -"Big Pimpin'" - Jay-Z -"Stutter" - Joe featuring Mystikal -"I Wish" - R. Kelly -"This Is the Night" - Clay Aiken -"Hella Good" - No Doubt -"I Know" - Dionne Farris -"I'll Be Missing You" - Puff Daddy and Faith Evans featuring 112 -"I Try" - Macy Gray -"Thong Song" - Sisqo -"Survivor" - Destiny's Child -"I Want It That Way" - Backstreet Boys -"Bad Day" - Daniel Powter -"I'm Like a Bird" - Nelly Furtado -"I Need to Know" - Marc Anthony -"Follow Me" - Uncle Kracker -"Hemorrhage (In My Hands)" - Fuel -"Soak Up the Sun" - Sheryl Crow -"I Hope You Dance" - Lee Ann Womack -"Can't Get You Out of My Head" - Kylie Minogue -"I Just Wanna Love U (Give It 2 Me)" - Jay-Z -"My Love Is Your Love" - Whitney Houston -"Bounce with Me" - Lil' Bow Wow -"Where the Party At" - Jagged Edge -"I'm Already There" - Lonestar -"I Don't Want to Miss a Thing" - Aerosmith -"If You Could Read My Mind" - Stars on 54 -"My Way" - Usher -"Always on Time" - Ja Rule featuring Ashanti \ No newline at end of file diff --git a/sh/tests/append-output_test.sh b/sh/tests/append-output_test.sh index c009c943d..b63a849ca 100755 --- a/sh/tests/append-output_test.sh +++ b/sh/tests/append-output_test.sh @@ -4,18 +4,89 @@ set -euo pipefail IFS=' ' +FILENAME="student/append-output.sh" +script_dirS=$(cd -P "$(dirname "$BASH_SOURCE")" &>/dev/null && pwd) -cd append-output -# reset the files. -if [ -f results.txt ]; then - rm results.txt - rm solution.txt +if test ! -e append-output; then + mkdir append-output + cat <append-output/songs.txt +"Breathe" - Faith Hill +"It Wasn't Me" - Shaggy featuring Ricardo "RikRok" Ducent +"Hanging by a Moment" - Lifehouse +"Shape of My Heart" - Backstreet Boys +"Thank You" - Dido +"I'm Like a Bird" - Nelly Furtado +"Family Affair" - Mary J. Blige +"Fallin'" - Alicia Keys +"All for You" - Janet Jackson +"I Wanna Know" - Joe +"U Remind Me" - Usher +"U Got It Bad" - Usher +"I'm a Believer" - Smash Mouth +"Get the Party Started" - Pink +"Wherever You Will Go" - The Calling +"In the End" - Linkin Park +"Loser" - Beck +"Get Ur Freak On" - Missy Elliott +"I'm Real" - Jennifer Lopez +"Butterfly" - Crazy Town +"Crawling" - Linkin Park +"I'm a Slave 4 U" - Britney Spears +"Elevation" - U2 +"Lady Marmalade" - Christina Aguilera, Lil' Kim, Mya, Pink +"Play" - Jennifer Lopez +"I'm Just a Kid" - Simple Plan +"Imitation of Life" - R.E.M. +"Big Pimpin'" - Jay-Z +"Stutter" - Joe featuring Mystikal +"I Wish" - R. Kelly +"This Is the Night" - Clay Aiken +"Hella Good" - No Doubt +"I Know" - Dionne Farris +"I'll Be Missing You" - Puff Daddy and Faith Evans featuring 112 +"I Try" - Macy Gray +"Thong Song" - Sisqo +"Survivor" - Destiny's Child +"I Want It That Way" - Backstreet Boys +"Bad Day" - Daniel Powter +"I'm Like a Bird" - Nelly Furtado +"I Need to Know" - Marc Anthony +"Follow Me" - Uncle Kracker +"Hemorrhage (In My Hands)" - Fuel +"Soak Up the Sun" - Sheryl Crow +"I Hope You Dance" - Lee Ann Womack +"Can't Get You Out of My Head" - Kylie Minogue +"I Just Wanna Love U (Give It 2 Me)" - Jay-Z +"My Love Is Your Love" - Whitney Houston +"Bounce with Me" - Lil' Bow Wow +"Where the Party At" - Jagged Edge +"I'm Already There" - Lonestar +"I Don't Want to Miss a Thing" - Aerosmith +"If You Could Read My Mind" - Stars on 54 +"My Way" - Usher +"Always on Time" - Ja Rule featuring Ashanti +EOF fi -# add the top two songs to the file as they are not asked. -cat songs.txt | grep "Linkin" >>results.txt - -submitted=$(bash ../student/append-output.sh) -expected=$(bash ../solutions/append-output.sh) +challenge() { + $(cd "$1" && bash "$script_dirS"/$FILENAME) + submitted=$(cat $1/results.txt) + rm $1"/results.txt" + $(cd "$1" && bash "$script_dirS"/solutions/append-output.sh) + expected=$(cat $1/results.txt) + diff <(echo "$submitted") <(echo "$expected") +} +if [ -s ${FILENAME} ]; then + if [[ $(cat $FILENAME | grep "cat <results.txt" | wc -l) -ne 0 ]]; then + echo "cheating is not allowed in this exercise!" + exit 1 + elif + [[ $(cat $FILENAME | grep echo | wc -l) -ne 0 ]] + then + echo "cheating is not allowed in this exercise!" + exit 1 + fi +fi -diff <(cat results.txt) <(cat solution.txt) +challenge append-output +rm -r append-output diff --git a/sh/tests/solutions/append-output.sh b/sh/tests/solutions/append-output.sh index d8e5f3552..ba4bb63ef 100755 --- a/sh/tests/solutions/append-output.sh +++ b/sh/tests/solutions/append-output.sh @@ -1,2 +1 @@ -cat songs.txt | grep "Linkin" >>solution.txt -cat songs.txt | grep "\- J" >>solution.txt +cat songs.txt | grep "\- J" >>results.txt diff --git a/subjects/devops/append-output/README.md b/subjects/devops/append-output/README.md index 62382678e..766ad7112 100644 --- a/subjects/devops/append-output/README.md +++ b/subjects/devops/append-output/README.md @@ -4,7 +4,7 @@ Create a file `append-output.sh` that will get the output of a file and parse it, and then write it to a file with a specific format using a single command. -Get the content of the `songs.txt`, parse it with the `grep` command to filter the file in order to get all the songs from the artist whose names start with `J`, and write the output to the existing file `results.txt`, check the example bellow: +Get the content of the `songs.txt`, parse it with the `grep` command to filter the file in order to get all the songs from the artist whose names start with `J`, and write the output to the existing file `results.txt`, check the example below: ### Usage @@ -37,11 +37,6 @@ Expected output: ```console $ ./append-output.sh $ cat result.txt -"In the End" - Linkin Park -"Crawling" - Linkin Park -"Elevation" - U2 -"Get the Party Started" - Pink -"Lady Marmalade" - Christina Aguilera, Lil' Kim, Mya, Pink "All for You" - Janet Jackson "I Wanna Know" - Joe "I'm Real" - Jennifer Lopez @@ -56,11 +51,11 @@ $ ### Hints -To add the output to a file with a specific format, you can use the >> operator to redirect the output of the command to a file, like this: +To add the output to a file with a specific format, you can use the `>>` operator to redirect the output of the command to a file, like this: -command1 | command2 >> output_file +`command1 | command2 >> output_file` -Here, command1 is the command that generates the output you want to parse, and command2 is the command that parses the output. The output of command2 will be redirected to the file output_file using the >> operator. If the file output_file already has some content inside, the file operator >> command will append to that file, unlike the > which will delete the content inside. +Here, `command1` is the command that generates the output you want to parse, and `command2` is the command that parses the output. The output of `command2` will be redirected to the file `output_file` using the `>>` operator. If the file `output_file` already has some content inside, the file operator `>>` command will append to that file, unlike the `>` which will delete the content inside. > You have to use Man or Google to know more about commands flags, in order to solve this exercise! > Google and Man will be your friends!