From b2c81baa393ea5bc19fb724d89e1a85c8a02e8b9 Mon Sep 17 00:00:00 2001 From: miguel Date: Wed, 21 Dec 2022 16:52:12 +0000 Subject: [PATCH] feat(set-env-vars): add subject, test and solution for the exercise --- sh/tests/set-env-vars_test.sh | 11 ++++ sh/tests/solutions/set-env-vars.sh | 7 +++ subjects/devops/env-format/README.md | 2 +- subjects/devops/set-env-vars/README.md | 70 ++++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 1 deletion(-) create mode 100755 sh/tests/set-env-vars_test.sh create mode 100755 sh/tests/solutions/set-env-vars.sh create mode 100644 subjects/devops/set-env-vars/README.md diff --git a/sh/tests/set-env-vars_test.sh b/sh/tests/set-env-vars_test.sh new file mode 100755 index 00000000..c1dc6294 --- /dev/null +++ b/sh/tests/set-env-vars_test.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +# Unofficial Bash Strict Mode +set -euo pipefail +IFS=' +' + +submitted=$(bash student/set-env-vars.sh) +expected=$(bash solutions/set-env-vars.sh) + +diff <(echo "$submitted") <(echo "$expected") diff --git a/sh/tests/solutions/set-env-vars.sh b/sh/tests/solutions/set-env-vars.sh new file mode 100755 index 00000000..712c43a7 --- /dev/null +++ b/sh/tests/solutions/set-env-vars.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +export MY_MESSAGE="Hello World" +export MY_NUM=100 +export MY_PI=3.142 +export MY_ARR=(one, two, three, four, five) +printenv diff --git a/subjects/devops/env-format/README.md b/subjects/devops/env-format/README.md index 23ed820c..4fb31ab8 100644 --- a/subjects/devops/env-format/README.md +++ b/subjects/devops/env-format/README.md @@ -8,7 +8,7 @@ Create a script `env-format.sh`, which will print the environment variables that - All the environment variables that have a letter `H` in the name, without printing the value of those variables. ```console -$ print env +$ printenv SHELL=/bin/bash QT_ACCESSIBILITY=1 NVM_RC_VERSION= diff --git a/subjects/devops/set-env-vars/README.md b/subjects/devops/set-env-vars/README.md new file mode 100644 index 00000000..5b2869f2 --- /dev/null +++ b/subjects/devops/set-env-vars/README.md @@ -0,0 +1,70 @@ +## set-env-vars + +### Instructions + +Create a script `set-env-vars.sh`, which will allow you to set the following variables as environment variables and and print them all: + +- `MY_MESSAGE` which contains the string `"Hello World"`. +- `MY_NUM` which contains the number `100`. +- `MY_PI` which contains the number `3.142`. + +Expected output: + +```console +$ printenv # The env variables present are just an example, yours will be different. +SHELL=/bin/bash +TERM=xterm +USER=demouser +LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca:... +MAIL=/var/mail/demouser +PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games +PWD=/home/demouser +LANG=en_US.UTF-8 +SHLVL=1 +HOME=/home/demouser +LOGNAME=demouser +LESSOPEN=| /usr/bin/lesspipe %s +LESSCLOSE=/usr/bin/lesspipe %s %s +_=/usr/bin/printenv +$ ./set-env-vars.sh +MY_NUM=100 +SHELL=/bin/bash +TERM=xterm +USER=demouser +LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca:... +MAIL=/var/mail/demouser +PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games +PWD=/home/demouser +LANG=en_US.UTF-8 +MY_PI=3.142 +SHLVL=1 +HOME=/home/demouser +LOGNAME=demouser +MY_MESSAGE=Hello World +LESSOPEN=| /usr/bin/lesspipe %s +LESSCLOSE=/usr/bin/lesspipe %s %s +_=/usr/bin/printenv +$ +``` + +### Hints + +Setting values to environment variables + +In order to set a value to an existing environment variable, we use an assignment expression. For instance, to set the value of the "LANG" variable to "he_IL.UTF-8", we use the following command: + +```console +$ LANG=he_IL.UTF-8 +``` + +If we use an assignment expression for a variable that doesn't exist, the shell will create a shell variable, which is similar to an environment variable but does not influence the behaviour of other applications. + +A shell variable can be exported to become an environment variable with the export command. To create the "EDITOR" environment variable and assign the value "nano" to it, you can do: + +```console +$ EDITOR=nano +$export EDITOR +``` + +> 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!