diff --git a/sh/tests/in-back-ground_test.sh b/sh/tests/in-back-ground_test.sh index 4f3cbf7f..1c971dde 100755 --- a/sh/tests/in-back-ground_test.sh +++ b/sh/tests/in-back-ground_test.sh @@ -7,20 +7,28 @@ IFS=' FILENAME="student/in-back-ground.sh" script_dirS=$(cd -P "$(dirname "$BASH_SOURCE")" &>/dev/null && pwd) + echo "- Australia is wider than the moon. The moon sits at 3400km in diameter, while Australia's diameter from east to west is almost 4000km." >facts challenge() { submitted=$(cd "$1" && bash "$script_dirS"/$FILENAME) + std_out=$(cat output.txt) expected=$(cd "$1" && bash "$script_dirS"/solutions/in-back-ground.sh) + sol_out=$(cat output.txt) diff <(echo "$submitted") <(echo "$expected") + diff <(echo "$std_out") <(echo "$sol_out") } +challenge_no_output() { + if [[ -f output.txt ]]; then + exit 1 + fi + submitted=$(cd "$1" && bash "$script_dirS"/$FILENAME) + expected=$(cd "$1" && bash "$script_dirS"/solutions/in-back-ground.sh) + diff <(echo "$submitted") <(echo "$expected") +} challenge . +rm output.txt echo "The sun is a star!" >facts -challenge . - -if [ -f sol-output ] && [ -f output ]; then - diff sol-output output - rm sol-output output -fi +challenge_no_output . diff --git a/sh/tests/solutions/in-back-ground.sh b/sh/tests/solutions/in-back-ground.sh index df8c1c5a..59252ede 100755 --- a/sh/tests/solutions/in-back-ground.sh +++ b/sh/tests/solutions/in-back-ground.sh @@ -1 +1 @@ -nohup cat facts | grep "moon" && echo "The moon fact was found!" >sol-output & +nohup cat facts | grep "moon" && echo "The moon fact was found!" >output.txt & diff --git a/subjects/devops/in-back-ground/README.md b/subjects/devops/in-back-ground/README.md index 9d13b204..9dadfb07 100644 --- a/subjects/devops/in-back-ground/README.md +++ b/subjects/devops/in-back-ground/README.md @@ -6,31 +6,39 @@ Create a script `in-back-ground.sh` which will execute a job in the background t - Run the command `cat` on the file "facts" which will read the contents of the file and print it to `stdout`. - The output of the cat command will be piped to the `grep` command, which will search for the string `"moon"` in the file. -- If the `grep` command succeeds (if it finds the string "moon"), the echo command will run and print `"The moon fact was found!"` to the `output` file. +- If the `grep` command succeeds (if it finds the string "moon"), you will print `"The moon fact was found!"` to the `output.txt` file. If it fails the file `output.txt` is not created. - If the command finds the string "moon", it will print the matching line to the `stdout`. -- Redirect the `stdout` to a new file `output`. +- Redirect the `stdout` to a new file `output.txt`. You must do all these steps running only one job and using the command `nohup`! Expected output: ```console +$ ls +facts in-back-ground.sh $ ./in-back-ground.sh nohup: redirecting stderr to stdout - Australia is wider than the moon. The moon sits at 3400km in diameter, while Australia's diameter from east to west is almost 4000km. $ ls -facts in-back-ground.sh output -$ cat output +facts in-back-ground.sh output.txt +$ cat output.txt The moon fact was found! -./in-back-ground.sh +$ +``` + +```console +$ ./in-back-ground.sh # If the string isn't found nohup: redirecting stderr to stdout $ ls facts in-back-ground.sh -$ cat output -cat: output: No such file or directory +$ cat output.txt +cat: output.txt: No such file or directory $ ``` +> In order to test your solution, you need to create your own `facts` file. This file must not be submitted! + ### Hints The `nohup` command is used to run a command in the background, even if you close the terminal or log out of the system. When a command is run with nohup, it ignores the "SIGHUP" signal, which is sent to processes when the terminal they are running in is closed.