diff --git a/sh/tests/bin-status_test.sh b/sh/tests/bin-status_test.sh new file mode 100755 index 00000000..e22682d4 --- /dev/null +++ b/sh/tests/bin-status_test.sh @@ -0,0 +1,34 @@ +#!/usr/bin/env bash + +# Unofficial Bash Strict Mode +set -euo pipefail +IFS=' +' + +FILENAME="student/bin-status.sh" + + +challenge() { + submitted=$(eval "$1" ; bash $FILENAME) + expected=$(eval "$1" ; bash solutions/bin-status.sh) + + diff <(echo "$submitted") <(echo "$expected") +} + + +# True if FILE exists and is a regular file +if [ -f ${FILENAME} ]; then + # FILE exists and it's not empty + if [ -s ${FILENAME} ]; then + challenge true + challenge false + challenge ls -l + challenge ls asdasdasdasdad + else + echo "The file exist but is empty" + exit 1 + fi +else + echo "File does not exist" + exit 1 +fi \ No newline at end of file diff --git a/sh/tests/solutions/bin-status.sh b/sh/tests/solutions/bin-status.sh new file mode 100755 index 00000000..08d55fbc --- /dev/null +++ b/sh/tests/solutions/bin-status.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +echo $? \ No newline at end of file diff --git a/subjects/devops/bin-status/README.md b/subjects/devops/bin-status/README.md new file mode 100644 index 00000000..0a905cc1 --- /dev/null +++ b/subjects/devops/bin-status/README.md @@ -0,0 +1,33 @@ +## json-researcher + +### Instructions + +Create the script `bin-status.sh` that will return the exit status of last command + +- What to use : `echo` + +- The output should be exactly like the example bellow but with the expected name + +```console +$ true ; ./bin-status.sh +0 +$ false ; ./bin-status.sh +1 +$ +``` + +### Hints + +`$?` is a variable that holds the return value of the last executed command. +`echo $?` displays 0 if the last command has been successfully executed and displays a non-zero value if some error has occurred. +The bash sets `$?` To the exit status of the last executed process. By convention 0 is a successful exit and non-zero indicates some kind of error. It can be used to check if the previous command has been executed without any errors. If it has executed successfully then it stores 0. `$?` is also useful in shell scripts as a way to decide what to do depending on how the last executed command worked by checking the exit status. + +```console +$ random-binary ; echo $? +<...> + +$ +``` + +> 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! \ No newline at end of file