diff --git a/bin/_gsh_protect b/bin/_gsh_protect index 21144240..7f9fb20c 100755 --- a/bin/_gsh_protect +++ b/bin/_gsh_protect @@ -3,5 +3,5 @@ chmod a-rw "$GSH_ROOT" chmod a-rw "$GSH_MISSIONS" chmod a-r "$GSH_CONFIG" -chmod a-r "$GSH_VAR" +chmod a-r "$GSH_TMP" chmod a-rw "$GSH_SBIN" diff --git a/bin/_gsh_systemconfig b/bin/_gsh_systemconfig index ebbc4f5b..095e15ae 100755 --- a/bin/_gsh_systemconfig +++ b/bin/_gsh_systemconfig @@ -1,8 +1,8 @@ -# #!/bin/sh +#!/bin/sh # display some info about the system echo "=========================" -echo "OSTYPE=$OSTYPE" +echo "bash' OSTYPE=$(bash -c 'echo $OSTYPE')" echo "=========================" echo "uname -a" uname -a diff --git a/bin/_gsh_unprotect b/bin/_gsh_unprotect index 2a6ffa44..6149158a 100755 --- a/bin/_gsh_unprotect +++ b/bin/_gsh_unprotect @@ -3,5 +3,5 @@ chmod "$(umask -S)" "$GSH_ROOT" chmod "$(umask -S)" "$GSH_MISSIONS" chmod "$(umask -S)" "$GSH_CONFIG" -chmod "$(umask -S)" "$GSH_VAR" +chmod "$(umask -S)" "$GSH_TMP" chmod "$(umask -S)" "$GSH_SBIN" diff --git a/bin/box.sh b/bin/box.sh index 2b505894..14cfc5dd 100755 --- a/bin/box.sh +++ b/bin/box.sh @@ -75,7 +75,7 @@ do ;; esac done -shift $(($OPTIND - 1)) +shift $((OPTIND - 1)) # get filename, or "-" filename=${1:--} @@ -143,7 +143,7 @@ else height=$(echo "$w_h" | cut -d' ' -f2) fi - $AWK -v box="$box" -v width=$width -v height=$height -f ./boxes-data.awk -f ./box.awk "$tmpfile" | decode + $AWK -v box="$box" -v width="$width" -v height="$height" -f ./boxes-data.awk -f ./box.awk "$tmpfile" | decode rm -f "$tmpfile" fi diff --git a/bin/missionname b/bin/missionname index 613dbb3a..4f32797b 100755 --- a/bin/missionname +++ b/bin/missionname @@ -22,7 +22,7 @@ missionname() { [ -f check.sh ] && break [ -f static.sh ] && break [ -f init.sh ] && break - [ $(pwd -P) = "/" ] && break + [ "$(pwd -P)" = "/" ] && break cd .. done diff --git a/bin/progress_bar b/bin/progress_bar index 59056206..f6610596 100755 --- a/bin/progress_bar +++ b/bin/progress_bar @@ -1,4 +1,6 @@ -#!/usr/bin/env bash +#!/bin/sh + +# display a small animation until the given PID stops . gettext.sh @@ -29,7 +31,7 @@ init_data() { case $design in "dots") # simple dots - STR=('.') + STR='.' PRE='' POST='\n' MSG='' @@ -38,7 +40,7 @@ init_data() { "bar") # simple dots - STR=('=') + STR='=' PRE='[' POST=']\n' MSG='' @@ -47,7 +49,10 @@ init_data() { "rotation") # rotating bar - STR=('\b|' '\b/' '\b-' '\b\\') + STR='\b| +\b/ +\b- +\b\' PRE='-' POST='\b \b' MSG="" @@ -56,8 +61,15 @@ init_data() { "bat") # flying bat - STR=('\b\b\b \,/' '\b\b\b \,/' '\b\b\b \,/' '\b\b\b \,/' - '\b\b\b /`\' '\b\b\b /`\' '\b\b\b /`\' '\b\b\b /`\') + STR='\b\b\b \,/ +\b\b\b \,/ +\b\b\b \,/ +\b\b\b \,/ +\b\b\b /`\ +\b\b\b /`\ +\b\b\b /`\ +\b\b\b /`\' + PRE=' ' POST='\b\b\b \r' MSG="$(gettext "While you are waiting, a bat flies by...")\\n" @@ -66,10 +78,10 @@ init_data() { "snake") # slithering snake - STR=('\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b __/\__/\__/\<:>' - '\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b _/\__/\__/\_<:>' - '\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b /\__/\__/\__<:>' - '\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b \__/\__/\__/<:>') + STR='\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b __/\__/\__/\<:> +\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b _/\__/\__/\_<:> +\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b /\__/\__/\__<:> +\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b \__/\__/\__/<:>' PRE='\__/\__/\__/<:>' POST='\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b \r' MSG="$(gettext "While you are waiting, a snake slithers by...")\\n" @@ -77,12 +89,12 @@ init_data() { ;; "caterpillar") - STR=('\b\b\b\b\b\b\b\b\b ___^___@' - '\b\b\b\b\b\b\b\b __/\__@' - '\b\b\b\b\b\b\b _/^\_@' - '\b\b\b\b\b\b __/\__@' - '\b\b\b\b\b\b\b ___^___@' - '\b\b\b\b\b\b\b\b\b ________@') + STR='\b\b\b\b\b\b\b\b\b ___^___@ +\b\b\b\b\b\b\b\b __/\__@ +\b\b\b\b\b\b\b _/^\_@ +\b\b\b\b\b\b __/\__@ +\b\b\b\b\b\b\b ___^___@ +\b\b\b\b\b\b\b\b\b ________@' PRE='_______@"' POST='\b\b\b\b\b\b\b\b\b \r' MSG="$(gettext "While you are waiting, a caterpillar crawls by...")\\n" @@ -90,10 +102,9 @@ init_data() { ;; "snail") - # Snail - STR=('\b\b\b\b\b\b\b_(@)__/"' - '\b\b\b\b\b\b\b\b__(@)_/"' - '\b\b\b\b\b\b\b\b _(@)_/"') + STR='\b\b\b\b\b\b\b_(@)__/" +\b\b\b\b\b\b\b\b__(@)_/" +\b\b\b\b\b\b\b\b _(@)_/"' PRE='_(O)_/"' POST='\b\b\b\b\b\b\b\b \r' MSG="$(gettext "While you are waiting, a snail goes by...")\\n" @@ -101,12 +112,11 @@ init_data() { ;; "spider") - STR=( - '\b\b\b\b\b //o\\\\' - '\b\b\b\b\b //o\\\\' - '\b\b\b\b\b///o\\' - '\b\b\b\b\b///o\\' - ) + # FIXME: not sure I understand the escaping here! + STR='\b\b\b\b\b //o\\\\\\\\ +\b\b\b\b\b //o\\\\\\\\ +\b\b\b\b\b///o\\\\ +\b\b\b\b\b///o\\\\' PRE='//o\\\\' POST='\b\b\b\b\b \r' MSG="$(gettext "While you are waiting, a spider crawls by...")\\n" @@ -114,11 +124,10 @@ init_data() { ;; "duck") - STR=( - '\b\b\b\b\\_@<' - '\b\b\b\b \\_@=' - '\b\b\b\b\\_@=' - '\b\b\b\b\\_@<') + STR='\b\b\b\b\\_@< +\b\b\b\b \\_@= +\b\b\b\b\\_@= +\b\b\b\b\\_@<' PRE='\\_@<' POST='\b\b\b\b\b \r' MSG="$(gettext "While you are waiting, a duck swims by...")\\n" @@ -127,12 +136,22 @@ init_data() { "fish-back-and-forth") # Swimming fish (small) going back and forth - STR=('\b\b\b ><>' '\b\b\b ><>' '\b\b\b ><>' '\b\b\b ><>' - '\b\b\b ><>' '\b\b\b ><>' '\b\b\b ><>' '\b\b\b ><>' - '\b\b\b \b\b\b\b<><' '\b\b\b \b\b\b\b<><' - '\b\b\b \b\b\b\b<><' '\b\b\b \b\b\b\b<><' - '\b\b\b \b\b\b\b<><' '\b\b\b \b\b\b\b<><' - '\b\b\b \b\b\b\b<><' '\b\b\b \b\b\b\b<><') + STR='\b\b\b ><> +\b\b\b ><> +\b\b\b ><> +\b\b\b ><> +\b\b\b ><> +\b\b\b ><> +\b\b\b ><> +\b\b\b ><> +\b\b\b \b\b\b\b<>< +\b\b\b \b\b\b\b<>< +\b\b\b \b\b\b\b<>< +\b\b\b \b\b\b\b<>< +\b\b\b \b\b\b\b<>< +\b\b\b \b\b\b\b<>< +\b\b\b \b\b\b\b<>< +\b\b\b \b\b\b\b<><' PRE='><>' POST='\b\b\b \r' MSG="$(gettext "While you are waiting, you see a fish swimming in circles...")\\n" @@ -141,8 +160,8 @@ init_data() { "centipede") # crawling centipede - STR=('\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b ,`,`,`,`,`,`,`(:)' - '\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b`,`,`,`,`,`,`,(:)') + STR='\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b ,`,`,`,`,`,`,`(:) +\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b`,`,`,`,`,`,`,(:)' PRE='`,`,`,`,`,`,`,(:)' POST='\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b \r' MSG="$(gettext "While you are waiting, a centipede crawls by...")\\n" @@ -151,7 +170,7 @@ init_data() { "ant") # crawling ant - STR=('\b\b\b\b\b >|<()') + STR='\b\b\b\b\b >|<()' PRE='>|<()' POST='\b\b\b\b\b \r' MSG="$(gettext "While you are waiting, an ant crawls by...")\\n" @@ -160,7 +179,7 @@ init_data() { "fish") # Swimming fish (small) - STR=('\b\b\b ><>') + STR='\b\b\b ><>' PRE='><>' POST='\b\b\b \r' MSG="$(gettext "While you are waiting, a fish swims by...")\\n" @@ -169,7 +188,7 @@ init_data() { "large-fish") # Swimming fish (large) - STR=("\b\b\b\b\b\b\b\b ><(((('>") + STR="\b\b\b\b\b\b\b\b ><(((('>" PRE="><(((('>" POST='\b\b\b\b\b\b\b\b \r' MSG="$(gettext "While you are waiting, a large fish swims by...")\\n" @@ -195,14 +214,17 @@ animation() { # Make progress for each character read on [stdin]. printf "$PRE" - while true + while kill -0 "$PID" 2>/dev/null + # NOTE: we need to test that PID is still active here as well, because + # the "break" in the body of the loop takes place in a subshell. It is + # not possible to return from the function from there (using "break 2" or + # "return")! do - for s in "${STR[@]}" + printf "$STR\n" | while read -r line do - printf "$s" - # Slow down the animation a little bit. + printf "$line" sleep $DELAY - kill -0 $PID 2>/dev/null || return + kill -0 "$PID" 2>/dev/null || break done done } @@ -230,14 +252,14 @@ EOS PID=$1 get_design "$design" - if [ "$design" == RANDOM ] + if [ "$design" = RANDOM ] then n=$(RANDOM) get_design "$((n%7))" # only modulo 7 to get the "interesting" animations fi init_data "$design" - if kill -0 $PID 2>/dev/null + if kill -0 "$PID" 2>/dev/null then animation printf "$POST" diff --git a/bin/realpath b/bin/realpath index e040bef6..b45a82cf 100755 --- a/bin/realpath +++ b/bin/realpath @@ -37,7 +37,7 @@ realpath() ( command cd -- "$dir" 2> /dev/null || { echo "realpath: $arg1: cannot access final dirname $dir" >&2; return 1; } dir=$(command pwd -P) [ "$dir" = "/" ] && dir="" - command printf "%s/%s\n" "$dir" "$(command basename -- "$target")" + command printf '%s/%s\n' "$dir" "$(command basename -- "$target")" return 0 fi done diff --git a/bin/save_environment.sh b/bin/save_environment.sh index e3887dee..a7a3e860 100644 --- a/bin/save_environment.sh +++ b/bin/save_environment.sh @@ -3,10 +3,30 @@ # aliases and processes { - compgen -v | sed 's/^/Variable:/' + ## get alias names + # for bash + # compgen -a | sed 's/^/Alias:/' + # or + alias | sed -e 's/^alias *//' -e 's/=.*//' -e 's/^/Alias:/' + + ## get variable names + # for bash + # compgen -v | sed 's/^/Variable:/' + # or, for both bash and zsh + [ -n "$BASH_VERSION" ] && set -o posix + set | sed -e 's/=.*//' -e 's/^/Variable:/' + [ -n "$BASH_VERSION" ] && set +o posix + + ## get function names + # for bash compgen -A function | sed 's/^/Function:/' - compgen -a | sed 's/^/Alias:/' + # for zsh + # print -l ${(ok)functions} + + ## get processes ps -o pid,comm | sed '1d;s/^/Process:/' + + ## get working directory pwd | sed 's/^/PWD:/' -} | grep -vE "grep|ps|sed|sort|bash" | sort +} | grep -vE "grep|ps|sed|sort|bash|zsh" | sort diff --git a/bin/sed-i b/bin/sed-i index 41eaea83..ae345d73 100755 --- a/bin/sed-i +++ b/bin/sed-i @@ -20,7 +20,7 @@ sed_i() { cp "$tmp" "$filename" rm -f "$tmp" "$bak" else - local ret=$? + ret=$? rm -f "$tmp" "$bak" return $ret fi diff --git a/bin/seq b/bin/seq index a2e18e90..4c40ced6 100755 --- a/bin/seq +++ b/bin/seq @@ -1,7 +1,7 @@ #!/bin/sh display_help() { - cat < 000001_cd_1 -... -... -generating '.mo' files -removing 'auto.sh' scripts -removing unnecessary (_*.sh, Makefile) files + -> copy basic/03_cd_HOME_throne + -> copy basic/04_mkdir_chest + ... +generating '.mo' files: ... +removing unnecessary files +remove tests setting admin password setting default GameShell mode creating archive @@ -54,18 +54,18 @@ creating self-extracting archive removing tgz archive removing temporary directory $ ls -... GameShell.sh ... +... gameshell.sh ... ``` -The `GameShell.sh` file contains an instance of GameShell that you can easily +The `gameshell.sh` file contains an instance of GameShell that you can easily copy to any other computer. To run it, you can either use ```sh -$ ./GameShell.sh +$ ./gameshell.sh ``` or ```sh -$ bash ./GameShell.sh +$ bash ./gameshell.sh ``` diff --git a/doc/mission.md b/doc/mission.md index 8495fcbc..f30c30ee 100644 --- a/doc/mission.md +++ b/doc/mission.md @@ -19,9 +19,9 @@ creating missions are the following: where the player is expected to move around. It is initially empty, and the missions populate it. Even though this is also the `HOME` directory of the player, using - `$GSH_HOME` is preferred over other abbreviations like `$HOME` or `~`. + `$GSH_HOME` is preferred over `$HOME` or `~`. -* `$GSH_VAR` contains all the "hidden" data that may be created by a mission: +* `$GSH_TMP` contains all the "hidden" data that may be created by a mission: temporary files, data required to check completion, data shared between missions, etc. Except in specific case ("dummy missions"), it is a good policy that each mission removes the files it creates. @@ -80,9 +80,67 @@ We will now describe in details what's expected in those 4 files. (The other files will be described in the next section.) +### Note on missions' `sh` files + +All the missions' `sh` files are **sourced**. This was decided in the early +versions of GameShell to make it possible to + - check the player defined an alias, + - change the current working directory of the player, + - define some environment variables. + +Most of the time, the `sh` scripts could be run in a subshell, which has the +advantage of making sure the environment is not polluted by leftovers from the +script. + +A good policy to adopt is that *except when you specifically need it*, the +script should execute in a subshell. The easiest way to do that is to define a +function +```sh +_mission_init() ( + ... +) +``` +Using parenthesis rather than braces ensures the function is run inside a +subshell. +Note that after sourcing a file `init.sh`, the function `_mission_init` is +automatically removed. The same is true for `check.sh` (where the function +`_mission_check` is unset) or any of the other `sh` files. + + +If not using a function, `unset`ing all the defined variables is encouraged. + +Note also that even if bash, zsh, or even dash have them, POSIX shell +functions don't have a concept of `local` variables. In other words, +```sh +_mission_init() { + dir=$GSH_ROOT/Castle + mkdir -p "$dir" +} +``` +defines a *global* variable `dir`! + +When running in verbose debug mode (flag `-D`), GameShell will print all +changes in the environment it detects. + + +In some cases, those files may be sourced from a subshell. This happens for +example if the previous mission was checked in a subshell, with something like +```sh +$ SOME_COMMAND | gsh check +``` +In such a case, `gsh check` happens in a subshell, and sourcing of the next +`init.sh` file happens in this same subshell. + +To prevent bugs when a mission specifically requires initialisation to change +the environment, GameShell tries to detect when a script is sourced in a +subshell. If it detects the environment has changed *and* that this happened +in a subshell, a warning is displayed asking the player to run `gsh reset`. + + + ### `static.sh` -Like all `.sh` files defining the mission, `static.sh` is **sourced** by +Like all `.sh` files defining the mission, `static.sh` is *sourced* by GameShell. It is first sourced when initializing a new game, and its typical use is creating the "world map". A mission taking place in the castle's cellar would @@ -109,8 +167,9 @@ use the directory `$MISSION_DIR` that points to the mission's directory. This is useful if the mission needs to copy files into GameShell's world. -_Note_ that because this file is sourced, it can change directory or define -environment variables. This is discouraged because of the following reasons. +_Note_ that because this file is sourced, it can change the working directory +or define environment variables. This is discouraged for the following +reasons. * Those files are not re-sourced when restarting a previous game. In that case, only the `static.sh` file corresponding to the current mission will be @@ -168,16 +227,6 @@ several versions of them by removing them first. (Or better yet, write a `clean.sh` script.) -_Note_ that because this file is sourced, it can change directory or define -environment variables. In some cases however, this file can be sourced in a -subshell. (For example, if the previous `gsh check` command was run in a -subshell, as in `COMMAND | gsh check`...) -Whenever GameShell detects this situation (subshell with creation of -environment variables or change of `CWD`), a message asking the player to run -`gsh reset` is displayed. This will re-source de the `init.sh` file, hopefully -not in a subshell this time. - - **If the last return value is `false` (anything different from `0`), the mission is cancelled.** @@ -207,11 +256,6 @@ _mission_check() { _mission_check ``` -Just like all the other scripts, `check.sh` is expected to keep the -environment clean: all local variables defined should be unset, etc. The only -exception is the function `_mission_check` that will be automatically unset. - - _Note_ that because this file is sourced, it can change directory or define environment variables. This is discouraged because sourcing might happen from a subshell (in case of `COMMAND | gsh check` for example). In that case, @@ -255,8 +299,8 @@ The other files are not used as often but allow to customize GameShell. ### `gshrc` (optional) -This file is added to the global configuration of the bash session used during -the game. It can be used to define variables, aliases, functions, etc.. +This file is added to the global configuration of the shell session used +during the game. It can be used to define variables, aliases, functions, etc.. They will be available throughout the game. @@ -274,17 +318,18 @@ should be no output. (See the `treasure-msg.txt` file.) ### `treasure-msg.txt` / `treasure-msg.sh` (optional) The file `treasure-msg.txt` is expected to be a UTF-8 encoded text file. It is -displayed when the mission is successfully completed. +displayed when the mission is successfully completed. If the terminal is wide +enough, a fancy treasure chest is added to the left of the message. -It can be used to inform the player that a treasure (i.e., a feature installed +This can be used to inform the player that a treasure (i.e., a feature installed by `treasure.sh`) has been won. Instead of `treasure-msg.txt`, a script `treasure-msg.sh` can be used to generate a dynamic message. If it exists, `treasure-msg.sh` is sourced when -the mission is successfully completed. - -A fancy treasure chest is added to the left of the message. The width of the -message shouldn't be more than 45 characters wide. +the mission is successfully completed. Note that if this file finished with a +non 0 return value, the `treasure.sh` file is *neither sourced nor installed*. +It can be used to avoid problems when some `treasure.sh` has some +dependencies. ### `bin/` (optional) @@ -321,7 +366,7 @@ archives by default. ### `test.sh` (optional) This file is sourced by the command `gsh test`, which is only available in -debug mode. The `test.sh` script usually uses a mix of standard bash commands +debug mode. The `test.sh` script usually uses a mix of standard shell commands and the special commands `gsh assert check true` (or `gsh assert check false`) which make testing easier. @@ -338,17 +383,11 @@ You can give a list of `index.txt` files and mission directories as arguments of GameShell if you want to customize the list / order of missions. This is particularly useful when testing a new mission: ```sh -$ ./start.sh -RD missions/contrib/my_new_mission +$ ./start.sh -Rdq missions/contrib/my_new_mission ``` -(The `-F` flag forces execution in the source directory, the `-R` flag resets -the previous game, and the flag `-D` will run GameShell in "debug" mode.) - - -Some available functions ------------------------- - -The file `$GSH_ROOT/lib/missions_utils.sh` defines a couple of useful -functions. +(The `-R` flag resets the previous game, the flag `-d` will run GameShell in +"debug" mode, and the `-q` flag removes the small messages at the start of +each mission.) Dummy missions @@ -363,7 +402,7 @@ contain a `static.sh` file. It can for example be used to share executable files in its `bin` or `sbin` directory. It can also contain data that will be used by other missions: -* either the `static.sh` file can copy the data to `$GSH_VAR` (or some other +* either the `static.sh` file can copy the data to `$GSH_TMP` (or some other place), * or the real missions can use symbolic links (with relative path) to refer to that data. diff --git a/doc/mission_ideas.md b/doc/mission_ideas.md index 0751a60d..6e323cdc 100644 --- a/doc/mission_ideas.md +++ b/doc/mission_ideas.md @@ -28,6 +28,9 @@ Some missions ideas - create empty files with `touch`, update modification date + - dealing with strange files: spaces (quoting / escaping), starting with a + dash (using `--`), etc. + - scripting: `for` loops on files - `pushd` / `popd` diff --git a/i18n/gameshell-HELP/en.txt b/i18n/gameshell-HELP/en.txt index 90c98580..6d0e0f40 100644 --- a/i18n/gameshell-HELP/en.txt +++ b/i18n/gameshell-HELP/en.txt @@ -30,7 +30,7 @@ gsh goto N (ADMIN) directly go to mission N gsh hardreset - reset the current mission by restarting the bash session + reset the current mission by restarting the shell gsh HELP this message diff --git a/i18n/gameshell-HELP/fr.txt b/i18n/gameshell-HELP/fr.txt index 389a9b17..b206616c 100644 --- a/i18n/gameshell-HELP/fr.txt +++ b/i18n/gameshell-HELP/fr.txt @@ -29,7 +29,7 @@ gsh goto N (ADMIN) passe directement à la mission N gsh hardreset - ré-initialise la mission en cours en redémarrant bash + ré-initialise la mission en cours en redémarrant le shell gsh HELP affiche ce message diff --git a/lib/bin_test.sh b/lib/bin_test.sh index 58254903..26ccbb16 100644 --- a/lib/bin_test.sh +++ b/lib/bin_test.sh @@ -76,7 +76,7 @@ test_sign() ( return 1 fi - echo $(RANDOM) > "$tmp" + echo "$(RANDOM)" > "$tmp" tmp2=$(mktemp) sign_file "$tmp" "$tmp2" if ! check_file "$tmp2" @@ -91,7 +91,7 @@ test_sign() ( return 1 fi - rm -f $tmp $tmp2 + rm -f "$tmp" "$tmp2" return 0 ) diff --git a/lib/gsh.sh b/lib/gsh.sh index 25873307..32f87c55 100644 --- a/lib/gsh.sh +++ b/lib/gsh.sh @@ -31,7 +31,7 @@ __log_action() { action=$2 D="$(date +%s)" S="$(checksum "$GSH_UID#$MISSION_NB#$action#$D")" - printf "%s %s %s %s\n" "$MISSION_NB" "$action" "$D" "$S" >> "$GSH_CONFIG/missions.log" + printf '%s %s %s %s\n' "$MISSION_NB" "$action" "$D" "$S" >> "$GSH_CONFIG/missions.log" } @@ -419,21 +419,21 @@ _gsh_assert_check() { mission_source "$MISSION_DIR/check.sh" local exit_status=$? - local nb_tests=$(cat "$GSH_VAR/nb_tests") + local nb_tests=$(cat "$GSH_TMP/nb_tests") nb_tests=$((nb_tests+1)) - echo "$nb_tests" > "$GSH_VAR/nb_tests" - local nb_failed_tests=$(cat "$GSH_VAR/nb_failed_tests") + echo "$nb_tests" > "$GSH_TMP/nb_tests" + local nb_failed_tests=$(cat "$GSH_TMP/nb_failed_tests") if [ "$expected" = "true" ] && [ "$exit_status" -ne 0 ] then nb_failed_tests=$((nb_failed_tests+1)) - echo "$nb_failed_tests" > "$GSH_VAR/nb_failed_tests" + echo "$nb_failed_tests" > "$GSH_TMP/nb_failed_tests" color_echo red "$(eval_gettext 'test $nb_tests failed') (expected check 'true')" [ -n "$msg" ] && echo "$msg" elif [ "$expected" = "false" ] && [ "$exit_status" -eq 0 ] then nb_failed_tests=$((nb_failed_tests+1)) - echo "$nb_failed_tests" > "$GSH_VAR/nb_failed_tests" + echo "$nb_failed_tests" > "$GSH_TMP/nb_failed_tests" color_echo red "$(eval_gettext 'test $nb_tests failed') (expected check 'false')" [ -n "$msg" ] && echo "$msg" fi @@ -454,12 +454,12 @@ _gsh_assert() { fi local msg=$2 - local nb_tests=$(cat "$GSH_VAR/nb_tests") - echo "$(( nb_tests + 1))" > "$GSH_VAR/nb_tests" + local nb_tests=$(cat "$GSH_TMP/nb_tests") + echo "$(( nb_tests + 1))" > "$GSH_TMP/nb_tests" if ! eval "$condition" then - echo "$(( nb_failed_tests + 1))" > "$GSH_VAR/nb_failed_tests" + echo "$(( nb_failed_tests + 1))" > "$GSH_TMP/nb_failed_tests" color_echo red "$(eval_gettext 'test $nb_tests failed') (expected condition 'true')" [ -n "$msg" ] && echo "$msg" fi @@ -484,13 +484,13 @@ _gsh_test() { return 2 fi - echo 0 > "$GSH_VAR/nb_tests" - echo 0 > "$GSH_VAR/nb_failed_tests" + echo 0 > "$GSH_TMP/nb_tests" + echo 0 > "$GSH_TMP/nb_failed_tests" mission_source "$MISSION_DIR/test.sh" local ret - local nb_tests=$(cat "$GSH_VAR/nb_tests") - local nb_failed_tests=$(cat "$GSH_VAR/nb_failed_tests") + local nb_tests=$(cat "$GSH_TMP/nb_tests") + local nb_failed_tests=$(cat "$GSH_TMP/nb_failed_tests") if [ "$nb_failed_tests" = 0 ] then @@ -504,7 +504,7 @@ _gsh_test() { echo ret=255 fi - rm -f "$GSH_VAR/nb_tests" "$GSH_VAR/nb_failed_tests" + rm -f "$GSH_TMP/nb_tests" "$GSH_TMP/nb_failed_tests" return "$ret" } @@ -593,7 +593,7 @@ gsh() { fi local MISSION_DIR="$(missiondir "$MISSION_NB")" - MISSION_NB=$MISSION_NB MISSION_DIR=$MISSION_DIR _gsh_$cmd "$@" + MISSION_NB=$MISSION_NB MISSION_DIR=$MISSION_DIR "_gsh_$cmd" "$@" ret=$? else echo "$(eval_gettext "Error: unknown gsh command '\$cmd'. diff --git a/lib/gshrc b/lib/gshrc index 3c2f8514..22956ea3 100644 --- a/lib/gshrc +++ b/lib/gshrc @@ -29,9 +29,6 @@ export PS2="> " export PS3="" export PS4="+ " -# running bash should take you "out" of GameShell -alias bash='bash --rcfile "$GSH_LIB/gshrc" -i' - # try to prevent using sudo and similar su() { echo "gsh: su: command not found" >&2 @@ -53,24 +50,26 @@ then fi -shopt -s nullglob -for file in "$GSH_CONFIG"/gshrc_*.sh -do - # shellcheck source=/dev/null - . "$file" -done -shopt -u nullglob +if [ -n "$(find "$GSH_CONFIG" -maxdepth 1 -name 'gshrc_*.sh' | head -n1)" ] +then + for file in "$GSH_CONFIG"/gshrc_*.sh + do + # shellcheck source=/dev/null + . "$file" + done +fi -shopt -s nullglob -for file in "$GSH_CONFIG"/treasure_*.sh -do - # shellcheck source=/dev/null - . "$file" -done -shopt -u nullglob +if [ -n "$(find "$GSH_CONFIG" -maxdepth 1 -name 'treasure_*.sh' | head -n1)" ] +then + for file in "$GSH_CONFIG"/treasure_*.sh + do + # shellcheck source=/dev/null + . "$file" + done +fi unset file MISSION_DIR -[ "$GSH_MODE" != "DEBUG" ] && ! [ -d "$GSH_ROOT/.git" ] && _gsh_protect +[ "$GSH_MODE" != "DEBUG" ] && ! [ -d "$GSH_ROOT/.git" ] && gsh protect __gsh_start diff --git a/lib/header.sh b/lib/header.sh index 015733d8..d029511e 100644 --- a/lib/header.sh +++ b/lib/header.sh @@ -41,7 +41,7 @@ N=0 while [ -e "$GSH_ROOT" ] do N=$((N+1)) - GSH_ROOT=$(echo "$GSH_ROOT" | sed "s/\.[0-9]*$//") + GSH_ROOT=$(echo "$GSH_ROOT" | sed 's/\.[0-9]*$//') GSH_ROOT="$GSH_ROOT.$N" done mkdir -p "$GSH_ROOT" diff --git a/lib/mission_source.sh b/lib/mission_source.sh index abf4ecb9..6e1a8407 100644 --- a/lib/mission_source.sh +++ b/lib/mission_source.sh @@ -52,8 +52,7 @@ mission_source() { fi echo " GSH: sourcing \$GSH_ROOT/${FILENAME#$GSH_ROOT/}" >&2 - local source_ret_value="" # otherwise, it appears in the environment! - local _MISSION_DIR="" + local _MISSION_DIR="" # otherwise, it appears in the environment! local _TEXTDOMAIN="" local _MISSION_NAME="" local MISSION_NAME="" diff --git a/lib/profile.sh b/lib/profile.sh index 0efe97cd..57c8b396 100644 --- a/lib/profile.sh +++ b/lib/profile.sh @@ -15,7 +15,7 @@ export GSH_MISSIONS="$GSH_ROOT/missions" # dynamic data export GSH_HOME="$GSH_ROOT/World" export GSH_CONFIG="$GSH_ROOT/.config" -export GSH_VAR="$GSH_ROOT/.var" +export GSH_TMP="$GSH_ROOT/.tmp" export GSH_BIN="$GSH_ROOT/.bin" export GSH_SBIN="$GSH_ROOT/.sbin" diff --git a/missions/FINAL_MISSION/check.sh b/missions/FINAL_MISSION/check.sh index 5b71ffdb..1c4efba6 100644 --- a/missions/FINAL_MISSION/check.sh +++ b/missions/FINAL_MISSION/check.sh @@ -1,3 +1,3 @@ -#!/bin/bash +#!/bin/sh false diff --git a/missions/FINAL_MISSION/goal.sh b/missions/FINAL_MISSION/goal.sh index bcbe8326..7bc29535 100644 --- a/missions/FINAL_MISSION/goal.sh +++ b/missions/FINAL_MISSION/goal.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh export password=$(gettext "qwerty") checksum "$password" > "$GSH_CONFIG/admin_hash" diff --git a/missions/FINAL_MISSION/init.sh b/missions/FINAL_MISSION/init.sh index deab6d95..ef4b7a39 100644 --- a/missions/FINAL_MISSION/init.sh +++ b/missions/FINAL_MISSION/init.sh @@ -1,3 +1,3 @@ -#!/bin/bash +#!/bin/sh parchment -B Twinkle "$(eval_gettext '$MISSION_DIR/msg/en.txt')" diff --git a/missions/basic/01_cd_tower/auto.sh b/missions/basic/01_cd_tower/auto.sh index 31d1e756..eef1ac73 100644 --- a/missions/basic/01_cd_tower/auto.sh +++ b/missions/basic/01_cd_tower/auto.sh @@ -1,2 +1,4 @@ +#!/bin/sh + cd "$(eval_gettext '$GSH_HOME/Castle/Main_tower/First_floor/Second_floor/Top_of_the_tower')" gsh check diff --git a/missions/basic/01_cd_tower/check.sh b/missions/basic/01_cd_tower/check.sh index 1f9d9a2b..7234f0fa 100644 --- a/missions/basic/01_cd_tower/check.sh +++ b/missions/basic/01_cd_tower/check.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh goal=$(realpath "$(eval_gettext "\$GSH_HOME/Castle/Main_tower/First_floor/Second_floor/Top_of_the_tower")") current=$(realpath "$PWD") diff --git a/missions/basic/01_cd_tower/init.sh b/missions/basic/01_cd_tower/init.sh index e3b6c9a8..f69634a7 100644 --- a/missions/basic/01_cd_tower/init.sh +++ b/missions/basic/01_cd_tower/init.sh @@ -1,3 +1,3 @@ -#!/bin/bash +#!/bin/sh cd "$GSH_HOME" diff --git a/missions/basic/01_cd_tower/static.sh b/missions/basic/01_cd_tower/static.sh index 680c364c..4994feb2 100644 --- a/missions/basic/01_cd_tower/static.sh +++ b/missions/basic/01_cd_tower/static.sh @@ -1 +1,2 @@ +#!/bin/sh mkdir -p "$(eval_gettext "\$GSH_HOME/Castle/Main_tower/First_floor/Second_floor/Top_of_the_tower")" diff --git a/missions/basic/01_cd_tower/test.sh b/missions/basic/01_cd_tower/test.sh index 97d37d9d..87a85fee 100644 --- a/missions/basic/01_cd_tower/test.sh +++ b/missions/basic/01_cd_tower/test.sh @@ -1,3 +1,5 @@ +#!/bin/sh + gsh assert_check false cd .. diff --git a/missions/basic/02_cd.._cellar/auto.sh b/missions/basic/02_cd.._cellar/auto.sh index 2a3b3aee..e3451966 100644 --- a/missions/basic/02_cd.._cellar/auto.sh +++ b/missions/basic/02_cd.._cellar/auto.sh @@ -1,2 +1,4 @@ +#!/bin/sh + cd "$(eval_gettext "\$GSH_HOME/Castle/Cellar")" gsh check diff --git a/missions/basic/02_cd.._cellar/check.sh b/missions/basic/02_cd.._cellar/check.sh index 866491ea..90a719a9 100644 --- a/missions/basic/02_cd.._cellar/check.sh +++ b/missions/basic/02_cd.._cellar/check.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh goal=$(realpath "$(eval_gettext "\$GSH_HOME/Castle/Cellar")") current=$(realpath "$PWD") diff --git a/missions/basic/02_cd.._cellar/static.sh b/missions/basic/02_cd.._cellar/static.sh index d8c59f6b..ec1ef27d 100644 --- a/missions/basic/02_cd.._cellar/static.sh +++ b/missions/basic/02_cd.._cellar/static.sh @@ -1,2 +1,4 @@ +#!/bin/sh + mkdir -p "$(eval_gettext '$GSH_HOME/Castle/Cellar')" sign_file "$MISSION_DIR/ascii-art/apple.txt" "$(eval_gettext '$GSH_HOME/Castle/Cellar')/$(gettext "barrel_of_apples")" diff --git a/missions/basic/02_cd.._cellar/test.sh b/missions/basic/02_cd.._cellar/test.sh index 202540d3..e8a2fa1e 100644 --- a/missions/basic/02_cd.._cellar/test.sh +++ b/missions/basic/02_cd.._cellar/test.sh @@ -1,3 +1,5 @@ +#!/bin/sh + cd gsh assert_check false diff --git a/missions/basic/03_cd_HOME_throne/auto.sh b/missions/basic/03_cd_HOME_throne/auto.sh index 4235416a..319be4b1 100644 --- a/missions/basic/03_cd_HOME_throne/auto.sh +++ b/missions/basic/03_cd_HOME_throne/auto.sh @@ -1,3 +1,6 @@ +#!/bin/bash +# ``history`` command is not in POSIX + cd "$(eval_gettext '$GSH_HOME/Castle/Main_building/Throne_room')" # commands from a sourced file aren't saved in the history, diff --git a/missions/basic/03_cd_HOME_throne/check.sh b/missions/basic/03_cd_HOME_throne/check.sh index da5de152..f0a3622f 100644 --- a/missions/basic/03_cd_HOME_throne/check.sh +++ b/missions/basic/03_cd_HOME_throne/check.sh @@ -1,14 +1,16 @@ -#!/bin/bash +#!/bin/sh + +# this should be POSIX compliant, but debian's sh (dash) doesn't have fc! _mission_check() { - local goal=$(realpath "$(eval_gettext "\$GSH_HOME/Castle/Main_building/Throne_room")") - local current=$(realpath "$PWD") + goal=$(realpath "$(eval_gettext "\$GSH_HOME/Castle/Main_building/Throne_room")") + current=$(realpath "$PWD") # TODO: for some unknown reason, redirecting the output of fc into another # command shifts the results: it then sees the "gsh check" command that # was used to run this function # Because of the previous remark, I need to look at the "-3" command. - local ppc=$(fc -nl -3 -3 | sed 's/^[[:blank:]]*//' | sed 's/[[:blank:]]*$//') + ppc=$(fc -nl -3 -3 | sed 's/^[[:blank:]]*//' | sed 's/[[:blank:]]*$//') # FIXME: also accept other commands to go back to the starting point? # FIXME: add an error message diff --git a/missions/basic/03_cd_HOME_throne/static.sh b/missions/basic/03_cd_HOME_throne/static.sh index 84b5059a..29c04fb4 100644 --- a/missions/basic/03_cd_HOME_throne/static.sh +++ b/missions/basic/03_cd_HOME_throne/static.sh @@ -1 +1,3 @@ +#!/bin/sh + mkdir -p "$(eval_gettext '$GSH_HOME/Castle/Main_building/Throne_room')" diff --git a/missions/basic/03_cd_HOME_throne/test.sh b/missions/basic/03_cd_HOME_throne/test.sh index 9d47a0d5..2ed8597b 100644 --- a/missions/basic/03_cd_HOME_throne/test.sh +++ b/missions/basic/03_cd_HOME_throne/test.sh @@ -1,3 +1,6 @@ +#!/bin/bash +# ``history`` command is not in POSIX + cd gsh assert check false diff --git a/missions/basic/03_cd_HOME_throne/treasure.sh b/missions/basic/03_cd_HOME_throne/treasure.sh index c32415cb..204e3389 100644 --- a/missions/basic/03_cd_HOME_throne/treasure.sh +++ b/missions/basic/03_cd_HOME_throne/treasure.sh @@ -1 +1,3 @@ -export PS1="\n\w\n[mission \$(gsh pcm)] $ " +#!/bin/sh + +export PS1='\n\w\n[mission $(gsh pcm)] $ ' diff --git a/missions/basic/04_mkdir_chest/auto.sh b/missions/basic/04_mkdir_chest/auto.sh index 3a15e1f0..e98155a6 100644 --- a/missions/basic/04_mkdir_chest/auto.sh +++ b/missions/basic/04_mkdir_chest/auto.sh @@ -1,2 +1,4 @@ +#!/bin/sh + mkdir -p "$(eval_gettext "\$GSH_HOME/Forest")/$(gettext "Hut")/$(gettext "Chest")" gsh check diff --git a/missions/basic/04_mkdir_chest/check.sh b/missions/basic/04_mkdir_chest/check.sh index 252fb09f..31651b89 100644 --- a/missions/basic/04_mkdir_chest/check.sh +++ b/missions/basic/04_mkdir_chest/check.sh @@ -1,11 +1,10 @@ -#!/bin/bash +#!/bin/sh -_mission_check() { - local forest="$(eval_gettext '$GSH_HOME/Forest')" - local HUT_dir_name=$(eval_gettext '$GSH_HOME/Forest')/$(gettext "Hut") +_mission_check() ( + forest="$(eval_gettext '$GSH_HOME/Forest')" + HUT=$(eval_gettext '$GSH_HOME/Forest')/$(gettext "Hut") # Check that there is only one hut. - local nb_huts nb_huts=$(find "$forest" -iname "$(gettext "Hut")" -type d | wc -l) if [ "$nb_huts" -ge 2 ] then @@ -19,15 +18,14 @@ _mission_check() { fi # Check the name of the hut. - if [ ! -d "$HUT_dir_name" ] + if [ ! -d "$HUT" ] then - local dir_name=.../${HUT_dir_name#$GSH_HOME/} + dir_name=.../${HUT#$GSH_HOME/} echo "$(eval_gettext 'The $dir_name directory does not exist!')" return 1 fi # Check that there is only one chest. - local nb_chests nb_chests=$(find "$forest" -iname "$(gettext "Chest")" -type d | wc -l) if [ "$nb_chests" -ge 2 ] then @@ -41,15 +39,14 @@ _mission_check() { fi # Check that the chest is at the root of the hut. - local chest if ! [ -d "$GSH_CHEST" ] then - local dir_name=.../${GSH_CHEST#$GSH_HOME/} + dir_name=.../${GSH_CHEST#$GSH_HOME/} echo "$(eval_gettext 'The $dir_name directory does not exist!')" return 1 fi return 0 -} +) _mission_check diff --git a/missions/basic/04_mkdir_chest/clean.sh b/missions/basic/04_mkdir_chest/clean.sh index 1e185051..25c3bece 100644 --- a/missions/basic/04_mkdir_chest/clean.sh +++ b/missions/basic/04_mkdir_chest/clean.sh @@ -1,3 +1,5 @@ +#!/bin/sh + case $GSH_LAST_ACTION in check_true) : diff --git a/missions/basic/04_mkdir_chest/static.sh b/missions/basic/04_mkdir_chest/static.sh index d3422ca5..a593c4f6 100644 --- a/missions/basic/04_mkdir_chest/static.sh +++ b/missions/basic/04_mkdir_chest/static.sh @@ -1 +1,3 @@ +#!/bin/sh + mkdir -p "$(eval_gettext "\$GSH_HOME/Forest")" diff --git a/missions/basic/04_mkdir_chest/test.sh b/missions/basic/04_mkdir_chest/test.sh index 45c7e06e..d571d374 100644 --- a/missions/basic/04_mkdir_chest/test.sh +++ b/missions/basic/04_mkdir_chest/test.sh @@ -1,3 +1,5 @@ +#!/bin/sh + find "$GSH_HOME" -iname "*$(gettext "Hut")*" -print0 | xargs -0 rm -rf find "$GSH_HOME" -iname "*$(gettext "Chest")*" -print0 | xargs -0 rm -rf diff --git a/missions/basic/05_rm_spiders_cellar/auto.sh b/missions/basic/05_rm_spiders_cellar/auto.sh index 7a35b358..d9ee2c88 100644 --- a/missions/basic/05_rm_spiders_cellar/auto.sh +++ b/missions/basic/05_rm_spiders_cellar/auto.sh @@ -1,2 +1,4 @@ +#!/bin/sh + rm "$(eval_gettext '$GSH_HOME/Castle/Cellar')/$(gettext "spider")"_? gsh check diff --git a/missions/basic/05_rm_spiders_cellar/check.sh b/missions/basic/05_rm_spiders_cellar/check.sh index b4c8b00f..f6625eab 100644 --- a/missions/basic/05_rm_spiders_cellar/check.sh +++ b/missions/basic/05_rm_spiders_cellar/check.sh @@ -1,11 +1,9 @@ -#!/bin/bash +#!/bin/sh -_mission_check() { - local cellar +_mission_check() ( cellar="$(eval_gettext '$GSH_HOME/Castle/Cellar')" # Check that there are no more spiders. - local spiders spiders=$(find "$cellar" -name "$(gettext "spider")_*") if [ -n "$spiders" ] then @@ -14,7 +12,6 @@ _mission_check() { fi # Check that the bats are still there. - local bats_nb bats_nb=$(find "$cellar" -name "$(gettext "bat")_*" | wc -l) if [ "$bats_nb" -ne 2 ] then @@ -23,6 +20,6 @@ _mission_check() { fi return 0 -} +) _mission_check diff --git a/missions/basic/05_rm_spiders_cellar/init.sh b/missions/basic/05_rm_spiders_cellar/init.sh index b6dd8262..1f957068 100644 --- a/missions/basic/05_rm_spiders_cellar/init.sh +++ b/missions/basic/05_rm_spiders_cellar/init.sh @@ -1,18 +1,17 @@ -#!/bin/bash +#!/bin/sh -_mission_init() { - local i +_mission_init() ( for i in $(seq 3) do - local spider=$(eval_gettext '$GSH_HOME/Castle/Cellar')/$(gettext "spider")_$i - sign_file "$MISSION_DIR/ascii-art/spider-$((RANDOM%3)).txt" "$spider" + spider=$(eval_gettext '$GSH_HOME/Castle/Cellar')/$(gettext "spider")_$i + sign_file "$MISSION_DIR/ascii-art/spider-$(($(RANDOM)%3)).txt" "$spider" done for i in $(seq 2) do - local bat=$(eval_gettext '$GSH_HOME/Castle/Cellar')/$(gettext "bat")_$i - sign_file "$MISSION_DIR/ascii-art/bat-$((RANDOM%3)).txt" "$bat" + bat=$(eval_gettext '$GSH_HOME/Castle/Cellar')/$(gettext "bat")_$i + sign_file "$MISSION_DIR/ascii-art/bat-$(($(RANDOM)%3)).txt" "$bat" done -} +) _mission_init diff --git a/missions/basic/05_rm_spiders_cellar/static.sh b/missions/basic/05_rm_spiders_cellar/static.sh index 7382a09f..89077f9c 100644 --- a/missions/basic/05_rm_spiders_cellar/static.sh +++ b/missions/basic/05_rm_spiders_cellar/static.sh @@ -1 +1,3 @@ +#!/bin/sh + mkdir -p "$(eval_gettext "\$GSH_HOME/Castle/Cellar")" diff --git a/missions/basic/05_rm_spiders_cellar/test.sh b/missions/basic/05_rm_spiders_cellar/test.sh index 02eea63e..4290a4d0 100644 --- a/missions/basic/05_rm_spiders_cellar/test.sh +++ b/missions/basic/05_rm_spiders_cellar/test.sh @@ -1,3 +1,5 @@ +#!/bin/sh + gsh assert check false rm "$(eval_gettext '$GSH_HOME/Castle/Cellar')"/* diff --git a/missions/basic/06_mv_coins_garden/auto.sh b/missions/basic/06_mv_coins_garden/auto.sh index 487e49f5..b5f63d30 100644 --- a/missions/basic/06_mv_coins_garden/auto.sh +++ b/missions/basic/06_mv_coins_garden/auto.sh @@ -1,2 +1,4 @@ +#!/bin/sh + mv "$(eval_gettext '$GSH_HOME/Garden')/$(gettext "coin")_"? "$GSH_CHEST" gsh check diff --git a/missions/basic/06_mv_coins_garden/check.sh b/missions/basic/06_mv_coins_garden/check.sh index 393fe3ef..e4cd49d5 100644 --- a/missions/basic/06_mv_coins_garden/check.sh +++ b/missions/basic/06_mv_coins_garden/check.sh @@ -1,39 +1,39 @@ -#!/bin/bash +#!/bin/sh # Receives coin number as first argument. -_mission_check() { - local coin_name="$(gettext "coin")_$1" +_mission_check() ( + coin_name="$(gettext "coin")_$1" - # Check that the coin is not in the garden. - if [ -f "$(eval_gettext '$GSH_HOME/Garden')/$coin_name" ] - then - echo "$(eval_gettext "The coin '\$coin_name' is still in the garden!")" - return 1 - fi + # Check that the coin is not in the garden. + if [ -f "$(eval_gettext '$GSH_HOME/Garden')/$coin_name" ] + then + echo "$(eval_gettext "The coin '\$coin_name' is still in the garden!")" + return 1 + fi - # Check that the coin is in the chest. - if [ ! -f "$GSH_CHEST/$coin_name" ] - then - echo "$(eval_gettext "The coin '\$coin_name' is not in the chest!")" - return 1 - fi + # Check that the coin is in the chest. + if [ ! -f "$GSH_CHEST/$coin_name" ] + then + echo "$(eval_gettext "The coin '\$coin_name' is not in the chest!")" + return 1 + fi - # Check that the contents of the coin. - if ! check_file "$GSH_CHEST/$coin_name" - then - echo "$(eval_gettext "The coin '\$coin_name' has been tampered with...")" - return 1 - fi + # Check that the contents of the coin. + if ! check_file "$GSH_CHEST/$coin_name" + then + echo "$(eval_gettext "The coin '\$coin_name' has been tampered with...")" + return 1 + fi - # check the coin. - if ! check_file "$GSH_CHEST/$coin_name" - then - echo "$(eval_gettext "The coin '\$coin_name' has been tampered with...")" - return 1 - fi + # check the coin. + if ! check_file "$GSH_CHEST/$coin_name" + then + echo "$(eval_gettext "The coin '\$coin_name' has been tampered with...")" + return 1 + fi - return 0 - } + return 0 +) if _mission_check 1 && _mission_check 2 && _mission_check 3 then diff --git a/missions/basic/06_mv_coins_garden/init.sh b/missions/basic/06_mv_coins_garden/init.sh index bd7701d8..6c606a16 100644 --- a/missions/basic/06_mv_coins_garden/init.sh +++ b/missions/basic/06_mv_coins_garden/init.sh @@ -1,18 +1,15 @@ -#!/bin/bash +#!/bin/sh -_mission_init() { - [ -z "$GSH_CHEST" ] && GSH_CHEST="$(eval_gettext '$GSH_HOME/Forest/Hut/Chest')" - mkdir -p "$GSH_CHEST" +[ -z "$GSH_CHEST" ] && GSH_CHEST="$(eval_gettext '$GSH_HOME/Forest/Hut/Chest')" +mkdir -p "$GSH_CHEST" - local D=$(date +%s) - - local i +_mission_init() ( for i in $(seq 3) do - local C="$(gettext "coin")_$i" + C="$(gettext "coin")_$i" touch "$(eval_gettext "\$GSH_HOME/Garden")/$C" sign_file "$(eval_gettext "\$GSH_HOME/Garden")/$C" done -} +) _mission_init diff --git a/missions/basic/06_mv_coins_garden/static.sh b/missions/basic/06_mv_coins_garden/static.sh index 53e406e5..07cdf514 100644 --- a/missions/basic/06_mv_coins_garden/static.sh +++ b/missions/basic/06_mv_coins_garden/static.sh @@ -1 +1,3 @@ +#!/bin/sh + mkdir -p "$(eval_gettext "\$GSH_HOME/Garden")" diff --git a/missions/basic/06_mv_coins_garden/test.sh b/missions/basic/06_mv_coins_garden/test.sh index c64da127..328211f6 100644 --- a/missions/basic/06_mv_coins_garden/test.sh +++ b/missions/basic/06_mv_coins_garden/test.sh @@ -1,7 +1,10 @@ +#!/bin/sh + mv "$(eval_gettext '$GSH_HOME/Garden')/$(gettext "coin")_"2 "$GSH_CHEST" gsh assert check false -mv "$(eval_gettext '$GSH_HOME/Garden')/$(gettext "coin")_"{1,3} "$GSH_CHEST" +mv "$(eval_gettext '$GSH_HOME/Garden')/$(gettext "coin")_1" \ + "$(eval_gettext '$GSH_HOME/Garden')/$(gettext "coin")_3" "$GSH_CHEST" gsh assert check false mv "$(eval_gettext '$GSH_HOME/Garden')/$(gettext "coin")_"? "$GSH_CHEST" diff --git a/missions/basic/07_mv_hidden_coins_garden/auto.sh b/missions/basic/07_mv_hidden_coins_garden/auto.sh index c5e5b564..9741ccf1 100644 --- a/missions/basic/07_mv_hidden_coins_garden/auto.sh +++ b/missions/basic/07_mv_hidden_coins_garden/auto.sh @@ -1,2 +1,4 @@ +#!/bin/sh + mv "$(eval_gettext '$GSH_HOME/Garden')/".*_"$(gettext "coin")"_* "$GSH_CHEST" gsh check diff --git a/missions/basic/07_mv_hidden_coins_garden/check.sh b/missions/basic/07_mv_hidden_coins_garden/check.sh index be234d08..5352b085 100644 --- a/missions/basic/07_mv_hidden_coins_garden/check.sh +++ b/missions/basic/07_mv_hidden_coins_garden/check.sh @@ -1,10 +1,10 @@ -#!/bin/bash +#!/bin/sh # Receives coin number as first argument. -_mission_check() { - local coin_name="$(gettext "coin")_$1" +_mission_check() ( + coin_name="$(gettext "coin")_$1" - local n=$(find "$(eval_gettext '$GSH_HOME/Garden')" -maxdepth 1 -name ".*_$coin_name" | wc -l) + n=$(find "$(eval_gettext '$GSH_HOME/Garden')" -maxdepth 1 -name ".*_$coin_name" | wc -l) if [ "$n" -gt 1 ] then echo "$(eval_gettext "There are several '\$coin_name...' in the garden!")" @@ -15,7 +15,7 @@ _mission_check() { return 1 fi - local n=$(find "$GSH_CHEST" -maxdepth 1 -name ".*_$coin_name" | wc -l) + n=$(find "$GSH_CHEST" -maxdepth 1 -name ".*_$coin_name" | wc -l) if [ "$n" -gt 1 ] then echo "$(eval_gettext "There are several '\$coin_name...' in your chest!")" @@ -26,7 +26,7 @@ _mission_check() { return 1 fi - local chest_coin=$(find "$GSH_CHEST" -maxdepth 1 -name ".*_$coin_name") + chest_coin=$(find "$GSH_CHEST" -maxdepth 1 -name ".*_$coin_name") # check the coin. if ! check_file "$chest_coin" then @@ -35,6 +35,6 @@ _mission_check() { fi return 0 -} +) _mission_check 1 && _mission_check 2 && _mission_check 3 diff --git a/missions/basic/07_mv_hidden_coins_garden/clean.sh b/missions/basic/07_mv_hidden_coins_garden/clean.sh index 65006962..ae3e27d4 100644 --- a/missions/basic/07_mv_hidden_coins_garden/clean.sh +++ b/missions/basic/07_mv_hidden_coins_garden/clean.sh @@ -1,3 +1,5 @@ +#!/bin/sh + case "$GSH_LAST_ACTION" in "check_true") ;; diff --git a/missions/basic/07_mv_hidden_coins_garden/init.sh b/missions/basic/07_mv_hidden_coins_garden/init.sh index e9d7da58..16ac33dc 100644 --- a/missions/basic/07_mv_hidden_coins_garden/init.sh +++ b/missions/basic/07_mv_hidden_coins_garden/init.sh @@ -1,20 +1,17 @@ -#!/bin/bash +#!/bin/sh -_mission_init() { - [ -z "$GSH_CHEST" ] && GSH_CHEST="$(eval_gettext '$GSH_HOME/Forest/Hut/Chest')" - mkdir -p "$GSH_CHEST" - - local D=$(date +%s) +[ -z "$GSH_CHEST" ] && GSH_CHEST="$(eval_gettext '$GSH_HOME/Forest/Hut/Chest')" +mkdir -p "$GSH_CHEST" +_mission_init() ( rm -f "$(eval_gettext '$GSH_HOME/Garden')/.$(gettext "coin")_"* - local i for i in $(seq 3) do - local f=$(eval_gettext '$GSH_HOME/Garden')/.${RANDOM}_$(gettext "coin")_$i + f=$(eval_gettext '$GSH_HOME/Garden')/.$(RANDOM)_$(gettext "coin")_$i touch "$f" sign_file "$f" done -} +) _mission_init diff --git a/missions/basic/07_mv_hidden_coins_garden/static.sh b/missions/basic/07_mv_hidden_coins_garden/static.sh index 53e406e5..07cdf514 100644 --- a/missions/basic/07_mv_hidden_coins_garden/static.sh +++ b/missions/basic/07_mv_hidden_coins_garden/static.sh @@ -1 +1,3 @@ +#!/bin/sh + mkdir -p "$(eval_gettext "\$GSH_HOME/Garden")" diff --git a/missions/basic/07_mv_hidden_coins_garden/test.sh b/missions/basic/07_mv_hidden_coins_garden/test.sh index d7a02151..96b0e690 100644 --- a/missions/basic/07_mv_hidden_coins_garden/test.sh +++ b/missions/basic/07_mv_hidden_coins_garden/test.sh @@ -1,7 +1,10 @@ +#!/bin/sh + mv "$(eval_gettext '$GSH_HOME/Garden')/".*_"$(gettext "coin")_2" "$GSH_CHEST" gsh assert check false -mv "$(eval_gettext '$GSH_HOME/Garden')/".*_"$(gettext "coin")_"{1,3} "$GSH_CHEST" +mv "$(eval_gettext '$GSH_HOME/Garden')/".*_"$(gettext "coin")_1" \ + "$(eval_gettext '$GSH_HOME/Garden')/".*_"$(gettext "coin")_3" "$GSH_CHEST" gsh assert check false cp "$(eval_gettext '$GSH_HOME/Garden')/".*_"$(gettext "coin")_1" "$(eval_gettext '$GSH_HOME/Garden')/.$(gettext "coin")_1_truc" diff --git a/missions/basic/08_rm_wildcard_spiders_cellar/auto.sh b/missions/basic/08_rm_wildcard_spiders_cellar/auto.sh index 294f23bd..be5e29f5 100644 --- a/missions/basic/08_rm_wildcard_spiders_cellar/auto.sh +++ b/missions/basic/08_rm_wildcard_spiders_cellar/auto.sh @@ -1,2 +1,4 @@ -rm -f "$(eval_gettext '$GSH_HOME/Castle/Cellar')/"*_$(gettext "spider")_* +#!/bin/sh + +rm -f "$(eval_gettext '$GSH_HOME/Castle/Cellar')/"*_"$(gettext "spider")"_* gsh check diff --git a/missions/basic/08_rm_wildcard_spiders_cellar/check.sh b/missions/basic/08_rm_wildcard_spiders_cellar/check.sh index f2504482..55dc318b 100644 --- a/missions/basic/08_rm_wildcard_spiders_cellar/check.sh +++ b/missions/basic/08_rm_wildcard_spiders_cellar/check.sh @@ -1,17 +1,17 @@ -#!/bin/bash +#!/bin/sh -_mission_check() { - local CELLAR="$(eval_gettext "\$GSH_HOME/Castle/Cellar")" +_mission_check() ( + CELLAR="$(eval_gettext "\$GSH_HOME/Castle/Cellar")" - local nb_spiders=$(find "$CELLAR" -maxdepth 1 -name "*_$(gettext "spider")_*" | wc -l | tr -d ' ') + nb_spiders=$(find "$CELLAR" -maxdepth 1 -name "*_$(gettext "spider")_*" | wc -l | tr -d ' ') if [ "$nb_spiders" -ne 0 ] then echo "$(eval_gettext "There still are some spiders (\$nb_spiders) in the cellar!")" return 1 fi - local S1=$(find "$CELLAR" -maxdepth 1 -name "*_$(gettext "bat")_*" | sort | checksum) - local S2=$(cat "$GSH_VAR/bats") + S1=$(find "$CELLAR" -maxdepth 1 -name "*_$(gettext "bat")_*" | sort | checksum) + S2=$(cat "$GSH_TMP/bats") if [ "$S1" != "$S2" ] then @@ -19,6 +19,6 @@ _mission_check() { return 1 fi return 0 -} +) _mission_check diff --git a/missions/basic/08_rm_wildcard_spiders_cellar/clean.sh b/missions/basic/08_rm_wildcard_spiders_cellar/clean.sh index 9f93571e..22376f29 100644 --- a/missions/basic/08_rm_wildcard_spiders_cellar/clean.sh +++ b/missions/basic/08_rm_wildcard_spiders_cellar/clean.sh @@ -1 +1,3 @@ -rm -f "$GSH_VAR/bats" +#!/bin/sh + +rm -f "$GSH_TMP/bats" diff --git a/missions/basic/08_rm_wildcard_spiders_cellar/init.sh b/missions/basic/08_rm_wildcard_spiders_cellar/init.sh index 9d3d5d18..d998eee6 100644 --- a/missions/basic/08_rm_wildcard_spiders_cellar/init.sh +++ b/missions/basic/08_rm_wildcard_spiders_cellar/init.sh @@ -1,6 +1,6 @@ -#!/bin/bash +#!/bin/sh -_mission_init() { +_mission_init() ( if ! [ -e "$MISSION_DIR/ascii-art" ] then DUMMY_MISSION=$(missionname "$MISSION_DIR/ascii-art") @@ -9,33 +9,29 @@ _mission_init() { return 1 fi - local CELLAR=$(eval_gettext "\$GSH_HOME/Castle/Cellar") + CELLAR=$(eval_gettext "\$GSH_HOME/Castle/Cellar") mkdir -p "$CELLAR" rm -f "$CELLAR"/*_"$(gettext "spider")"_* - local I - for I in $(seq 100) + RANDOM 100 | for I in $(seq 50) do - local spider=${CELLAR}/${RANDOM}_$(gettext "spider")_$I - sign_file "$MISSION_DIR/ascii-art/spider-$((RANDOM%3)).txt" "$spider" - if [ "$((I%5))" -eq 0 ] - then - printf "." - fi + read RANDOM + spider=${CELLAR}/${RANDOM}_$(gettext "spider")_$I + read RANDOM + cp "$MISSION_DIR/ascii-art/spider-$((RANDOM%3)).txt" "$spider" done - for I in $(seq 5) + RANDOM 10 | for I in $(seq 5) do - local bat=${CELLAR}/${RANDOM}_$(gettext "bat")_$I - sign_file "$MISSION_DIR/ascii-art/bat-$((RANDOM%3)).txt" "$bat" - if [ "$((I%5))" -eq 0 ] - then - printf "." - fi + + read RANDOM + bat=${CELLAR}/${RANDOM}_$(gettext "bat")_$I + read RANDOM + cp "$MISSION_DIR/ascii-art/bat-$((RANDOM%3)).txt" "$bat" done - find "$CELLAR" -maxdepth 1 -name "*_$(gettext "bat")_*" | sort | checksum > "$GSH_VAR/bats" + find "$CELLAR" -maxdepth 1 -name "*_$(gettext "bat")_*" | sort | checksum > "$GSH_TMP/bats" return 0 -} +) -. progress_bar.sh _mission_init +_mission_init diff --git a/missions/basic/08_rm_wildcard_spiders_cellar/static.sh b/missions/basic/08_rm_wildcard_spiders_cellar/static.sh index 7382a09f..89077f9c 100644 --- a/missions/basic/08_rm_wildcard_spiders_cellar/static.sh +++ b/missions/basic/08_rm_wildcard_spiders_cellar/static.sh @@ -1 +1,3 @@ +#!/bin/sh + mkdir -p "$(eval_gettext "\$GSH_HOME/Castle/Cellar")" diff --git a/missions/basic/08_rm_wildcard_spiders_cellar/test.sh b/missions/basic/08_rm_wildcard_spiders_cellar/test.sh index d0268b6c..8e14958d 100644 --- a/missions/basic/08_rm_wildcard_spiders_cellar/test.sh +++ b/missions/basic/08_rm_wildcard_spiders_cellar/test.sh @@ -1,3 +1,5 @@ +#!/bin/sh + gsh assert check false rm -f "$(eval_gettext '$GSH_HOME/Castle/Cellar')"/??* diff --git a/missions/basic/09_rm_wildcard_hidden_spiders_cellar/auto.sh b/missions/basic/09_rm_wildcard_hidden_spiders_cellar/auto.sh index 74113474..cd9387e8 100644 --- a/missions/basic/09_rm_wildcard_hidden_spiders_cellar/auto.sh +++ b/missions/basic/09_rm_wildcard_hidden_spiders_cellar/auto.sh @@ -1,2 +1,4 @@ -rm -f "$(eval_gettext '$GSH_HOME/Castle/Cellar')/".*_$(gettext "spider")_* +#!/bin/sh + +rm -f "$(eval_gettext '$GSH_HOME/Castle/Cellar')/".*_"$(gettext "spider")"_* gsh check diff --git a/missions/basic/09_rm_wildcard_hidden_spiders_cellar/check.sh b/missions/basic/09_rm_wildcard_hidden_spiders_cellar/check.sh index f2504482..b0dfbff1 100644 --- a/missions/basic/09_rm_wildcard_hidden_spiders_cellar/check.sh +++ b/missions/basic/09_rm_wildcard_hidden_spiders_cellar/check.sh @@ -1,24 +1,24 @@ -#!/bin/bash +#!/bin/sh -_mission_check() { - local CELLAR="$(eval_gettext "\$GSH_HOME/Castle/Cellar")" +_mission_check() ( + CELLAR="$(eval_gettext "\$GSH_HOME/Castle/Cellar")" - local nb_spiders=$(find "$CELLAR" -maxdepth 1 -name "*_$(gettext "spider")_*" | wc -l | tr -d ' ') - if [ "$nb_spiders" -ne 0 ] - then - echo "$(eval_gettext "There still are some spiders (\$nb_spiders) in the cellar!")" - return 1 - fi + nb_spiders=$(find "$CELLAR" -maxdepth 1 -name "*_$(gettext "spider")_*" | wc -l | tr -d ' ') + if [ "$nb_spiders" -ne 0 ] + then + echo "$(eval_gettext "There still are some spiders (\$nb_spiders) in the cellar!")" + return 1 + fi - local S1=$(find "$CELLAR" -maxdepth 1 -name "*_$(gettext "bat")_*" | sort | checksum) - local S2=$(cat "$GSH_VAR/bats") + S1=$(find "$CELLAR" -maxdepth 1 -name "*_$(gettext "bat")_*" | sort | checksum) + S2=$(cat "$GSH_TMP/bats") - if [ "$S1" != "$S2" ] - then - echo "$(eval_gettext "Some bats have been modified!")" - return 1 - fi - return 0 -} + if [ "$S1" != "$S2" ] + then + echo "$(eval_gettext "Some bats have been modified!")" + return 1 + fi + return 0 +) _mission_check diff --git a/missions/basic/09_rm_wildcard_hidden_spiders_cellar/clean.sh b/missions/basic/09_rm_wildcard_hidden_spiders_cellar/clean.sh index 2f1b5914..4a44d2aa 100644 --- a/missions/basic/09_rm_wildcard_hidden_spiders_cellar/clean.sh +++ b/missions/basic/09_rm_wildcard_hidden_spiders_cellar/clean.sh @@ -1,2 +1,4 @@ -rm -f "$GSH_VAR/bats" +#!/bin/sh + +rm -f "$GSH_TMP/bats" diff --git a/missions/basic/09_rm_wildcard_hidden_spiders_cellar/init.sh b/missions/basic/09_rm_wildcard_hidden_spiders_cellar/init.sh index 513a995d..055916ff 100644 --- a/missions/basic/09_rm_wildcard_hidden_spiders_cellar/init.sh +++ b/missions/basic/09_rm_wildcard_hidden_spiders_cellar/init.sh @@ -1,6 +1,6 @@ -#!/bin/bash +#!/bin/sh -_mission_init() { +_mission_init() ( if ! [ -e "$MISSION_DIR/ascii-art" ] then DUMMY_MISSION=$(missionname "$MISSION_DIR/ascii-art") @@ -9,34 +9,29 @@ _mission_init() { return 1 fi - local CELLAR=$(eval_gettext "\$GSH_HOME/Castle/Cellar") + CELLAR=$(eval_gettext "\$GSH_HOME/Castle/Cellar") mkdir -p "$CELLAR" rm -f "$CELLAR"/.??* - local I - for I in $(seq 100) + RANDOM 100 | for I in $(seq 50) do - local spider=${CELLAR}/.${RANDOM}_$(gettext "spider")_$I - sign_file "$MISSION_DIR/ascii-art/spider-$((RANDOM%3)).txt" "$spider" - if [ "$((I%5))" -eq 0 ] - then - printf "." - fi + read RANDOM + spider=${CELLAR}/.${RANDOM}_$(gettext "spider")_$I + read RANDOM + cp "$MISSION_DIR/ascii-art/spider-$((RANDOM%3)).txt" "$spider" done - for I in $(seq 10) + RANDOM 10 | for I in $(seq 5) do - local bat=${CELLAR}/.${RANDOM}_$(gettext "bat")_$I + read RANDOM + bat=${CELLAR}/.${RANDOM}_$(gettext "bat")_$I + read RANDOM sign_file "$MISSION_DIR/ascii-art/bat-$((RANDOM%3)).txt" "$bat" - if [ "$((I%5))" -eq 0 ] - then - printf "." - fi done - find "$CELLAR" -maxdepth 1 -name "*_$(gettext "bat")_*" | sort | checksum > "$GSH_VAR/bats" + find "$CELLAR" -maxdepth 1 -name "*_$(gettext "bat")_*" | sort | checksum > "$GSH_TMP/bats" return 0 -} +) -. progress_bar.sh _mission_init +_mission_init diff --git a/missions/basic/09_rm_wildcard_hidden_spiders_cellar/static.sh b/missions/basic/09_rm_wildcard_hidden_spiders_cellar/static.sh index 7382a09f..89077f9c 100644 --- a/missions/basic/09_rm_wildcard_hidden_spiders_cellar/static.sh +++ b/missions/basic/09_rm_wildcard_hidden_spiders_cellar/static.sh @@ -1 +1,3 @@ +#!/bin/sh + mkdir -p "$(eval_gettext "\$GSH_HOME/Castle/Cellar")" diff --git a/missions/basic/09_rm_wildcard_hidden_spiders_cellar/test.sh b/missions/basic/09_rm_wildcard_hidden_spiders_cellar/test.sh index e5fd3f5b..7bc34029 100644 --- a/missions/basic/09_rm_wildcard_hidden_spiders_cellar/test.sh +++ b/missions/basic/09_rm_wildcard_hidden_spiders_cellar/test.sh @@ -1,3 +1,5 @@ +#!/bin/sh + gsh assert check false rm -f "$(eval_gettext '$GSH_HOME/Castle/Cellar')"/.??* diff --git a/missions/basic/09_rm_wildcard_hidden_spiders_cellar/treasure.sh b/missions/basic/09_rm_wildcard_hidden_spiders_cellar/treasure.sh index 98ecfe91..49feeac6 100644 --- a/missions/basic/09_rm_wildcard_hidden_spiders_cellar/treasure.sh +++ b/missions/basic/09_rm_wildcard_hidden_spiders_cellar/treasure.sh @@ -1,5 +1,7 @@ +#!/bin/sh + # NOTE: --literal doesn't exist in freebsd -if ls --literal / &> /dev/null +if ls --literal / >/dev/null 2>/dev/null then alias ls='ls --literal -p' else diff --git a/missions/basic/10_cp_standard_great_hall/auto.sh b/missions/basic/10_cp_standard_great_hall/auto.sh index bc050ba3..df8b56ca 100644 --- a/missions/basic/10_cp_standard_great_hall/auto.sh +++ b/missions/basic/10_cp_standard_great_hall/auto.sh @@ -1,2 +1,4 @@ -cp "$(eval_gettext '$GSH_HOME/Castle/Great_hall')"/$(gettext "standard")_? "$GSH_CHEST" +#!/bin/sh + +cp "$(eval_gettext '$GSH_HOME/Castle/Great_hall')/$(gettext "standard")"_? "$GSH_CHEST" gsh check diff --git a/missions/basic/10_cp_standard_great_hall/check.sh b/missions/basic/10_cp_standard_great_hall/check.sh index d46d568d..0c64da61 100644 --- a/missions/basic/10_cp_standard_great_hall/check.sh +++ b/missions/basic/10_cp_standard_great_hall/check.sh @@ -1,13 +1,20 @@ -#!/bin/bash +#!/bin/sh -_mission_check() { - local GREAT_HALL="$(eval_gettext '$GSH_HOME/Castle/Great_hall')" +[ -z "$GSH_CHEST" ] && GSH_CHEST="$(eval_gettext '$GSH_HOME/Forest/Hut/Chest')" +mkdir -p "$GSH_CHEST" - if ! cmp -s "$GSH_VAR/great_hall_contents" <(command ls "$GREAT_HALL" | sort) +_mission_check() ( + GREAT_HALL="$(eval_gettext '$GSH_HOME/Castle/Great_hall')" + + temp=$(mktemp) + command ls "$GREAT_HALL" | sort >"$temp" + if ! cmp -s "$GSH_TMP/great_hall_contents" "$temp" then echo "$(gettext "You changed the contents of the great hall!")" + rm -f "$temp" return 1 fi + rm -f "$temp" if command ls "$GSH_CHEST" | grep -Eq "$(gettext "decorative_shield")|$(gettext "suit_of_armour")_$(gettext "stag_head")" then @@ -15,14 +22,18 @@ _mission_check() { return 1 fi - if ! cmp -s <(grep "$(gettext "standard")" "$GSH_VAR/great_hall_contents") \ - <(command ls "$GSH_CHEST" | sort | grep "$(gettext "standard")") + temp1=$(mktemp) + temp2=$(mktemp) + grep "$(gettext "standard")" "$GSH_TMP/great_hall_contents" >"$temp1" + command ls "$GSH_CHEST" | sort | grep "$(gettext "standard")" >"$temp2" + if ! cmp -s "$temp1" "$temp2" then + rm -f "$temp1" "$temp2" echo "$(gettext "I wanted all the standards!")" return 1 fi - + rm -f "$temp1" "$temp2" return 0 -} +) _mission_check diff --git a/missions/basic/10_cp_standard_great_hall/clean.sh b/missions/basic/10_cp_standard_great_hall/clean.sh index 838b0a69..cb77a6b4 100644 --- a/missions/basic/10_cp_standard_great_hall/clean.sh +++ b/missions/basic/10_cp_standard_great_hall/clean.sh @@ -1,16 +1,18 @@ +#!/bin/sh + mission_source "$MISSION_DIR/clean0.sh" -rm -f "$GSH_VAR/great_hall_contents" +rm -f "$GSH_TMP/great_hall_contents" case "$GSH_LAST_ACTION" in - check_true | skip) - : - ;; - *) - great_hall="$(eval_gettext '$GSH_HOME/Castle/Great_hall')" - rm -f "$great_hall/*$(gettext "standard")*" - rm -f "$GSH_CHEST"/*"$(gettext "standard")"* - unset great_hall - ;; + check_true | skip) + : + ;; + *) + great_hall="$(eval_gettext '$GSH_HOME/Castle/Great_hall')" + rm -f "$great_hall/*$(gettext "standard")*" + rm -f "$GSH_CHEST"/*"$(gettext "standard")"* + unset great_hall + ;; esac diff --git a/missions/basic/10_cp_standard_great_hall/clean0.sh b/missions/basic/10_cp_standard_great_hall/clean0.sh index 6bd133ae..fb7b653b 100644 --- a/missions/basic/10_cp_standard_great_hall/clean0.sh +++ b/missions/basic/10_cp_standard_great_hall/clean0.sh @@ -1,15 +1,17 @@ +#!/bin/sh + case "$GSH_LAST_ACTION" in - check_true | skip) - : - ;; - *) - great_hall=$(eval_gettext '$GSH_HOME/Castle/Great_hall') - rm -f "$great_hall"/*"$(gettext "stag_head")"* - rm -f "$great_hall"/*"$(gettext "decorative_shield")"* - rm -f "$great_hall"/*"$(gettext "suit_of_armour")"* - rm -f "$GSH_CHEST"/*"$(gettext "stag_head")"* - rm -f "$GSH_CHEST"/*"$(gettext "decorative_shield")"* - rm -f "$GSH_CHEST"/*"$(gettext "suit_of_armour")"* - unset great_hall - ;; + check_true | skip) + : + ;; + *) + great_hall=$(eval_gettext '$GSH_HOME/Castle/Great_hall') + rm -f "$great_hall"/*"$(gettext "stag_head")"* + rm -f "$great_hall"/*"$(gettext "decorative_shield")"* + rm -f "$great_hall"/*"$(gettext "suit_of_armour")"* + rm -f "$GSH_CHEST"/*"$(gettext "stag_head")"* + rm -f "$GSH_CHEST"/*"$(gettext "decorative_shield")"* + rm -f "$GSH_CHEST"/*"$(gettext "suit_of_armour")"* + unset great_hall + ;; esac diff --git a/missions/basic/10_cp_standard_great_hall/init.sh b/missions/basic/10_cp_standard_great_hall/init.sh index 0d188998..8e3bf3ee 100644 --- a/missions/basic/10_cp_standard_great_hall/init.sh +++ b/missions/basic/10_cp_standard_great_hall/init.sh @@ -1,24 +1,21 @@ -#!/bin/bash +#!/bin/sh -_mission_init() { - [ -z "$GSH_CHEST" ] && GSH_CHEST="$(eval_gettext '$GSH_HOME/Forest/Hut/Chest')" - mkdir -p "$GSH_CHEST" +[ -z "$GSH_CHEST" ] && GSH_CHEST="$(eval_gettext '$GSH_HOME/Forest/Hut/Chest')" +mkdir -p "$GSH_CHEST" +_mission_init() ( mission_source "$MISSION_DIR/init0.sh" - local great_hall="$(eval_gettext '$GSH_HOME/Castle/Great_hall')" + great_hall="$(eval_gettext '$GSH_HOME/Castle/Great_hall')" - local D=$(date +%s) - - local i for i in $(seq 4) do - local f="$(gettext "standard")_${i}" + f="$(gettext "standard")_${i}" touch "${great_hall}/${f}" sign_file "${great_hall}/${f}" done - ls "$great_hall" | sort > "$GSH_VAR/great_hall_contents" -} + command ls "$great_hall" | sort > "$GSH_TMP/great_hall_contents" +) _mission_init diff --git a/missions/basic/10_cp_standard_great_hall/init0.sh b/missions/basic/10_cp_standard_great_hall/init0.sh index c20fa6e7..acce2c81 100644 --- a/missions/basic/10_cp_standard_great_hall/init0.sh +++ b/missions/basic/10_cp_standard_great_hall/init0.sh @@ -1,14 +1,14 @@ -#!/bin/bash +#!/bin/sh -_mission_init0() { - local great_hall="$(eval_gettext '$GSH_HOME/Castle/Great_hall')" +_mission_init0() ( + great_hall="$(eval_gettext '$GSH_HOME/Castle/Great_hall')" rm -f "$great_hall/"*_"$(gettext "stag_head")" - sign_file "$MISSION_DIR/ascii-art/stag_head.txt" "$great_hall/${RANDOM}_$(gettext "stag_head")" + sign_file "$MISSION_DIR/ascii-art/stag_head.txt" "$great_hall/$(RANDOM)_$(gettext "stag_head")" rm -f "$great_hall/"*_"$(gettext "decorative_shield")" - sign_file "$MISSION_DIR/ascii-art/shield.txt" "$great_hall/${RANDOM}_$(gettext "decorative_shield")" + sign_file "$MISSION_DIR/ascii-art/shield.txt" "$great_hall/$(RANDOM)_$(gettext "decorative_shield")" rm -f "$great_hall/"*_"$(gettext "suit_of_armour")" - sign_file "$MISSION_DIR/ascii-art/armour.txt" "$great_hall/${RANDOM}_$(gettext "suit_of_armour")" -} + sign_file "$MISSION_DIR/ascii-art/armour.txt" "$great_hall/$(RANDOM)_$(gettext "suit_of_armour")" +) _mission_init0 unset -f _mission_init0 diff --git a/missions/basic/10_cp_standard_great_hall/static.sh b/missions/basic/10_cp_standard_great_hall/static.sh index 62ce2f8c..ec161c89 100644 --- a/missions/basic/10_cp_standard_great_hall/static.sh +++ b/missions/basic/10_cp_standard_great_hall/static.sh @@ -1 +1,3 @@ +#!/bin/sh + mkdir -p "$(eval_gettext "\$GSH_HOME/Castle/Great_hall")" diff --git a/missions/basic/10_cp_standard_great_hall/test.sh b/missions/basic/10_cp_standard_great_hall/test.sh index 8d0c2e90..2f44c1b2 100644 --- a/missions/basic/10_cp_standard_great_hall/test.sh +++ b/missions/basic/10_cp_standard_great_hall/test.sh @@ -1,8 +1,9 @@ +#!/bin/sh -cp "$(eval_gettext '$GSH_HOME/Castle/Great_hall')"/$(gettext "standard")_? "$GSH_CHEST" +cp "$(eval_gettext '$GSH_HOME/Castle/Great_hall')/$(gettext "standard")"_? "$GSH_CHEST" gsh assert check true -mv "$(eval_gettext '$GSH_HOME/Castle/Great_hall')"/$(gettext "standard")_? "$GSH_CHEST" +mv "$(eval_gettext '$GSH_HOME/Castle/Great_hall')/$(gettext "standard")"_? "$GSH_CHEST" gsh assert check false cp "$(eval_gettext '$GSH_HOME/Castle/Great_hall')"/* "$GSH_CHEST" @@ -10,7 +11,7 @@ gsh assert check false gsh assert check false -cp "$(eval_gettext '$GSH_HOME/Castle/Great_hall')"/$(gettext "standard")_? "$GSH_CHEST" +cp "$(eval_gettext '$GSH_HOME/Castle/Great_hall')/$(gettext "standard")"_? "$GSH_CHEST" rm "$(eval_gettext '$GSH_HOME/Castle/Great_hall')"/*"$(gettext "stag_head")"* gsh assert check false diff --git a/missions/basic/11_cp_wildcards_tapestries_great_hall/auto.sh b/missions/basic/11_cp_wildcards_tapestries_great_hall/auto.sh index 27bc726b..a705cbc4 100644 --- a/missions/basic/11_cp_wildcards_tapestries_great_hall/auto.sh +++ b/missions/basic/11_cp_wildcards_tapestries_great_hall/auto.sh @@ -1,2 +1,4 @@ -cp "$(eval_gettext '$GSH_HOME/Castle/Great_hall')"/*$(gettext "tapestry")* "$GSH_CHEST" +#!/bin/sh + +cp "$(eval_gettext '$GSH_HOME/Castle/Great_hall')"/*"$(gettext "tapestry")"* "$GSH_CHEST" gsh check diff --git a/missions/basic/11_cp_wildcards_tapestries_great_hall/check.sh b/missions/basic/11_cp_wildcards_tapestries_great_hall/check.sh index ee52727f..0167de94 100644 --- a/missions/basic/11_cp_wildcards_tapestries_great_hall/check.sh +++ b/missions/basic/11_cp_wildcards_tapestries_great_hall/check.sh @@ -1,13 +1,17 @@ -#!/bin/bash +#!/bin/sh -_mission_check () { - local GREAT_HALL="$(eval_gettext '$GSH_HOME/Castle/Great_hall')" +_mission_check () ( + GREAT_HALL="$(eval_gettext '$GSH_HOME/Castle/Great_hall')" - if ! cmp -s "$GSH_VAR/great_hall_contents" <(command ls "$GREAT_HALL" | sort) + temp=$(mktemp) + command ls "$GREAT_HALL" | sort >"$temp" + if ! cmp -s "$GSH_TMP/great_hall_contents" "$temp" then echo "$(gettext "You changed the contents of the great hall!")" + rm -f "$temp" return 1 fi + rm -f "$temp" if command ls "$GSH_CHEST" | grep -Eq "$(gettext "decorative_shield")|$(gettext "suit_of_armour")_$(gettext "stag_head")" then @@ -15,14 +19,18 @@ _mission_check () { return 1 fi - if ! cmp -s <(grep "$(gettext "tapestry")" "$GSH_VAR/great_hall_contents") \ - <(command ls "$GSH_CHEST" | sort | grep "$(gettext "tapestry")") + temp1=$(mktemp) + temp2=$(mktemp) + grep "$(gettext "tapestry")" "$GSH_TMP/great_hall_contents" >"$temp1" + command ls "$GSH_CHEST" | sort | grep "$(gettext "tapestry")" >"$temp2" + if ! cmp -s "$temp1" "$temp2" then echo "$(gettext "I wanted all the tapestries!")" + rm -f "$temp1" "$temp2" return 1 fi - + rm -f "$temp1" "$temp2" return 0 -} +) _mission_check diff --git a/missions/basic/11_cp_wildcards_tapestries_great_hall/clean.sh b/missions/basic/11_cp_wildcards_tapestries_great_hall/clean.sh index 428ddb0c..5c85bfae 100644 --- a/missions/basic/11_cp_wildcards_tapestries_great_hall/clean.sh +++ b/missions/basic/11_cp_wildcards_tapestries_great_hall/clean.sh @@ -1,16 +1,18 @@ +#!/bin/sh + mission_source "$MISSION_DIR"/clean0.sh -rm -f "$GSH_VAR/great_hall_contents" +rm -f "$GSH_TMP/great_hall_contents" case "$GSH_LAST_ACTION" in - check_true | skip) - : - ;; - *) - great_hall=$(eval_gettext '$GSH_HOME/Castle/Great_hall') - rm -f "$great_hall"/*"$(gettext "tapestry")"* - rm -f "$GSH_CHEST"/*"$(gettext "tapestry")"* - unset great_hall - ;; + check_true | skip) + : + ;; + *) + great_hall=$(eval_gettext '$GSH_HOME/Castle/Great_hall') + rm -f "$great_hall"/*"$(gettext "tapestry")"* + rm -f "$GSH_CHEST"/*"$(gettext "tapestry")"* + unset great_hall + ;; esac diff --git a/missions/basic/11_cp_wildcards_tapestries_great_hall/init.sh b/missions/basic/11_cp_wildcards_tapestries_great_hall/init.sh index 41ed7244..3adfc622 100644 --- a/missions/basic/11_cp_wildcards_tapestries_great_hall/init.sh +++ b/missions/basic/11_cp_wildcards_tapestries_great_hall/init.sh @@ -1,6 +1,9 @@ -#!/bin/bash +#!/bin/sh -_mission_init() { +[ -z "$GSH_CHEST" ] && GSH_CHEST="$(eval_gettext '$GSH_HOME/Forest/Hut/Chest')" +mkdir -p "$GSH_CHEST" + +_mission_init() ( if ! [ -e "$MISSION_DIR/init0.sh" ] then DUMMY_MISSION=$(missionname "$MISSION_DIR/init0.sh") @@ -9,24 +12,18 @@ _mission_init() { return 1 fi - [ -z "$GSH_CHEST" ] && GSH_CHEST="$(eval_gettext '$GSH_HOME/Forest/Hut/Chest')" - mkdir -p "$GSH_CHEST" - mission_source "$MISSION_DIR/init0.sh" - local D=$(date +%s) - - local great_hall="$(eval_gettext "\$GSH_HOME/Castle/Great_hall")" - local i + great_hall="$(eval_gettext "\$GSH_HOME/Castle/Great_hall")" for i in $(seq 10) do - local f=$great_hall/${RANDOM}_$(gettext "tapestry")_$(printf "%02d" "$i") - cp "$MISSION_DIR/ascii-art/tapestry-$((RANDOM%5)).txt" "$f" + f=$great_hall/$(RANDOM)_$(gettext "tapestry")_$(printf "%02d" "$i") + cp "$MISSION_DIR/ascii-art/tapestry-$(($(RANDOM)%5)).txt" "$f" sign_file "$f" done - ls "$great_hall" | sort > "$GSH_VAR/great_hall_contents" + ls "$great_hall" | sort > "$GSH_TMP/great_hall_contents" return 0 -} +) _mission_init diff --git a/missions/basic/11_cp_wildcards_tapestries_great_hall/static.sh b/missions/basic/11_cp_wildcards_tapestries_great_hall/static.sh index 62ce2f8c..ec161c89 100644 --- a/missions/basic/11_cp_wildcards_tapestries_great_hall/static.sh +++ b/missions/basic/11_cp_wildcards_tapestries_great_hall/static.sh @@ -1 +1,3 @@ +#!/bin/sh + mkdir -p "$(eval_gettext "\$GSH_HOME/Castle/Great_hall")" diff --git a/missions/basic/11_cp_wildcards_tapestries_great_hall/test.sh b/missions/basic/11_cp_wildcards_tapestries_great_hall/test.sh index 4e5cfff5..e3dade0d 100644 --- a/missions/basic/11_cp_wildcards_tapestries_great_hall/test.sh +++ b/missions/basic/11_cp_wildcards_tapestries_great_hall/test.sh @@ -1,3 +1,4 @@ +#!/bin/sh cp "$(eval_gettext '$GSH_HOME/Castle/Great_hall')"/*"$(gettext "tapestry")"* "$GSH_CHEST" gsh assert check true diff --git a/missions/basic/12_cp_ls_mtime_paintings_tower/auto.sh b/missions/basic/12_cp_ls_mtime_paintings_tower/auto.sh index aa5ff921..c7d87a8a 100644 --- a/missions/basic/12_cp_ls_mtime_paintings_tower/auto.sh +++ b/missions/basic/12_cp_ls_mtime_paintings_tower/auto.sh @@ -1,4 +1,6 @@ -filename=$(head -n1 "$GSH_VAR/painting") +#!/bin/sh + +filename=$(head -n1 "$GSH_TMP/painting") cp "$(eval_gettext '$GSH_HOME/Castle/Main_tower/First_floor')/$filename" "$GSH_CHEST" diff --git a/missions/basic/12_cp_ls_mtime_paintings_tower/check.sh b/missions/basic/12_cp_ls_mtime_paintings_tower/check.sh index d7aec1ad..349c1900 100644 --- a/missions/basic/12_cp_ls_mtime_paintings_tower/check.sh +++ b/missions/basic/12_cp_ls_mtime_paintings_tower/check.sh @@ -1,9 +1,9 @@ -#!/bin/bash +#!/bin/sh -_mission_check() { - local filename="$(head -n1 "$GSH_VAR/painting")" +_mission_check() ( + filename="$(head -n1 "$GSH_TMP/painting")" - local n=$(find "$GSH_CHEST" -name "$(gettext "painting")_*" | wc -l) + n=$(find "$GSH_CHEST" -name "$(gettext "painting")_*" | wc -l) if [ "$n" -eq 0 ] then echo "$(gettext "There is no painting in your chest...")" @@ -31,7 +31,7 @@ _mission_check() { echo "$(gettext "The painting in your chest is invalid...")" return 1 fi -} +) _mission_check diff --git a/missions/basic/12_cp_ls_mtime_paintings_tower/clean.sh b/missions/basic/12_cp_ls_mtime_paintings_tower/clean.sh index f7f09d73..976e1e24 100644 --- a/missions/basic/12_cp_ls_mtime_paintings_tower/clean.sh +++ b/missions/basic/12_cp_ls_mtime_paintings_tower/clean.sh @@ -1 +1,3 @@ -rm -f "$GSH_VAR/painting" +#!/bin/sh + +rm -f "$GSH_TMP/painting" diff --git a/missions/basic/12_cp_ls_mtime_paintings_tower/init.sh b/missions/basic/12_cp_ls_mtime_paintings_tower/init.sh index f813d05f..0223171b 100644 --- a/missions/basic/12_cp_ls_mtime_paintings_tower/init.sh +++ b/missions/basic/12_cp_ls_mtime_paintings_tower/init.sh @@ -1,39 +1,53 @@ -#!/bin/bash +#!/bin/sh -_mission_init() { - [ -z "$GSH_CHEST" ] && GSH_CHEST="$(eval_gettext '$GSH_HOME/Forest/Hut/Chest')" - mkdir -p "$GSH_CHEST" +[ -z "$GSH_CHEST" ] && GSH_CHEST="$(eval_gettext '$GSH_HOME/Forest/Hut/Chest')" +mkdir -p "$GSH_CHEST" +_mission_init() ( find "$GSH_HOME" -iname "$(gettext "painting")_*" -print0 | xargs -0 rm -rf - local filename=$(mktemp "$(eval_gettext '$GSH_HOME/Castle/Main_tower/First_floor')"/$(gettext "painting")_XXXXXX) + cd "$(eval_gettext '$GSH_HOME/Castle/Main_tower/First_floor')" + + while true + do + filename="$(gettext "painting")_$(random_string 8)" + [ -e "$filename" ] || break + done box.sh -B Diamond "$MISSION_DIR/ascii-art/painting-pipe" > "$filename" - local Y=$((1980 + RANDOM%10)) - local M=$(printf "%02d" $((1 + RANDOM%12))) - local D=$(printf "%02d" $((1 + RANDOM%28))) - local h=$(printf "%02d" $((RANDOM%24))) - local m=$(printf "%02d" $((RANDOM%60))) - local s=$(printf "%02d" $((RANDOM%60))) - echo "$(basename "$filename")" > "$GSH_VAR/painting" + Y=$((1980 + $(RANDOM)%10)) + M=$(printf "%02d" $((1 + $(RANDOM)%12))) + D=$(printf "%02d" $((1 + $(RANDOM)%28))) + h=$(printf "%02d" $(($(RANDOM)%24))) + m=$(printf "%02d" $(($(RANDOM)%60))) + s=$(printf "%02d" $(($(RANDOM)%60))) + echo "$(basename "$filename")" > "$GSH_TMP/painting" sign_file "$filename" touch -t "$Y$M$D$h$m.$s" "$filename" - filename=$(mktemp "$(eval_gettext '$GSH_HOME/Castle/Main_tower/First_floor')"/$(gettext "painting")_XXXXXX) + while true + do + filename="$(gettext "painting")_$(random_string 8)" + [ -e "$filename" ] || break + done box.sh -B Diamond "$MISSION_DIR/ascii-art/painting-star_wars" > "$filename" sign_file "$filename" touch "$filename" - filename=$(mktemp "$(eval_gettext '$GSH_HOME/Castle/Main_tower/First_floor')"/$(gettext "painting")_XXXXXX) + while true + do + filename="$(gettext "painting")_$(random_string 8)" + [ -e "$filename" ] || break + done box.sh -B Diamond "$MISSION_DIR/ascii-art/painting-joconde" > "$filename" Y=$(date +%Y) - Y=$((Y + 1 + RANDOM%10)) - M=$(printf "%02d" $((1 + RANDOM%12))) - D=$(printf "%02d" $((1 + RANDOM%28))) - h=$(printf "%02d" $((RANDOM%24))) - m=$(printf "%02d" $((RANDOM%60))) - s=$(printf "%02d" $((RANDOM%60))) + Y=$((Y + 1 + $(RANDOM)%10)) + M=$(printf "%02d" $((1 + $(RANDOM)%12))) + D=$(printf "%02d" $((1 + $(RANDOM)%28))) + h=$(printf "%02d" $(($(RANDOM)%24))) + m=$(printf "%02d" $(($(RANDOM)%60))) + s=$(printf "%02d" $(($(RANDOM)%60))) sign_file "$filename" touch -t "$Y$M$D$h$m.$s" "$filename" -} +) _mission_init diff --git a/missions/basic/12_cp_ls_mtime_paintings_tower/static.sh b/missions/basic/12_cp_ls_mtime_paintings_tower/static.sh index d4a74fea..b6210b2c 100644 --- a/missions/basic/12_cp_ls_mtime_paintings_tower/static.sh +++ b/missions/basic/12_cp_ls_mtime_paintings_tower/static.sh @@ -1 +1,3 @@ +#!/bin/sh + mkdir -p "$(eval_gettext "\$GSH_HOME/Castle/Main_tower/First_floor")" diff --git a/missions/basic/12_cp_ls_mtime_paintings_tower/test.sh b/missions/basic/12_cp_ls_mtime_paintings_tower/test.sh index 271af4da..4bc5c778 100644 --- a/missions/basic/12_cp_ls_mtime_paintings_tower/test.sh +++ b/missions/basic/12_cp_ls_mtime_paintings_tower/test.sh @@ -1,4 +1,6 @@ -cp "$(eval_gettext '$GSH_HOME/Castle/Main_tower/First_floor')/$(head -n1 "$GSH_VAR/painting")" "$GSH_CHEST" +#!/bin/sh + +cp "$(eval_gettext '$GSH_HOME/Castle/Main_tower/First_floor')/$(head -n1 "$GSH_TMP/painting")" "$GSH_CHEST" gsh assert check true gsh assert check false @@ -8,5 +10,5 @@ gsh assert check false gsh assert '[ "$(find "$GSH_HOME" -name "'$(gettext "painting")'_*" | wc -l)" -eq 3 ]' -cp "$(eval_gettext '$GSH_HOME/Castle/Main_tower/First_floor')/$(head -n1 "$GSH_VAR/painting")" "$GSH_CHEST" +cp "$(eval_gettext '$GSH_HOME/Castle/Main_tower/First_floor')/$(head -n1 "$GSH_TMP/painting")" "$GSH_CHEST" gsh assert check true diff --git a/missions/finding_files_maze/00_shared/clean.sh b/missions/finding_files_maze/00_shared/clean.sh index 736c4747..e5c09e77 100644 --- a/missions/finding_files_maze/00_shared/clean.sh +++ b/missions/finding_files_maze/00_shared/clean.sh @@ -1,9 +1,11 @@ +#!/bin/sh + maze="$(eval_gettext '$GSH_HOME/Garden/Maze')" case $PWD in - "$maze"/?*) - cd "$maze" - echo "$(gettext "You are back at the entrance of the maze...")" - ;; + "$maze"/?*) + cd "$maze" + echo "$(gettext "You are back at the entrance of the maze...")" + ;; esac case "$maze" in diff --git a/missions/finding_files_maze/00_shared/static.sh b/missions/finding_files_maze/00_shared/static.sh index df7182f6..35eca540 100644 --- a/missions/finding_files_maze/00_shared/static.sh +++ b/missions/finding_files_maze/00_shared/static.sh @@ -1,3 +1,5 @@ +#!/bin/sh + mkdir -p "$(eval_gettext '$GSH_HOME/Garden/Maze')" mkdir -p "$(eval_gettext '$GSH_HOME/Garden/Flower_garden')" mkdir -p "$(eval_gettext '$GSH_HOME/Garden/Shed')" diff --git a/missions/finding_files_maze/01_ls_cd/auto.sh b/missions/finding_files_maze/01_ls_cd/auto.sh index 4e1df484..358439cd 100644 --- a/missions/finding_files_maze/01_ls_cd/auto.sh +++ b/missions/finding_files_maze/01_ls_cd/auto.sh @@ -1,3 +1,7 @@ -# find "$(eval_gettext '$GSH_HOME/Garden/Maze')" -name "$(gettext "copper_coin")" -type f -print0 | xargs -0 mv -t "$GSH_CHEST" -find "$(eval_gettext '$GSH_HOME/Garden/Maze')" -name "*_$(gettext "copper_coin")_*" -type f -exec mv {} "$GSH_CHEST" \; +#!/bin/sh + +( + cd "$(eval_gettext '$GSH_HOME/Garden/Maze')" + find . -name "*_$(gettext "copper_coin")_*" -type f -exec mv "{}" "$GSH_CHEST" \; +) gsh check diff --git a/missions/finding_files_maze/01_ls_cd/check.sh b/missions/finding_files_maze/01_ls_cd/check.sh index de6bc605..88397d93 100644 --- a/missions/finding_files_maze/01_ls_cd/check.sh +++ b/missions/finding_files_maze/01_ls_cd/check.sh @@ -1,9 +1,9 @@ -#!/bin/bash +#!/bin/sh -_mission_check() { - local maze="$(eval_gettext '$GSH_HOME/Garden/Maze')" +_mission_check() ( + maze="$(eval_gettext '$GSH_HOME/Garden/Maze')" - local coin=$(find "$maze" -name "*_$(gettext "copper_coin")_*") + coin=$(find "$maze" -name "*_$(gettext "copper_coin")_*") if [ -n "$coin" ] then @@ -11,7 +11,6 @@ _mission_check() { return 1 fi - local coin coin=$(find "$GSH_CHEST" -maxdepth 1 -name "*_$(gettext "copper_coin")_*") if [ -z "$coin" ] then @@ -19,11 +18,11 @@ _mission_check() { return 1 fi - if ! cmp -s "$coin" "$GSH_VAR/copper_coin" + if ! cmp -s "$coin" "$GSH_TMP/copper_coin" then echo "$(gettext "Booh... The copper coin in you chest is invalid!")" return 1 fi -} +) _mission_check diff --git a/missions/finding_files_maze/01_ls_cd/clean.sh b/missions/finding_files_maze/01_ls_cd/clean.sh index d6f2dfc6..545dab25 100644 --- a/missions/finding_files_maze/01_ls_cd/clean.sh +++ b/missions/finding_files_maze/01_ls_cd/clean.sh @@ -1,2 +1,4 @@ -source "$MISSION_DIR/../00_shared/clean.sh" -rm -f "$GSH_VAR/copper_coin" +#!/bin/sh + +. "$MISSION_DIR/../00_shared/clean.sh" +rm -f "$GSH_TMP/copper_coin" diff --git a/missions/finding_files_maze/01_ls_cd/goal.sh b/missions/finding_files_maze/01_ls_cd/goal.sh index 72800703..a663606f 100644 --- a/missions/finding_files_maze/01_ls_cd/goal.sh +++ b/missions/finding_files_maze/01_ls_cd/goal.sh @@ -1,3 +1,5 @@ +#!/bin/sh + export maze="$(eval_gettext '$GSH_HOME/Garden/Maze')" envsubst '$maze' < "$(eval_gettext '$MISSION_DIR/goal/en.txt')" unset maze diff --git a/missions/finding_files_maze/01_ls_cd/init.sh b/missions/finding_files_maze/01_ls_cd/init.sh index e7830e9e..231899e4 100644 --- a/missions/finding_files_maze/01_ls_cd/init.sh +++ b/missions/finding_files_maze/01_ls_cd/init.sh @@ -1,25 +1,25 @@ -#!/bin/bash +#!/bin/sh -_mission_init() { - if ! command -v generate_maze.sh &> /dev/null +[ -z "$GSH_CHEST" ] && GSH_CHEST="$(eval_gettext '$GSH_HOME/Forest/Hut/Chest')" +mkdir -p "$GSH_CHEST" + +_mission_init() ( + if ! command -v generate_maze.sh >/dev/null then - local DUMMY_MISSION=$(realpath "$MISSION_DIR/../00_shared") + DUMMY_MISSION=$(realpath "$MISSION_DIR/../00_shared") DUMMY_MISSION=${DUMMY_MISSION#$GSH_MISSIONS/} echo "$(eval_gettext "Dummy mission '\$DUMMY_MISSION' is required for mission \$MISSION_NB (\$MISSION_NAME).")" >&2 return 1 fi - [ -z "$GSH_CHEST" ] && GSH_CHEST="$(eval_gettext '$GSH_HOME/Forest/Hut/Chest')" - mkdir -p "$GSH_CHEST" - - local maze="$(eval_gettext '$GSH_HOME/Garden/Maze')" + maze="$(eval_gettext '$GSH_HOME/Garden/Maze')" mkdir -p "$maze" - local d=$(generate_maze.sh "$maze" 3 2 1) + d=$(generate_maze.sh "$maze" 3 2 1) echo "$(checksum "$d")" > "$d/OOOOO_$(gettext "copper_coin")_OOOOO" - echo "$(checksum "$d")" > "$GSH_VAR/copper_coin" + echo "$(checksum "$d")" > "$GSH_TMP/copper_coin" return 0 -} +) _mission_init diff --git a/missions/finding_files_maze/01_ls_cd/test.sh b/missions/finding_files_maze/01_ls_cd/test.sh index 35972c57..05fec847 100644 --- a/missions/finding_files_maze/01_ls_cd/test.sh +++ b/missions/finding_files_maze/01_ls_cd/test.sh @@ -1,11 +1,25 @@ -gsh assert check false +#!/bin/sh -find "$(eval_gettext '$GSH_HOME/Garden/Maze')" -name "*_$(gettext "copper_coin")_*" -type f -print0 | xargs -0 rm -rf -gsh assert check false +( + gsh assert check false +) -find "$(eval_gettext '$GSH_HOME/Garden/Maze')" -name "*_$(gettext "copper_coin")_*" -type f -print0 | xargs -0 rm -rf -echo "coin" > "$(eval_gettext '$GSH_HOME/Garden/Maze')/copper_coin" -gsh assert check false +( + cd "$(eval_gettext '$GSH_HOME/Garden/Maze')" + find . -name "*_$(gettext "copper_coin")_*" -type f | xargs rm -rf + gsh assert check false +) + +( + cd "$(eval_gettext '$GSH_HOME/Garden/Maze')" + find . -name "*_$(gettext "copper_coin")_*" -type f | xargs rm -rf + echo "coin" > "$(eval_gettext '$GSH_HOME/Garden/Maze')/copper_coin" + gsh assert check false +) + +( + cd "$(eval_gettext '$GSH_HOME/Garden/Maze')" + find . -name "*_$(gettext "copper_coin")_*" -type f -exec mv "{}" "$GSH_CHEST" \; + gsh assert check true +) -find "$(eval_gettext '$GSH_HOME/Garden/Maze')" -name "*_$(gettext "copper_coin")_*" -type f -exec mv {} "$GSH_CHEST" \; -gsh assert check true diff --git a/missions/finding_files_maze/01_ls_cd/treasure-msg.sh b/missions/finding_files_maze/01_ls_cd/treasure-msg.sh index f3cff7c7..41cda008 100644 --- a/missions/finding_files_maze/01_ls_cd/treasure-msg.sh +++ b/missions/finding_files_maze/01_ls_cd/treasure-msg.sh @@ -1,3 +1,5 @@ +#!/bin/sh + if command ls --color >/dev/null 2>/dev/null then # standard linux diff --git a/missions/finding_files_maze/01_ls_cd/treasure.sh b/missions/finding_files_maze/01_ls_cd/treasure.sh index b890e2c7..34592df7 100644 --- a/missions/finding_files_maze/01_ls_cd/treasure.sh +++ b/missions/finding_files_maze/01_ls_cd/treasure.sh @@ -1,22 +1,24 @@ +#!/bin/sh + # NOTE: --literal doesn't exist in freebsd # --color doesn't exist in macos, it is replaced by -G # colors do not exist in openbsd! # -if ls --literal &> /dev/null +if ls --literal >/dev/null 2>/dev/null then - if ls --color=auto &> /dev/null + if ls --color=auto >/dev/null 2>/dev/null then alias ls='ls --literal -p --color=auto' - elif ls -G &> /dev/null + elif ls -G >/dev/null 2>/dev/null then alias ls='ls --literal -p -G' fi else - if ls --color=auto &> /dev/null + if ls --color=auto >/dev/null 2>/dev/null then export COLORTERM=1 # necessary for freeBSD alias ls='ls -p --color=auto' - elif ls -G / &> /dev/null + elif ls -G / >/dev/null 2>/dev/null then alias ls='ls -p -G' fi diff --git a/missions/finding_files_maze/02_tree/auto.sh b/missions/finding_files_maze/02_tree/auto.sh index 484728b9..e1fafb9b 100644 --- a/missions/finding_files_maze/02_tree/auto.sh +++ b/missions/finding_files_maze/02_tree/auto.sh @@ -1,3 +1,7 @@ -# find "$(eval_gettext '$GSH_HOME/Garden/Maze')" -name "*$(gettext "silver_coin")*" -type f -print0 | xargs -0 mv -t $GSH_CHEST -find "$(eval_gettext '$GSH_HOME/Garden/Maze')" -name "*$(gettext "silver_coin")*" -type f -exec mv "{}" "$GSH_CHEST" \; +#!/bin/sh + +( + cd "$(eval_gettext '$GSH_HOME/Garden/Maze')" + find . -name "*$(gettext "silver_coin")*" -type f -exec mv "{}" "$GSH_CHEST" \; +) gsh check diff --git a/missions/finding_files_maze/02_tree/check.sh b/missions/finding_files_maze/02_tree/check.sh index 049f6050..ef39252d 100644 --- a/missions/finding_files_maze/02_tree/check.sh +++ b/missions/finding_files_maze/02_tree/check.sh @@ -1,9 +1,9 @@ -#!/bin/bash +#!/bin/sh -_mission_check() { - local maze="$(eval_gettext '$GSH_HOME/Garden/Maze')" +_mission_check() ( + maze="$(eval_gettext '$GSH_HOME/Garden/Maze')" - local coin=$(find "$maze" -name "*$(gettext "silver_coin")*") + coin=$(find "$maze" -name "*$(gettext "silver_coin")*") if [ -n "$coin" ] then @@ -11,7 +11,6 @@ _mission_check() { return 1 fi - local coin coin=$(find "$GSH_CHEST" -maxdepth 1 -name "*$(gettext "silver_coin")*") if [ -z "$coin" ] then @@ -19,11 +18,11 @@ _mission_check() { return 1 fi - if ! cmp -s "$coin" "$GSH_VAR/silver_coin" + if ! cmp -s "$coin" "$GSH_TMP/silver_coin" then echo "$(gettext "The silver coin in you chest is invalid!")" return 1 fi -} +) _mission_check diff --git a/missions/finding_files_maze/02_tree/clean.sh b/missions/finding_files_maze/02_tree/clean.sh index ed41b14b..06ef7dfa 100644 --- a/missions/finding_files_maze/02_tree/clean.sh +++ b/missions/finding_files_maze/02_tree/clean.sh @@ -1,2 +1,4 @@ -source "$MISSION_DIR/../00_shared/clean.sh" -rm -f "$GSH_VAR/silver_coin" +#!/bin/sh + +. "$MISSION_DIR/../00_shared/clean.sh" +rm -f "$GSH_TMP/silver_coin" diff --git a/missions/finding_files_maze/02_tree/init.sh b/missions/finding_files_maze/02_tree/init.sh index 9be269d8..727dcf52 100644 --- a/missions/finding_files_maze/02_tree/init.sh +++ b/missions/finding_files_maze/02_tree/init.sh @@ -1,24 +1,26 @@ -#!/bin/bash +#!/bin/sh -_mission_init() { - if ! command -v generate_maze.sh &> /dev/null +[ -z "$GSH_CHEST" ] && GSH_CHEST="$(eval_gettext '$GSH_HOME/Forest/Hut/Chest')" +mkdir -p "$GSH_CHEST" + +_mission_init() ( + if ! command -v generate_maze.sh >/dev/null then - local DUMMY_MISSION=$(realpath "$MISSION_DIR/../00_shared") + DUMMY_MISSION=$(realpath "$MISSION_DIR/../00_shared") DUMMY_MISSION=${DUMMY_MISSION#$GSH_MISSIONS/} echo "$(eval_gettext "Dummy mission '\$DUMMY_MISSION' is required for mission \$MISSION_NB (\$MISSION_NAME).")" >&2 return 1 fi - [ -z "$GSH_CHEST" ] && GSH_CHEST="$(eval_gettext '$GSH_HOME/Forest/Hut/Chest')" - mkdir -p "$GSH_CHEST" - local maze="$(eval_gettext '$GSH_HOME/Garden/Maze')" + maze="$(eval_gettext '$GSH_HOME/Garden/Maze')" mkdir -p "$maze" - local d=$(generate_maze.sh "$maze" 3 3 1) + d=$(generate_maze.sh "$maze" 3 3 1) echo "$(checksum "$d")" > "$d/OOOOO_$(gettext "silver_coin")_OOOOO" - echo "$(checksum "$d")" > "$GSH_VAR/silver_coin" + echo "$(checksum "$d")" > "$GSH_TMP/silver_coin" return 0 -} +) + _mission_init diff --git a/missions/finding_files_maze/02_tree/test.sh b/missions/finding_files_maze/02_tree/test.sh index 509fea4e..87246c89 100644 --- a/missions/finding_files_maze/02_tree/test.sh +++ b/missions/finding_files_maze/02_tree/test.sh @@ -1,11 +1,17 @@ -gsh assert check false +#!/bin/sh -find "$(eval_gettext '$GSH_HOME/Garden/Maze')" -name "*$(gettext "silver_coin")*" -type f -print0 | xargs -0 rm -rf gsh assert check false -find "$(eval_gettext '$GSH_HOME/Garden/Maze')" -name "*$(gettext "silver_coin")*" -type f -print0 | xargs -0 rm -rf -echo "coin" > "$(eval_gettext '$GSH_HOME/Garden/Maze')/silver_coin" -gsh assert check false +( + cd "$(eval_gettext '$GSH_HOME/Garden/Maze')" + + find . -name "*$(gettext "silver_coin")*" -type f | xargs rm -rf + gsh assert check false + + find . -name "*$(gettext "silver_coin")*" -type f | xargs rm -rf + echo "coin" > "$(eval_gettext '$GSH_HOME/Garden/Maze')/silver_coin" + gsh assert check false -find "$(eval_gettext '$GSH_HOME/Garden/Maze')" -name "*$(gettext "silver_coin")*" -type f -exec mv "{}" "$GSH_CHEST" \; -gsh assert check true + find . -name "*$(gettext "silver_coin")*" -type f -exec mv "{}" "$GSH_CHEST" \; + gsh assert check true +) diff --git a/missions/finding_files_maze/03_find_1/auto.sh b/missions/finding_files_maze/03_find_1/auto.sh index 63e37977..ae7d8754 100644 --- a/missions/finding_files_maze/03_find_1/auto.sh +++ b/missions/finding_files_maze/03_find_1/auto.sh @@ -1,3 +1,7 @@ -# find "$(eval_gettext '$GSH_HOME/Garden/Maze')" -iname "*$(gettext "coin")*" -type f -print0 | xargs -0 mv -t $GSH_CHEST -find "$(eval_gettext '$GSH_HOME/Garden/Maze')" -iname "*$(gettext "coin")*" -type f -exec mv "{}" "$GSH_CHEST" \; +#!/bin/sh + +( + cd "$(eval_gettext '$GSH_HOME/Garden/Maze')" + find . -iname "*$(gettext "coin")*" -type f -exec mv "{}" "$GSH_CHEST" \; +) gsh check diff --git a/missions/finding_files_maze/03_find_1/check.sh b/missions/finding_files_maze/03_find_1/check.sh index 75242f4c..e44f134a 100644 --- a/missions/finding_files_maze/03_find_1/check.sh +++ b/missions/finding_files_maze/03_find_1/check.sh @@ -1,9 +1,8 @@ -#!/bin/bash +#!/bin/sh -_mission_check_p() { - local coin_name=$1 - local COIN_NB=$2 - local path +_mission_check_p() ( + coin_name=$1 + COIN_NB=$2 path=$(find "$GSH_CHEST" -name "*$(gettext "$coin_name")_$COIN_NB" -type f) if [ -z "$path" ] @@ -11,17 +10,17 @@ _mission_check_p() { echo "$(gettext "Some of the coins are not in your chest!")" return 1 fi - if ! cmp -s "$path" "$GSH_VAR/${coin_name}_$COIN_NB" + if ! cmp -s "$path" "$GSH_TMP/${coin_name}_$COIN_NB" then echo "$(eval_gettext "Coin '\$coin_name' in your chest is invalid!")" return 1 fi -} +) -_mission_check() { - local maze="$(eval_gettext '$GSH_HOME/Garden/Maze')" +_mission_check() ( + maze="$(eval_gettext '$GSH_HOME/Garden/Maze')" - local nb=$(find "$maze" -iname "$(gettext "gold_coin")" -type f | wc -l) + nb=$(find "$maze" -iname "$(gettext "gold_coin")" -type f | wc -l) if [ "$nb" -gt 2 ] then echo "$(gettext "There are too many gold coins in the maze!")" @@ -34,7 +33,7 @@ _mission_check() { fi _mission_check_p "gold_coin" 1 && _mission_check_p "GolD_CoiN" 2 -} +) if _mission_check then diff --git a/missions/finding_files_maze/03_find_1/clean.sh b/missions/finding_files_maze/03_find_1/clean.sh index a7eaaebc..2e708e44 100644 --- a/missions/finding_files_maze/03_find_1/clean.sh +++ b/missions/finding_files_maze/03_find_1/clean.sh @@ -1,2 +1,4 @@ -source "$MISSION_DIR/../00_shared/clean.sh" -rm -f "$GSH_VAR"/{gold_coin,GolD_CoiN}_* +#!/bin/sh + +. "$MISSION_DIR/../00_shared/clean.sh" +rm -f "$GSH_TMP"/gold_coin_* "$GSH_TMP"/GolD_CoiN_* diff --git a/missions/finding_files_maze/03_find_1/init.sh b/missions/finding_files_maze/03_find_1/init.sh index 1aabc93f..12759905 100644 --- a/missions/finding_files_maze/03_find_1/init.sh +++ b/missions/finding_files_maze/03_find_1/init.sh @@ -1,29 +1,30 @@ -#!/bin/bash +#!/bin/sh -_mission_init() { - if ! command -v generate_maze.sh &> /dev/null +[ -z "$GSH_CHEST" ] && GSH_CHEST="$(eval_gettext '$GSH_HOME/Forest/Hut/Chest')" +mkdir -p "$GSH_CHEST" + +_mission_init() ( + if ! command -v generate_maze.sh >/dev/null then - local DUMMY_MISSION=$(realpath "$MISSION_DIR/../00_shared") + DUMMY_MISSION=$(realpath "$MISSION_DIR/../00_shared") DUMMY_MISSION=${DUMMY_MISSION#$GSH_MISSIONS/} echo "$(eval_gettext "Dummy mission '\$DUMMY_MISSION' is required for mission \$MISSION_NB (\$MISSION_NAME).")" >&2 return 1 fi - [ -z "$GSH_CHEST" ] && GSH_CHEST="$(eval_gettext '$GSH_HOME/Forest/Hut/Chest')" - mkdir -p "$GSH_CHEST" - - local maze="$(eval_gettext '$GSH_HOME/Garden/Maze')" + maze="$(eval_gettext '$GSH_HOME/Garden/Maze')" mkdir -p "$maze" - local dir=$(generate_maze.sh "$maze" 3 10 2) + dir=$(generate_maze.sh "$maze" 3 10 2) - local dir1=$(echo "$dir" | sed -n '1p;1q') - local dir2=$(echo "$dir" | sed -n '2p;2q') + dir1=$(echo "$dir" | sed -n '1p;1q') + dir2=$(echo "$dir" | sed -n '2p;2q') checksum "$dir1" > "$dir1/$(gettext "gold_coin")_1" - checksum "$dir1" > "$GSH_VAR/gold_coin_1" + checksum "$dir1" > "$GSH_TMP/gold_coin_1" checksum "$dir2" > "$dir2/$(gettext "GolD_CoiN")_2" - checksum "$dir2" > "$GSH_VAR/GolD_CoiN_2" + checksum "$dir2" > "$GSH_TMP/GolD_CoiN_2" return 0 -} +) + _mission_init diff --git a/missions/finding_files_maze/03_find_1/test.sh b/missions/finding_files_maze/03_find_1/test.sh index bbc4754a..6456a00e 100644 --- a/missions/finding_files_maze/03_find_1/test.sh +++ b/missions/finding_files_maze/03_find_1/test.sh @@ -1,11 +1,17 @@ -gsh assert check false +#!/bin/sh -find "$(eval_gettext '$GSH_HOME/Garden/Maze')" -iname "*$(gettext "gold_coin")*" -type f -print0 | xargs -0 rm -rf gsh assert check false -find "$(eval_gettext '$GSH_HOME/Garden/Maze')" -iname "*$(gettext "gold_coin")*" -type f -print0 | xargs -0 rm -rf -echo "coin" > "$(eval_gettext '$GSH_HOME/Garden/Maze')/gold_coin" -gsh assert check false +( + cd "$(eval_gettext '$GSH_HOME/Garden/Maze')" + + find . -iname "*$(gettext "gold_coin")*" -type f | xargs rm -rf + gsh assert check false + + find . -iname "*$(gettext "gold_coin")*" -type f | xargs rm -rf + echo "coin" > "$(eval_gettext '$GSH_HOME/Garden/Maze')/gold_coin" + gsh assert check false -find "$(eval_gettext '$GSH_HOME/Garden/Maze')" -iname "*$(gettext "gold_coin")*" -type f -exec mv "{}" "$GSH_CHEST" \; -gsh assert check true + find . -iname "*$(gettext "gold_coin")*" -type f -exec mv "{}" "$GSH_CHEST" \; + gsh assert check true +) diff --git a/missions/finding_files_maze/04_find_2/auto.sh b/missions/finding_files_maze/04_find_2/auto.sh index bd7a4094..375b5c5a 100644 --- a/missions/finding_files_maze/04_find_2/auto.sh +++ b/missions/finding_files_maze/04_find_2/auto.sh @@ -1,3 +1,7 @@ -# find "$(eval_gettext '$GSH_HOME/Garden/Maze')" -type f -print0 | xargs -0 grep -Zl "$(gettext "ruby")" | xargs -0 mv -t "$GSH_CHEST" -find "$(eval_gettext '$GSH_HOME/Garden/Maze')" -type f -print0 | xargs -0 grep -l "$(gettext "ruby")" | xargs -I"{}" mv "{}" "$GSH_CHEST" +#!/bin/sh + +( + cd "$(eval_gettext '$GSH_HOME/Garden/Maze')" + find . -type f | xargs grep -l "$(gettext "ruby")" | xargs -I"{}" mv "{}" "$GSH_CHEST" +) gsh check diff --git a/missions/finding_files_maze/04_find_2/check.sh b/missions/finding_files_maze/04_find_2/check.sh index 8a427242..7720115b 100644 --- a/missions/finding_files_maze/04_find_2/check.sh +++ b/missions/finding_files_maze/04_find_2/check.sh @@ -1,9 +1,9 @@ -#!/bin/bash +#!/bin/sh -_mission_check() { - local maze="$(eval_gettext '$GSH_HOME/Garden/Maze')" +_mission_check() ( + maze="$(eval_gettext '$GSH_HOME/Garden/Maze')" - local nb=$(find "$maze" -type f -print0 | xargs -0 grep -l "$(gettext "ruby")" | wc -l) + nb=$(find "$maze" -type f -print0 | xargs -0 grep -l "$(gettext "ruby")" | wc -l) if [ "$nb" -gt 1 ] then @@ -16,19 +16,18 @@ _mission_check() { return 1 fi - local filename=$(cut -d" " -f1 "$GSH_VAR/ruby") + filename=$(cut -d" " -f1 "$GSH_TMP/ruby") if ! [ -f "$GSH_CHEST/$filename" ] then echo "$(gettext "The ruby is not in the chest!")" return 1 - elif ! cmp -s "$GSH_VAR/ruby" "$GSH_CHEST/$filename" + elif ! cmp -s "$GSH_TMP/ruby" "$GSH_CHEST/$filename" then echo "$(gettext "The ruby in your chest is not valid!")" return 1 fi return 0 -} - +) _mission_check diff --git a/missions/finding_files_maze/04_find_2/clean.sh b/missions/finding_files_maze/04_find_2/clean.sh index e59f28ec..c2214f84 100644 --- a/missions/finding_files_maze/04_find_2/clean.sh +++ b/missions/finding_files_maze/04_find_2/clean.sh @@ -1,2 +1,4 @@ -source "$MISSION_DIR/../00_shared/clean.sh" -rm -f "$GSH_VAR/ruby" +#!/bin/sh + +. "$MISSION_DIR/../00_shared/clean.sh" +rm -f "$GSH_TMP/ruby" diff --git a/missions/finding_files_maze/04_find_2/init.sh b/missions/finding_files_maze/04_find_2/init.sh index aff9e597..7118721e 100644 --- a/missions/finding_files_maze/04_find_2/init.sh +++ b/missions/finding_files_maze/04_find_2/init.sh @@ -1,35 +1,36 @@ -#!/bin/bash +#!/bin/sh -_mission_init() { - if ! command -v generate_maze.sh &> /dev/null +[ -z "$GSH_CHEST" ] && GSH_CHEST="$(eval_gettext '$GSH_HOME/Forest/Hut/Chest')" +mkdir -p "$GSH_CHEST" + +_mission_init() ( + if ! command -v generate_maze.sh >/dev/null then - local DUMMY_MISSION=$(realpath "$MISSION_DIR/../00_shared") + DUMMY_MISSION=$(realpath "$MISSION_DIR/../00_shared") DUMMY_MISSION=${DUMMY_MISSION#$GSH_MISSIONS/} echo "$(eval_gettext "Dummy mission '\$DUMMY_MISSION' is required for mission \$MISSION_NB (\$MISSION_NAME).")" >&2 return 1 fi - [ -z "$GSH_CHEST" ] && GSH_CHEST="$(eval_gettext '$GSH_HOME/Forest/Hut/Chest')" - mkdir -p "$GSH_CHEST" - - local maze="$(eval_gettext '$GSH_HOME/Garden/Maze')" + maze="$(eval_gettext '$GSH_HOME/Garden/Maze')" mkdir -p "$maze" - local dir=$(generate_maze.sh "$maze" 3 10 6) + dir=$(generate_maze.sh "$maze" 3 10 6) - local dir1=$(echo "$dir" | head -n 1) - local R=$RANDOM - local sum=$(checksum "$R $(gettext "ruby")") + dir1=$(echo "$dir" | head -n 1) + R=$(RANDOM) + sum=$(checksum "$R $(gettext "ruby")") echo "$R $(gettext "ruby") $sum" > "$dir1/$R" - echo "$R $(gettext "ruby") $sum" > "$GSH_VAR/ruby" + echo "$R $(gettext "ruby") $sum" > "$GSH_TMP/ruby" echo "$dir" | sed '1d' | while read dir1 do - R=$RANDOM + R=$(RANDOM) sum=$(checksum "$R $(gettext "stone")") echo "$R $(gettext "stone") $sum" > "$dir1/$R" done return 0 -} +) + _mission_init diff --git a/missions/finding_files_maze/04_find_2/test.sh b/missions/finding_files_maze/04_find_2/test.sh index 72a62eeb..32dbf6fa 100644 --- a/missions/finding_files_maze/04_find_2/test.sh +++ b/missions/finding_files_maze/04_find_2/test.sh @@ -1,3 +1,5 @@ +#!/bin/sh + gsh assert check false ( diff --git a/missions/finding_files_maze/05_find_xargs_grep/auto.sh b/missions/finding_files_maze/05_find_xargs_grep/auto.sh index 2e37d7e1..5157d418 100644 --- a/missions/finding_files_maze/05_find_xargs_grep/auto.sh +++ b/missions/finding_files_maze/05_find_xargs_grep/auto.sh @@ -1,3 +1,7 @@ -# find "$(eval_gettext '$GSH_HOME/Garden/Maze')" -type f -print0 | xargs -0 grep -Zl "$(gettext "diamond")" | xargs -0 mv -t "$GSH_CHEST" -find "$(eval_gettext '$GSH_HOME/Garden/Maze')" -type f -print0 | xargs -0 grep -l "$(gettext "diamond")" | xargs -I"{}" mv "{}" "$GSH_CHEST" +#!/bin/sh + +( + cd "$(eval_gettext '$GSH_HOME/Garden/Maze')" + find . -type f | xargs grep -l "$(gettext "diamond")" | xargs -I"{}" mv "{}" "$GSH_CHEST" +) gsh check diff --git a/missions/finding_files_maze/05_find_xargs_grep/check.sh b/missions/finding_files_maze/05_find_xargs_grep/check.sh index 6600e5ad..915d7fb6 100644 --- a/missions/finding_files_maze/05_find_xargs_grep/check.sh +++ b/missions/finding_files_maze/05_find_xargs_grep/check.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/bin/sh -_mission_check() { - local maze="$(eval_gettext '$GSH_HOME/Garden/Maze')" - local nb=$(find "$maze" -type f -print0 | xargs -0 grep -l "$(gettext "diamond")" | wc -l) +_mission_check() ( + maze="$(eval_gettext '$GSH_HOME/Garden/Maze')" + nb=$(find "$maze" -type f -print0 | xargs -0 grep -l "$(gettext "diamond")" | wc -l) if [ "$nb" -gt 1 ] then @@ -15,7 +15,6 @@ _mission_check() { return 1 fi - local diamond diamond=$(find "$GSH_CHEST" -type f -print0 | xargs -0 grep -l "$(gettext "diamond")") if [ -z "$diamond" ] @@ -24,19 +23,18 @@ _mission_check() { return 1 fi - local filename=$(cut -d" " -f1 "$GSH_VAR/diamond") + filename=$(cut -d" " -f1 "$GSH_TMP/diamond") if ! [ -f "$GSH_CHEST/$filename" ] then echo "$(gettext "The diamond is not in the chest!")" return 1 - elif ! cmp -s "$GSH_VAR/diamond" "$GSH_CHEST/$filename" + elif ! cmp -s "$GSH_TMP/diamond" "$GSH_CHEST/$filename" then echo "$(gettext "The diamond in your chest is not valid!")" return 1 fi return 0 -} - +) _mission_check diff --git a/missions/finding_files_maze/05_find_xargs_grep/clean.sh b/missions/finding_files_maze/05_find_xargs_grep/clean.sh index 07d7d7e8..df3ffd7c 100644 --- a/missions/finding_files_maze/05_find_xargs_grep/clean.sh +++ b/missions/finding_files_maze/05_find_xargs_grep/clean.sh @@ -1,2 +1,4 @@ -source "$MISSION_DIR/../00_shared/clean.sh" -rm -f "$GSH_VAR/diamond" +#!/bin/sh + +. "$MISSION_DIR/../00_shared/clean.sh" +rm -f "$GSH_TMP/diamond" diff --git a/missions/finding_files_maze/05_find_xargs_grep/init.sh b/missions/finding_files_maze/05_find_xargs_grep/init.sh index 1173550f..f3350180 100644 --- a/missions/finding_files_maze/05_find_xargs_grep/init.sh +++ b/missions/finding_files_maze/05_find_xargs_grep/init.sh @@ -1,27 +1,28 @@ -#!/bin/bash +#!/bin/sh -_mission_init() { - if ! command -v generate_maze.sh &> /dev/null +[ -z "$GSH_CHEST" ] && GSH_CHEST="$(eval_gettext '$GSH_HOME/Forest/Hut/Chest')" +mkdir -p "$GSH_CHEST" + +_mission_init() ( + if ! command -v generate_maze.sh >/dev/null then - local DUMMY_MISSION=$(realpath "$MISSION_DIR/../00_shared") + DUMMY_MISSION=$(realpath "$MISSION_DIR/../00_shared") DUMMY_MISSION=${DUMMY_MISSION#$GSH_MISSIONS/} echo "$(eval_gettext "Dummy mission '\$DUMMY_MISSION' is required for mission \$MISSION_NB (\$MISSION_NAME).")" >&2 return 1 fi - [ -z "$GSH_CHEST" ] && GSH_CHEST="$(eval_gettext '$GSH_HOME/Forest/Hut/Chest')" - mkdir -p "$GSH_CHEST" - - local maze="$(eval_gettext '$GSH_HOME/Garden/Maze')" + maze="$(eval_gettext '$GSH_HOME/Garden/Maze')" mkdir -p "$maze" - local file=$(generate_maze.sh "$maze" 3 10 1 "$(gettext "stone")") + file=$(generate_maze.sh "$maze" 3 10 1 "$(gettext "stone")") - local R=$(basename "$file") - local sum=$(checksum "$R $(gettext "diamond")") + R=$(basename "$file") + sum=$(checksum "$R $(gettext "diamond")") echo "$R $(gettext "diamond") $sum" > "$file" - echo "$R $(gettext "diamond") $sum" > "$GSH_VAR/diamond" + echo "$R $(gettext "diamond") $sum" > "$GSH_TMP/diamond" return 0 -} +) + _mission_init diff --git a/missions/finding_files_maze/05_find_xargs_grep/test.sh b/missions/finding_files_maze/05_find_xargs_grep/test.sh index ae516468..2d3cd819 100644 --- a/missions/finding_files_maze/05_find_xargs_grep/test.sh +++ b/missions/finding_files_maze/05_find_xargs_grep/test.sh @@ -1,3 +1,5 @@ +#!/bin/sh + gsh assert check false ( diff --git a/missions/intermediate/01_alias_la/auto.sh b/missions/intermediate/01_alias_la/auto.sh index 706a72d1..a8068b02 100644 --- a/missions/intermediate/01_alias_la/auto.sh +++ b/missions/intermediate/01_alias_la/auto.sh @@ -1,2 +1,4 @@ +#!/bin/sh + alias la="ls -A" gsh check diff --git a/missions/intermediate/01_alias_la/check.sh b/missions/intermediate/01_alias_la/check.sh index cd6d533e..32e49db8 100644 --- a/missions/intermediate/01_alias_la/check.sh +++ b/missions/intermediate/01_alias_la/check.sh @@ -1,24 +1,25 @@ #!/bin/bash +# "local" is not in POSIX, and function cannot be in a subshell as it needs +# access to aliases defined in the main shell. + _mission_check() { - local cmd=$(alias la 2> /dev/null | cut -f2 -d"=" | tr -d "' \t") - if [ -z "$cmd" ] - then - echo "$(gettext "The alias 'la' doesn't exist...")" - return 1 - elif ! la &> /dev/null - then - echo "$(gettext "The alias 'la' is invalid...")" - unalias la 2>/dev/null - return 1 - elif [ "$cmd" != 'ls-A' ] - then - echo "$(gettext "The alias 'la' doesn't run 'ls -A'...")" - unalias la 2>/dev/null - return 1 - else - return 0 - fi + local cmd=$(alias la 2> /dev/null | cut -f2 -d"=" | tr -d "' \"\\t") + if [ -z "$cmd" ] + then + echo "$(gettext "The alias 'la' doesn't exist...")" + return 1 + elif ! la / >/dev/null 2>/dev/null + then + echo "$(gettext "The alias 'la' is invalid...")" + return 1 + elif [ "$cmd" != 'ls-A' ] + then + echo "$(gettext "The alias 'la' doesn't run 'ls -A'...")" + return 1 + else + return 0 + fi } _mission_check diff --git a/missions/intermediate/01_alias_la/goal.sh b/missions/intermediate/01_alias_la/goal.sh index 14047838..513d7f04 100644 --- a/missions/intermediate/01_alias_la/goal.sh +++ b/missions/intermediate/01_alias_la/goal.sh @@ -1,3 +1,5 @@ +#!/bin/sh + cp "$MISSION_DIR/ascii-art/diamond.txt" ./".$(gettext "nice_rock")" cat "$(eval_gettext '$MISSION_DIR/goal/en.txt')" diff --git a/missions/intermediate/01_alias_la/test.sh b/missions/intermediate/01_alias_la/test.sh index fbf7fb1f..3959e18f 100644 --- a/missions/intermediate/01_alias_la/test.sh +++ b/missions/intermediate/01_alias_la/test.sh @@ -1,3 +1,5 @@ +#!/bin/sh + alias la="ls -A" gsh assert check true unalias la 2>/dev/null diff --git a/missions/intermediate/01_alias_la/treasure.sh b/missions/intermediate/01_alias_la/treasure.sh index e1211215..f14f6c05 100644 --- a/missions/intermediate/01_alias_la/treasure.sh +++ b/missions/intermediate/01_alias_la/treasure.sh @@ -1,5 +1,7 @@ +#!/bin/sh + # NOTE: --literal doesn't exist in freebsd -if ls --literal / &> /dev/null +if ls --literal / >/dev/null 2>/dev/null then alias la='ls --literal -A' else diff --git a/missions/intermediate/02_alias_journal/auto.sh b/missions/intermediate/02_alias_journal/auto.sh index 90177ec5..0cef56b5 100644 --- a/missions/intermediate/02_alias_journal/auto.sh +++ b/missions/intermediate/02_alias_journal/auto.sh @@ -1,2 +1,4 @@ +#!/bin/sh + alias "$(gettext "journal")"="nano \"\$GSH_CHEST/$(gettext "journal").txt\"" gsh check diff --git a/missions/intermediate/02_alias_journal/check.sh b/missions/intermediate/02_alias_journal/check.sh index 67ff9e90..9d4e2d8a 100644 --- a/missions/intermediate/02_alias_journal/check.sh +++ b/missions/intermediate/02_alias_journal/check.sh @@ -1,45 +1,46 @@ #!/bin/bash +# function cannot be in a subshell because it needs access to aliases defined +# in the main shell. + _mission_check() { - local cmd - cmd=$(alias $(gettext "journal") 2> /dev/null | cut -f2 -d"=" | tr -d "'") - if [ -z "$cmd" ] - then - local alias_name=$(gettext "journal") - echo "$(eval_gettext "No alias '\$alias_name' has been found...")" - return 1 - fi + local cmd + cmd=$(alias "$(gettext "journal")" 2>/dev/null | cut -f2 -d"=" | tr -d "'") + if [ -z "$cmd" ] + then + local alias_name=$(gettext "journal") + echo "$(eval_gettext "No alias '\$alias_name' has been found...")" + return 1 + fi - EDITOR=${EDITOR:-nano} - cmd=$(echo "$cmd" | EDITOR=$EDITOR envsubst '$EDITOR') + EDITOR=${EDITOR:-nano} + cmd=$(echo "$cmd" | EDITOR=$EDITOR envsubst '$EDITOR') - case "$cmd" in - *$EDITOR*) - # "cd /" to detect - # alias journal="nano journal.txt" - # used from the Chest - local target_path - target_path="$(cd / ; eval "${cmd/$EDITOR/realpath}" 2>/dev/null)" - if [ "$target_path" = "$(realpath "$GSH_CHEST/$(gettext "journal").txt" 2>/dev/null)" ] - then - return 0 - else - target_path="${cmd// *$EDITOR */}" - echo "$(eval_gettext "It seems you alias doesn't refer to the appropriate file (\$target_path). + case "$cmd" in + *$EDITOR*) + # "cd /" to detect + # alias journal="nano journal.txt" + # used from the Chest + local target_path + target_path="$(cd / ; eval "${cmd/$EDITOR/realpath}" 2>/dev/null)" + if [ "$target_path" = "$(realpath "$GSH_CHEST/$(gettext "journal").txt" 2>/dev/null)" ] + then + return 0 + else + target_path="${cmd// *$EDITOR */}" + echo "$(eval_gettext "It seems you alias doesn't refer to the appropriate file (\$target_path). Make sure to use an absolute path...")" - unalias $(gettext "journal") 2>/dev/null - find "$GSH_HOME" -iname "*$(gettext "journal")*" -print0 | xargs -0 rm -rf - return 1 - fi + find "$GSH_HOME" -iname "*$(gettext "journal")*" -print0 | xargs -0 rm -rf + return 1 + fi - ;; - *) - echo "$(eval_gettext "Your alias doesn't use the command '\$EDITOR'...")" - unalias $(gettext "journal") 2>/dev/null - find "$GSH_HOME" -iname "*$(gettext "journal")*" -print0 | xargs -0 rm -rf - return 1 - ;; - esac + ;; + *) + echo "$(eval_gettext "Your alias doesn't use the command '\$EDITOR'...")" + find "$GSH_HOME" -iname "*$(gettext "journal")*" -print0 | xargs -0 rm -rf + return 1 + ;; + esac } _mission_check diff --git a/missions/intermediate/02_alias_journal/clean.sh b/missions/intermediate/02_alias_journal/clean.sh index 723ee787..789dc2f5 100644 --- a/missions/intermediate/02_alias_journal/clean.sh +++ b/missions/intermediate/02_alias_journal/clean.sh @@ -1,10 +1,10 @@ -#!/bin/bash +#!/bin/sh case "$GSH_LAST_ACTION" in "check_true") ;; *) - unalias journal 2>/dev/null + unalias "$(gettext "journal")" 2>/dev/null unset EDITOR ;; esac diff --git a/missions/intermediate/02_alias_journal/gshrc/en.sh b/missions/intermediate/02_alias_journal/gshrc/en.sh index 19935865..eadcf044 100644 --- a/missions/intermediate/02_alias_journal/gshrc/en.sh +++ b/missions/intermediate/02_alias_journal/gshrc/en.sh @@ -12,7 +12,7 @@ # Whenever you edit this file, the definitions it contains are not # directly added to the current shell. You can however rely on the # following alias to do that for you. -alias reload="source $GSH_CHEST/bashrc" +alias reload="source $GSH_CHEST/gshrc" # Here are a few alias ideas for you: # - an alias "gg" for "gsh goal", diff --git a/missions/intermediate/02_alias_journal/gshrc/fr.sh b/missions/intermediate/02_alias_journal/gshrc/fr.sh index e7f6e102..28293259 100644 --- a/missions/intermediate/02_alias_journal/gshrc/fr.sh +++ b/missions/intermediate/02_alias_journal/gshrc/fr.sh @@ -14,7 +14,7 @@ # Les définitions de ce fichier ne sont pas ajoutées directement # au shell courant. Vous pouvez cependant utiliser le raccourci # suivant pour ajouter les ajouter manuellement. -alias reload="source $GSH_CHEST/bashrc" +alias reload="source $GSH_CHEST/gshrc" # Voici quelques idées d'alias qui peuvent être utiles : # - un alias "gg" pour "gsh goal", diff --git a/missions/intermediate/02_alias_journal/i18n/en.po b/missions/intermediate/02_alias_journal/i18n/en.po index 04c1344f..46fec2de 100644 --- a/missions/intermediate/02_alias_journal/i18n/en.po +++ b/missions/intermediate/02_alias_journal/i18n/en.po @@ -8,15 +8,15 @@ msgstr "Content-Type: text/plain; charset=UTF-8\n" msgid "$GSH_HOME/Forest/Hut/Chest" msgstr "$GSH_HOME/Forest/Hut/Chest" +# path for the text file containing the goal +msgid "$MISSION_DIR/goal/en.txt" +msgstr "$MISSION_DIR/goal/en.txt" + # path for gshrc containing the explanations #, sh-format msgid "$MISSION_DIR/gshrc/en.sh" msgstr "$MISSION_DIR/gshrc/en.sh" -# path for the text file containing the goal -msgid "$MISSION_DIR/goal/en.txt" -msgstr "$MISSION_DIR/goal/en.txt" - # path for the text file containing the treasure message msgid "$MISSION_DIR/treasure-msg/en.txt" msgstr "$MISSION_DIR/treasure-msg/en.txt" diff --git a/missions/intermediate/02_alias_journal/i18n/fr.po b/missions/intermediate/02_alias_journal/i18n/fr.po index 45afa151..651a1f2d 100644 --- a/missions/intermediate/02_alias_journal/i18n/fr.po +++ b/missions/intermediate/02_alias_journal/i18n/fr.po @@ -7,15 +7,15 @@ msgstr "Content-Type: text/plain; charset=UTF-8\n" msgid "$GSH_HOME/Forest/Hut/Chest" msgstr "$GSH_HOME/Foret/Hutte/Coffre" +# path for the text file containing the goal +msgid "$MISSION_DIR/goal/en.txt" +msgstr "$MISSION_DIR/goal/fr.txt" + # path for gshrc containing the explanations #, sh-format msgid "$MISSION_DIR/gshrc/en.sh" msgstr "$MISSION_DIR/gshrc/fr.sh" -# path for the text file containing the goal -msgid "$MISSION_DIR/goal/en.txt" -msgstr "$MISSION_DIR/goal/fr.txt" - # path for the text file containing the treasure message msgid "$MISSION_DIR/treasure-msg/en.txt" msgstr "$MISSION_DIR/treasure-msg/fr.txt" diff --git a/missions/intermediate/02_alias_journal/i18n/template.pot b/missions/intermediate/02_alias_journal/i18n/template.pot index e4b8832e..5b1dc910 100644 --- a/missions/intermediate/02_alias_journal/i18n/template.pot +++ b/missions/intermediate/02_alias_journal/i18n/template.pot @@ -7,15 +7,15 @@ msgstr "Content-Type: text/plain; charset=UTF-8\n" msgid "$GSH_HOME/Forest/Hut/Chest" msgstr "" +# path for the text file containing the goal +msgid "$MISSION_DIR/goal/en.txt" +msgstr "" + # path for gshrc containing the explanations #, sh-format msgid "$MISSION_DIR/gshrc/en.sh" msgstr "" -# path for the text file containing the goal -msgid "$MISSION_DIR/goal/en.txt" -msgstr "" - # path for the text file containing the treasure message msgid "$MISSION_DIR/treasure-msg/en.txt" msgstr "" diff --git a/missions/intermediate/02_alias_journal/init.sh b/missions/intermediate/02_alias_journal/init.sh index e6aa159f..fc8f4ba2 100644 --- a/missions/intermediate/02_alias_journal/init.sh +++ b/missions/intermediate/02_alias_journal/init.sh @@ -1,6 +1,6 @@ -#!/bin/bash +#!/bin/sh -if ! command -v nano &> /dev/null; then +if ! command -v nano >/dev/null; then echo "$(eval_gettext "The command 'nano' is required for mission \$MISSION_NAME. (Debian / Ubuntu: install package 'nano')")" >&2 false diff --git a/missions/intermediate/02_alias_journal/test.sh b/missions/intermediate/02_alias_journal/test.sh index a686fe90..80186756 100644 --- a/missions/intermediate/02_alias_journal/test.sh +++ b/missions/intermediate/02_alias_journal/test.sh @@ -1,27 +1,29 @@ -alias "$(gettext "journal")"="nano \"\$GSH_CHEST/$(gettext "journal").txt\"" +#!/bin/sh + +alias "$(gettext "journal")"="nano \"$GSH_CHEST/$(gettext "journal").txt\"" gsh assert check true unalias "$(gettext "journal")" 2>/dev/null cd "$GSH_CHEST" -alias "$(gettext "journal")"="nano \"$(gettext "journal").txt\"" +alias "$(gettext "journal")"="nano '$(gettext "journal").txt\"" gsh assert check false cd -alias "$(gettext "journal")"="nano \"\${GSH_CHEST#$GSH_ROOT/}/$(gettext "journal").txt\"" +alias "$(gettext "journal")"="nano \"${GSH_CHEST#$GSH_ROOT/}/$(gettext "journal").txt\"" gsh assert check false EDITOR=vim -alias "$(gettext "journal")"="vim \"\$GSH_CHEST/$(gettext "journal").txt\"" +alias "$(gettext "journal")"="vim \"$GSH_CHEST/$(gettext "journal").txt\"" gsh assert check true EDITOR=vim -alias "$(gettext "journal")"="\$EDITOR \"\$GSH_CHEST/$(gettext "journal").txt\"" +alias "$(gettext "journal")"="\$EDITOR \"$GSH_CHEST/$(gettext "journal").txt\"" gsh assert check true EDITOR=vim -alias "$(gettext "journal")"="nano \"\$GSH_CHEST/$(gettext "journal").txt\"" +alias "$(gettext "journal")"="nano \"$GSH_CHEST/$(gettext "journal").txt\"" gsh assert check false EDITOR=nano -alias "$(gettext "journal")"="nano \"\$GSH_CHEST/$(gettext "journal").txt\"" +alias "$(gettext "journal")"="nano \"$GSH_CHEST/$(gettext "journal").txt\"" gsh assert check true diff --git a/missions/intermediate/02_alias_journal/treasure.sh b/missions/intermediate/02_alias_journal/treasure.sh index 77cb067e..478b1041 100644 --- a/missions/intermediate/02_alias_journal/treasure.sh +++ b/missions/intermediate/02_alias_journal/treasure.sh @@ -1,11 +1,13 @@ -alias "$(gettext "journal")"="nano $GSH_CHEST/$(gettext "journal").txt" +#!/bin/sh + +alias "$(gettext "journal")"="nano '$GSH_CHEST/$(gettext "journal").txt'" if [ ! -f "$GSH_CHEST/gshrc" ] then - cp "$(eval_gettext '$MISSION_DIR/gshrc/en.sh')" "$GSH_CHEST/bashrc" + cp "$(eval_gettext '$MISSION_DIR/gshrc/en.sh')" "$GSH_CHEST/gshrc" fi if [ -f "$GSH_CHEST/gshrc" ] then - source "$GSH_CHEST/gshrc" + . "$GSH_CHEST/gshrc" fi diff --git a/missions/intermediate/03_tab_spider_lair/auto.sh b/missions/intermediate/03_tab_spider_lair/auto.sh index 68c84dc0..3891266f 100644 --- a/missions/intermediate/03_tab_spider_lair/auto.sh +++ b/missions/intermediate/03_tab_spider_lair/auto.sh @@ -1,3 +1,5 @@ +#!/bin/sh + lair="$(find "$(eval_gettext '$GSH_HOME/Castle/Cellar')" -type d -name ".$(gettext "Lair_of_the_spider_queen")*")" cd "$lair" queen=$(find "$lair" -type f -name "*$(gettext "spider_queen")*") diff --git a/missions/intermediate/03_tab_spider_lair/check.sh b/missions/intermediate/03_tab_spider_lair/check.sh index c28eb5ce..aca3d9eb 100644 --- a/missions/intermediate/03_tab_spider_lair/check.sh +++ b/missions/intermediate/03_tab_spider_lair/check.sh @@ -1,42 +1,42 @@ -#!/bin/bash - -_mission_check() { - - # adjust - local MAX_DELAY=20 - - - local lair="$(find "$(eval_gettext '$GSH_HOME/Castle/Cellar')" -type d -name ".$(gettext "Lair_of_the_spider_queen")*")" - if [ "$(realpath "$(pwd)")" != "$(realpath "$lair")" ] - then - echo "$(gettext "You are not in the queen spider lair!")" - return 1 - fi - - local queen=$(find "$lair" -type f -name "*$(gettext "spider_queen")*") - if [ -n "$queen" ] - then - echo "$(gettext "The queen spider is still in its lair...")" - return 1 - fi - local bat=$(find "$lair" -type f -name "*$(gettext "baby_bat")*") - if [ -z "$bat" ] - then - echo "$(gettext "Where is the baby bat?")" - return 1 - fi - - local now=$(date +%s) - local start=$(cat "$GSH_VAR/start_time") - local nb_seconds=$((now - start)) - if [ "$nb_seconds" -gt "$MAX_DELAY" ] - then - echo "$(eval_gettext "Good, but you took \$nb_seconds seconds. You needed to take less than \$MAX_DELAY seconds...")" - return 1 - fi - - echo "$(eval_gettext "Perfect, it took you only \$nb_seconds seconds to complete this mission!")" - return 0 -} +#!/bin/sh + +_mission_check() ( + + # adjust + MAX_DELAY=20 + + + lair="$(find "$(eval_gettext '$GSH_HOME/Castle/Cellar')" -type d -name ".$(gettext "Lair_of_the_spider_queen")*")" + if [ "$(realpath "$(pwd)")" != "$(realpath "$lair")" ] + then + echo "$(gettext "You are not in the queen spider lair!")" + return 1 + fi + + queen=$(find "$lair" -type f -name "*$(gettext "spider_queen")*") + if [ -n "$queen" ] + then + echo "$(gettext "The queen spider is still in its lair...")" + return 1 + fi + bat=$(find "$lair" -type f -name "*$(gettext "baby_bat")*") + if [ -z "$bat" ] + then + echo "$(gettext "Where is the baby bat?")" + return 1 + fi + + now=$(date +%s) + start=$(cat "$GSH_TMP/start_time") + nb_seconds=$((now - start)) + if [ "$nb_seconds" -gt "$MAX_DELAY" ] + then + echo "$(eval_gettext "Good, but you took \$nb_seconds seconds. You needed to take less than \$MAX_DELAY seconds...")" + return 1 + fi + + echo "$(eval_gettext "Perfect, it took you only \$nb_seconds seconds to complete this mission!")" + return 0 +) _mission_check diff --git a/missions/intermediate/03_tab_spider_lair/clean.sh b/missions/intermediate/03_tab_spider_lair/clean.sh index 4aa768e2..12f03b49 100644 --- a/missions/intermediate/03_tab_spider_lair/clean.sh +++ b/missions/intermediate/03_tab_spider_lair/clean.sh @@ -1,5 +1,7 @@ +#!/bin/sh + set +o noglob -rm -f "$GSH_VAR/start_time" +rm -f "$GSH_TMP/start_time" case $GSH_LAST_ACTION in check_true) diff --git a/missions/intermediate/03_tab_spider_lair/init.sh b/missions/intermediate/03_tab_spider_lair/init.sh index 4065919a..a35eca20 100644 --- a/missions/intermediate/03_tab_spider_lair/init.sh +++ b/missions/intermediate/03_tab_spider_lair/init.sh @@ -1,23 +1,23 @@ -#!/bin/bash +#!/bin/sh -_mission_init() { - local r1=$(random_string 16) - local r2=$(random_string 16) - local lair="$(eval_gettext '$GSH_HOME/Castle/Cellar')/.$(gettext "Lair_of_the_spider_queen") ${r1} ${r2}" +_mission_init() ( + r1=$(random_string 16) + r2=$(random_string 16) + lair="$(eval_gettext '$GSH_HOME/Castle/Cellar')/.$(gettext "Lair_of_the_spider_queen") ${r1} ${r2}" mkdir -p "$lair" r1=$(random_string 16) r2=$(random_string 16) - local queen="${r1}_$(gettext "spider_queen")_$r2" + queen="${r1}_$(gettext "spider_queen")_$r2" sign_file "$MISSION_DIR/ascii-art/spider-queen.txt" "$lair/$queen" r1=$(random_string 16) r2=$(random_string 16) - local bat="${r1}_$(gettext "baby_bat")_$r2" + bat="${r1}_$(gettext "baby_bat")_$r2" sign_file "$MISSION_DIR/ascii-art/baby-bat.txt" "$lair/$bat" - date +%s > "$GSH_VAR/start_time" -} + date +%s > "$GSH_TMP/start_time" +) set -o noglob _mission_init diff --git a/missions/intermediate/03_tab_spider_lair/static.sh b/missions/intermediate/03_tab_spider_lair/static.sh index 7382a09f..89077f9c 100644 --- a/missions/intermediate/03_tab_spider_lair/static.sh +++ b/missions/intermediate/03_tab_spider_lair/static.sh @@ -1 +1,3 @@ +#!/bin/sh + mkdir -p "$(eval_gettext "\$GSH_HOME/Castle/Cellar")" diff --git a/missions/intermediate/03_tab_spider_lair/test.sh b/missions/intermediate/03_tab_spider_lair/test.sh index 0fc31b1b..3ec57029 100644 --- a/missions/intermediate/03_tab_spider_lair/test.sh +++ b/missions/intermediate/03_tab_spider_lair/test.sh @@ -1,3 +1,5 @@ +#!/bin/sh + cd gsh assert check false diff --git a/missions/intermediate/04_bg_xeyes/auto.sh b/missions/intermediate/04_bg_xeyes/auto.sh index 856084d0..6675692b 100644 --- a/missions/intermediate/04_bg_xeyes/auto.sh +++ b/missions/intermediate/04_bg_xeyes/auto.sh @@ -1,3 +1,7 @@ +#!/bin/bash + +# ``history`` command is not in POSIX + history -s "xeyes" history -s "xeyes &" xeyes & diff --git a/missions/intermediate/04_bg_xeyes/check.sh b/missions/intermediate/04_bg_xeyes/check.sh index 3e65b81c..25daea50 100644 --- a/missions/intermediate/04_bg_xeyes/check.sh +++ b/missions/intermediate/04_bg_xeyes/check.sh @@ -1,4 +1,6 @@ -#!/bin/bash +#!/bin/sh + +# fc in specified in POSIX, but debian's sh doesn't implement it! # turn history on (off by default for non-interactive shells HISTFILE=$GSH_CONFIG/history diff --git a/missions/intermediate/04_bg_xeyes/init.sh b/missions/intermediate/04_bg_xeyes/init.sh index 4cb5d490..8ca061db 100644 --- a/missions/intermediate/04_bg_xeyes/init.sh +++ b/missions/intermediate/04_bg_xeyes/init.sh @@ -1,12 +1,14 @@ +#!/bin/sh + if [ -z "$DISPLAY" ]; then echo "$(eval_gettext "The variable DISPLAY is not defined. A running X server is required for mission \$MISSION_NAME.")" >&2 false -elif ! command -v xeyes &> /dev/null; then +elif ! command -v xeyes >/dev/null; then echo "$(eval_gettext "The command 'xeyes' is required for mission \$MISSION_NAME. (Debian / Ubuntu: install package 'x11-apps')")" >&2 false -elif ! command -v ps &> /dev/null; then +elif ! command -v ps >/dev/null; then echo "$(eval_gettext "The command 'ps' is required for mission \$MISSION_NAME. (Debian / Ubuntu: install package 'psproc')")" >&2 false diff --git a/missions/intermediate/04_bg_xeyes/test.sh b/missions/intermediate/04_bg_xeyes/test.sh index 64792723..54a6b384 100644 --- a/missions/intermediate/04_bg_xeyes/test.sh +++ b/missions/intermediate/04_bg_xeyes/test.sh @@ -1,3 +1,5 @@ +#!/bin/bash + history -s "xeyes" history -s "xeyes &" gsh assert check false diff --git a/missions/misc/01_cal_nostradamus/auto.sh b/missions/misc/01_cal_nostradamus/auto.sh index e0cd7e03..694fb9b3 100644 --- a/missions/misc/01_cal_nostradamus/auto.sh +++ b/missions/misc/01_cal_nostradamus/auto.sh @@ -1,21 +1,20 @@ -#!/bin/bash +#!/bin/sh -_mission_auto() { - local YYYY=$(cut -d"-" -f1 "$GSH_VAR"/date) - local MM=$(cut -d"-" -f2 "$GSH_VAR"/date) - local DD=$(cut -d"-" -f3 "$GSH_VAR"/date) +_mission_auto() ( + YYYY=$(cut -d"-" -f1 "$GSH_TMP"/date) + MM=$(cut -d"-" -f2 "$GSH_TMP"/date) + DD=$(cut -d"-" -f3 "$GSH_TMP"/date) DOW() ( - local dow if dow=$(date --date="$1" +%A 2> /dev/null) then # with gnu "date" command - echo $dow + echo "$dow" return 0 elif dow=$(date -jf "%Y-%m-%d" "$1" +%A 2> /dev/null) then # with freebsd "date" command - echo $dow + echo "$dow" return 0 else echo "$(gettext "Error: can not get day of week with 'date' command.")" >&2 @@ -23,18 +22,17 @@ _mission_auto() { fi ) - local day=$(DOW "$YYYY-$MM-$DD") - local I + day=$(DOW "$YYYY-$MM-$DD") for I in $(seq 7) do - local answer=$(DOW "2000-05-$I") + answer=$(DOW "2000-05-$I") if [ "$day" = "$answer" ] then unset -f DOW - gsh check < <(echo "$I") + echo "$I" return fi done -} +) -_mission_auto +_mission_auto | gsh check diff --git a/missions/misc/01_cal_nostradamus/check.sh b/missions/misc/01_cal_nostradamus/check.sh index 7aeb237b..9edd7b54 100644 --- a/missions/misc/01_cal_nostradamus/check.sh +++ b/missions/misc/01_cal_nostradamus/check.sh @@ -1,17 +1,16 @@ -#!/bin/bash +#!/bin/sh -_mission_check() { +_mission_check() ( DOW() ( - local dow if dow=$(date --date="$1" +%A 2> /dev/null) then # with gnu "date" command - echo $dow + echo "$dow" return 0 elif dow=$(date -jf "%Y-%m-%d" "$1" +%A 2> /dev/null) then # with freebsd "date" command - echo $dow + echo "$dow" return 0 else echo "$(gettext "Error: can not get day of week with 'date' command.")" >&2 @@ -19,20 +18,19 @@ _mission_check() { fi ) - local YYYY=$(cut -d"-" -f1 "$GSH_VAR"/date) - local MM=$(cut -d"-" -f2 "$GSH_VAR"/date) - local DD=$(cut -d"-" -f3 "$GSH_VAR"/date) + YYYY=$(cut -d"-" -f1 "$GSH_TMP"/date) + MM=$(cut -d"-" -f2 "$GSH_TMP"/date) + DD=$(cut -d"-" -f3 "$GSH_TMP"/date) echo "$(eval_gettext 'What was the day of the week for the $MM-$DD-$YYYY?')" - local i for i in $(seq 7) do - printf " $i : " + printf " %d : " "$i" DOW "2000-05-$i" done - local n - read -erp "$(gettext "Your answer: ")" n + printf "%s " "$(gettext "Your answer:")" + read -r n case "$n" in *[!0-9]* | "") @@ -45,11 +43,11 @@ _mission_check() { echo "$(gettext "That's not even a valid answer!")" return 1 fi - local s=$(DOW "2000-05-$n") - local t=$(DOW "$YYYY-$MM-$DD") + s=$(DOW "2000-05-$n") + t=$(DOW "$YYYY-$MM-$DD") unset -f DOW [ "$t" = "$s" ] esac -} +) _mission_check diff --git a/missions/misc/01_cal_nostradamus/clean.sh b/missions/misc/01_cal_nostradamus/clean.sh index 37301693..50cc279f 100644 --- a/missions/misc/01_cal_nostradamus/clean.sh +++ b/missions/misc/01_cal_nostradamus/clean.sh @@ -1 +1,3 @@ -rm -f "$GSH_VAR"/date +#!/bin/sh + +rm -f "$GSH_TMP"/date diff --git a/missions/misc/01_cal_nostradamus/goal.sh b/missions/misc/01_cal_nostradamus/goal.sh index 4e8724fb..77962d26 100644 --- a/missions/misc/01_cal_nostradamus/goal.sh +++ b/missions/misc/01_cal_nostradamus/goal.sh @@ -1,7 +1,7 @@ -#!/bin/bash +#!/bin/sh -export YYYY=$(cut -d"-" -f1 "$GSH_VAR"/date) -export MM=$(cut -d"-" -f2 "$GSH_VAR"/date) -export DD=$(cut -d"-" -f3 "$GSH_VAR"/date) +export YYYY=$(cut -d"-" -f1 "$GSH_TMP"/date) +export MM=$(cut -d"-" -f2 "$GSH_TMP"/date) +export DD=$(cut -d"-" -f3 "$GSH_TMP"/date) envsubst '$YYYY $MM $DD' < "$(eval_gettext '$MISSION_DIR/goal/en.txt')" unset YYYY MM DD diff --git a/missions/misc/01_cal_nostradamus/i18n/en.po b/missions/misc/01_cal_nostradamus/i18n/en.po index 52c37ba0..e872e540 100644 --- a/missions/misc/01_cal_nostradamus/i18n/en.po +++ b/missions/misc/01_cal_nostradamus/i18n/en.po @@ -29,5 +29,5 @@ msgstr "The command 'cal' is required for mission $MISSION_NAME.\n" msgid "What was the day of the week for the $MM-$DD-$YYYY?" msgstr "What was the day of the week for the $MM-$DD-$YYYY?" -msgid "Your answer: " -msgstr "Your answer: " +msgid "Your answer:" +msgstr "Your answer:" diff --git a/missions/misc/01_cal_nostradamus/i18n/fr.po b/missions/misc/01_cal_nostradamus/i18n/fr.po index a92c921d..63564337 100644 --- a/missions/misc/01_cal_nostradamus/i18n/fr.po +++ b/missions/misc/01_cal_nostradamus/i18n/fr.po @@ -28,5 +28,5 @@ msgstr "La commande 'cal' est nécessaire pour la mission $MISSION_NAME.\n" msgid "What was the day of the week for the $MM-$DD-$YYYY?" msgstr "Quel était le jours de la semaine pour le $DD-$MM-$YYYY ?" -msgid "Your answer: " -msgstr "Votre réponse : " +msgid "Your answer:" +msgstr "Votre réponse :" diff --git a/missions/misc/01_cal_nostradamus/i18n/template.pot b/missions/misc/01_cal_nostradamus/i18n/template.pot index d65a7f78..cfd22faf 100644 --- a/missions/misc/01_cal_nostradamus/i18n/template.pot +++ b/missions/misc/01_cal_nostradamus/i18n/template.pot @@ -27,5 +27,5 @@ msgstr "" msgid "What was the day of the week for the $MM-$DD-$YYYY?" msgstr "" -msgid "Your answer: " +msgid "Your answer:" msgstr "" diff --git a/missions/misc/01_cal_nostradamus/init.sh b/missions/misc/01_cal_nostradamus/init.sh index ec3c651d..1863128d 100644 --- a/missions/misc/01_cal_nostradamus/init.sh +++ b/missions/misc/01_cal_nostradamus/init.sh @@ -1,15 +1,15 @@ -#!/bin/bash +#!/bin/sh -_mission_init() { - if ! command -v cal &> /dev/null; then +_mission_init() ( + if ! command -v cal >/dev/null; then echo "$(eval_gettext "The command 'cal' is required for mission \$MISSION_NAME. (Debian / Ubuntu: install package 'bsdmainutils')")" >&2 return 1 fi - local YYYY=$((1900 + RANDOM % 300)) - local MM=$(printf "%02d" "$((1 + RANDOM % 12))") - local DD=$(printf "%02d" "$((13 + RANDOM % 5))") - echo "$YYYY-$MM-$DD" > "$GSH_VAR/date" -} + YYYY=$((1900 + $(RANDOM) % 300)) + MM=$(printf "%02d" "$((1 + $(RANDOM) % 12))") + DD=$(printf "%02d" "$((13 + $(RANDOM) % 5))") + echo "$YYYY-$MM-$DD" > "$GSH_TMP/date" +) _mission_init diff --git a/missions/misc/01_cal_nostradamus/test.sh b/missions/misc/01_cal_nostradamus/test.sh index 5d4f617b..e9af5931 100644 --- a/missions/misc/01_cal_nostradamus/test.sh +++ b/missions/misc/01_cal_nostradamus/test.sh @@ -1,55 +1,48 @@ -_DOW() { - local dow +#!/bin/sh + +_DOW() ( if dow=$(date --date="$1" +%A 2> /dev/null) then # with gnu "date" command - echo $dow + echo "$dow" return 0 elif dow=$(date -jf "%Y-%m-%d" "$1" +%A 2> /dev/null) then # with freebsd "date" command - echo $dow + echo "$dow" return 0 else echo "$(gettext "Error: can not get day of week with 'date' command.")" >&2 return 1 fi -} +) -get_answer() { - local offset=$1 +get_answer() ( + offset=$1 - local YYYY=$(cut -d"-" -f1 "$GSH_VAR"/date) - local MM=$(cut -d"-" -f2 "$GSH_VAR"/date) - local DD=$(cut -d"-" -f3 "$GSH_VAR"/date) + YYYY=$(cut -d"-" -f1 "$GSH_TMP"/date) + MM=$(cut -d"-" -f2 "$GSH_TMP"/date) + DD=$(cut -d"-" -f3 "$GSH_TMP"/date) - local day=$(_DOW "$YYYY-$MM-$DD") - local i + day=$(_DOW "$YYYY-$MM-$DD") for i in $(seq 7) do - local answer=$(_DOW "2000-05-$i") + answer=$(_DOW "2000-05-$i") if [ "$day" = "$answer" ] then echo $((1+(i + offset - 1) % 7)) break fi done -} +) -gsh assert check true < <(get_answer 0) > /dev/null -printf "." -gsh assert check false < <(get_answer 1) > /dev/null -printf "." -gsh assert check false < <(get_answer 2) > /dev/null -printf "." -gsh assert check false < <(get_answer 3) > /dev/null -printf "." -gsh assert check false < <(get_answer 4) > /dev/null -printf "." -gsh assert check false < <(get_answer 5) > /dev/null -printf "." -gsh assert check false < <(get_answer 6) > /dev/null -printf "." -gsh assert check true < <(get_answer 7) > /dev/null +get_answer 0 | gsh assert check true +get_answer 1 | gsh assert check false +get_answer 2 | gsh assert check false +get_answer 3 | gsh assert check false +get_answer 4 | gsh assert check false +get_answer 5 | gsh assert check false +get_answer 6 | gsh assert check false +get_answer 7 | gsh assert check true unset -f get_answer _DOW diff --git a/missions/misc/02_nano_journal/auto.sh b/missions/misc/02_nano_journal/auto.sh index f5151103..df67c02d 100644 --- a/missions/misc/02_nano_journal/auto.sh +++ b/missions/misc/02_nano_journal/auto.sh @@ -1,3 +1,5 @@ +#!/bin/sh + mkdir -p "$GSH_CHEST" echo "I'll be back." > "$GSH_CHEST/$(gettext "journal").txt" gsh check diff --git a/missions/misc/02_nano_journal/check.sh b/missions/misc/02_nano_journal/check.sh index af8f46fc..137313ee 100644 --- a/missions/misc/02_nano_journal/check.sh +++ b/missions/misc/02_nano_journal/check.sh @@ -1,7 +1,7 @@ -#!/bin/bash +#!/bin/sh -_mission_check() { - local journal_file="$GSH_CHEST/$(gettext "journal").txt" +_mission_check() ( + journal_file="$GSH_CHEST/$(gettext "journal").txt" if [ ! -f "$journal_file" ] then journal=~${journal#$GSH_ROOT} @@ -17,6 +17,6 @@ _mission_check() { else return 0 fi -} +) _mission_check diff --git a/missions/misc/02_nano_journal/init.sh b/missions/misc/02_nano_journal/init.sh index e6aa159f..fc8f4ba2 100644 --- a/missions/misc/02_nano_journal/init.sh +++ b/missions/misc/02_nano_journal/init.sh @@ -1,6 +1,6 @@ -#!/bin/bash +#!/bin/sh -if ! command -v nano &> /dev/null; then +if ! command -v nano >/dev/null; then echo "$(eval_gettext "The command 'nano' is required for mission \$MISSION_NAME. (Debian / Ubuntu: install package 'nano')")" >&2 false diff --git a/missions/misc/02_nano_journal/test.sh b/missions/misc/02_nano_journal/test.sh index 41a1591b..282b8771 100644 --- a/missions/misc/02_nano_journal/test.sh +++ b/missions/misc/02_nano_journal/test.sh @@ -1,3 +1,5 @@ +#!/bin/sh + rm -f "$GSH_CHEST/$(gettext "journal").txt" gsh assert check false diff --git a/missions/misc/03_tr_caesar_shift/auto.sh b/missions/misc/03_tr_caesar_shift/auto.sh index 75277500..7059361f 100644 --- a/missions/misc/03_tr_caesar_shift/auto.sh +++ b/missions/misc/03_tr_caesar_shift/auto.sh @@ -1 +1,3 @@ -gsh check < "$GSH_VAR/magic_word" +#!/bin/sh + +gsh check < "$GSH_TMP/magic_word" diff --git a/missions/misc/03_tr_caesar_shift/check.sh b/missions/misc/03_tr_caesar_shift/check.sh index c8d39391..d5bd43fc 100644 --- a/missions/misc/03_tr_caesar_shift/check.sh +++ b/missions/misc/03_tr_caesar_shift/check.sh @@ -1,9 +1,9 @@ -#!/bin/bash +#!/bin/sh echo "$(gettext "What's the key that will make Merlin's chest to appear?")" -read -er dcode +read -r dcode -if [ "$dcode" = "$(cat "$GSH_VAR/magic_word")" ] +if [ "$dcode" = "$(cat "$GSH_TMP/magic_word")" ] then merlin_chest=$(eval_gettext '$GSH_HOME/Castle/Main_building/Library/Merlin_s_office')/$(gettext 'Merlin_s_chest') mkdir -p "$merlin_chest" diff --git a/missions/misc/03_tr_caesar_shift/clean.sh b/missions/misc/03_tr_caesar_shift/clean.sh index 0f8d5da1..363bccf8 100644 --- a/missions/misc/03_tr_caesar_shift/clean.sh +++ b/missions/misc/03_tr_caesar_shift/clean.sh @@ -1 +1,3 @@ -rm -f "$GSH_VAR/magic_word" +#!/bin/sh + +rm -f "$GSH_TMP/magic_word" diff --git a/missions/misc/03_tr_caesar_shift/init.sh b/missions/misc/03_tr_caesar_shift/init.sh index e3ab3bd0..6f36c872 100644 --- a/missions/misc/03_tr_caesar_shift/init.sh +++ b/missions/misc/03_tr_caesar_shift/init.sh @@ -1,22 +1,22 @@ -#!/bin/bash +#!/bin/sh # mission originaly created by Tiemen Duvillard -_mission_init() { - local SECRET_KEY=$(random_string 4 | tr A-Z a-z) +_mission_init() ( + SECRET_KEY=$(random_string 4 | tr A-Z a-z) # FIXME, only store hash of key? - echo "$SECRET_KEY" > "$GSH_VAR/magic_word" + echo "$SECRET_KEY" > "$GSH_TMP/magic_word" - local random_shift=$((12 + RANDOM % 3)) + random_shift=$((12 + $(RANDOM) % 3)) - local ab=abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz - local D=${ab:random_shift:26} + ab=abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz + D=$(echo "$ab" | cut -c "$((random_shift+1))-$((random_shift+26))") echo "$(eval_gettext "here is my will: you will get my chest, and everything it contains. this check is in the cellar, and the word to make it re-appear is: \$SECRET_KEY merlin the enchanter")" | tr "a-z" "$D" > "$(eval_gettext '$GSH_HOME/Castle/Main_building/Library/Merlin_s_office/Drawer')/$(gettext 'secret_message')" -} +) _mission_init diff --git a/missions/misc/03_tr_caesar_shift/static.sh b/missions/misc/03_tr_caesar_shift/static.sh index b28a14aa..33b9bd2e 100644 --- a/missions/misc/03_tr_caesar_shift/static.sh +++ b/missions/misc/03_tr_caesar_shift/static.sh @@ -1,2 +1,4 @@ +#!/bin/sh + mkdir -p "$(eval_gettext '$GSH_HOME/Castle/Main_building/Library/Merlin_s_office/Drawer')" sign_file "$MISSION_DIR/ascii-art/ink_scroll.txt" "$(eval_gettext '$GSH_HOME/Castle/Main_building/Library/Merlin_s_office/Drawer')/$(gettext "ink_and_scroll")" diff --git a/missions/permissions/01_chmod_x_dir_king_quarter/auto.sh b/missions/permissions/01_chmod_x_dir_king_quarter/auto.sh index d3901a77..91fc68d5 100644 --- a/missions/permissions/01_chmod_x_dir_king_quarter/auto.sh +++ b/missions/permissions/01_chmod_x_dir_king_quarter/auto.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh chmod +x "$(eval_gettext '$GSH_HOME/Castle/Main_building/Throne_room/Kings_quarter')" cd "$(eval_gettext '$GSH_HOME/Castle/Main_building/Throne_room/Kings_quarter')" diff --git a/missions/permissions/01_chmod_x_dir_king_quarter/check.sh b/missions/permissions/01_chmod_x_dir_king_quarter/check.sh index eb80387d..5d097549 100644 --- a/missions/permissions/01_chmod_x_dir_king_quarter/check.sh +++ b/missions/permissions/01_chmod_x_dir_king_quarter/check.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh dir=$(eval_gettext '$GSH_HOME/Castle/Main_building/Throne_room/Kings_quarter') diff --git a/missions/permissions/01_chmod_x_dir_king_quarter/clean.sh b/missions/permissions/01_chmod_x_dir_king_quarter/clean.sh index a64ee5e9..781e910c 100644 --- a/missions/permissions/01_chmod_x_dir_king_quarter/clean.sh +++ b/missions/permissions/01_chmod_x_dir_king_quarter/clean.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh king_quarter=$(eval_gettext '$GSH_HOME/Castle/Main_building/Throne_room/Kings_quarter') diff --git a/missions/permissions/01_chmod_x_dir_king_quarter/init.sh b/missions/permissions/01_chmod_x_dir_king_quarter/init.sh index c3a71071..7b190d4c 100644 --- a/missions/permissions/01_chmod_x_dir_king_quarter/init.sh +++ b/missions/permissions/01_chmod_x_dir_king_quarter/init.sh @@ -1,7 +1,7 @@ -#!/bin/bash +#!/bin/sh -_mission_init() { - if ! command -v man &> /dev/null; then +_mission_init() ( + if ! command -v man >/dev/null; then echo "$(eval_gettext "The command 'man' is required for mission \$MISSION_NAME. (Debian / Ubuntu: install package 'man-db')")" >&2 return 1 @@ -9,10 +9,10 @@ _mission_init() { [ "$(realpath "$(pwd)")" = "$(realpath "$dir")" ] && cd .. - local dir=$(eval_gettext '$GSH_HOME/Castle/Main_building/Throne_room/Kings_quarter') + dir=$(eval_gettext '$GSH_HOME/Castle/Main_building/Throne_room/Kings_quarter') chmod -x "$dir" return 0 -} +) _mission_init diff --git a/missions/permissions/01_chmod_x_dir_king_quarter/static.sh b/missions/permissions/01_chmod_x_dir_king_quarter/static.sh index 5b870670..ff2b3b1f 100644 --- a/missions/permissions/01_chmod_x_dir_king_quarter/static.sh +++ b/missions/permissions/01_chmod_x_dir_king_quarter/static.sh @@ -1,3 +1,3 @@ -#!/bin/bash +#!/bin/sh mkdir -p "$(eval_gettext '$GSH_HOME/Castle/Main_building/Throne_room/Kings_quarter')" diff --git a/missions/permissions/01_chmod_x_dir_king_quarter/test.sh b/missions/permissions/01_chmod_x_dir_king_quarter/test.sh index d52f03c8..f78761bc 100644 --- a/missions/permissions/01_chmod_x_dir_king_quarter/test.sh +++ b/missions/permissions/01_chmod_x_dir_king_quarter/test.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh gsh assert check false diff --git a/missions/permissions/02_chmod_r_file_king_quarter/auto.sh b/missions/permissions/02_chmod_r_file_king_quarter/auto.sh index 27c11fe8..390b6081 100644 --- a/missions/permissions/02_chmod_r_file_king_quarter/auto.sh +++ b/missions/permissions/02_chmod_r_file_king_quarter/auto.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh dir=$(eval_gettext '$GSH_HOME/Castle/Main_building/Throne_room/Kings_quarter') chmod +r "$dir/.$(gettext "secret_note")" diff --git a/missions/permissions/02_chmod_r_file_king_quarter/check.sh b/missions/permissions/02_chmod_r_file_king_quarter/check.sh index 40398ec5..bb697a5e 100644 --- a/missions/permissions/02_chmod_r_file_king_quarter/check.sh +++ b/missions/permissions/02_chmod_r_file_king_quarter/check.sh @@ -1,7 +1,8 @@ -#!/bin/bash +#!/bin/sh -read -erp "$(gettext "What's the combination to open the King's safe?") " key -real_key=$(cat "$GSH_VAR/key") +printf "%s " "$(gettext "What's the combination to open the King's safe?")" +read -r key +real_key=$(cat "$GSH_TMP/key") if [ "$key" = "$real_key" ] then diff --git a/missions/permissions/02_chmod_r_file_king_quarter/clean.sh b/missions/permissions/02_chmod_r_file_king_quarter/clean.sh index d9cfb810..481bc6fd 100644 --- a/missions/permissions/02_chmod_r_file_king_quarter/clean.sh +++ b/missions/permissions/02_chmod_r_file_king_quarter/clean.sh @@ -1,6 +1,6 @@ -#!/bin/bash +#!/bin/sh # avoid problems when making a tar archive chmod +r "$(eval_gettext '$GSH_HOME/Castle/Main_building/Throne_room/Kings_quarter')/.$(gettext "secret_note")" -rm -f "$GSH_VAR/key" +rm -f "$GSH_TMP/key" diff --git a/missions/permissions/02_chmod_r_file_king_quarter/init.sh b/missions/permissions/02_chmod_r_file_king_quarter/init.sh index 6d34892a..7d110863 100644 --- a/missions/permissions/02_chmod_r_file_king_quarter/init.sh +++ b/missions/permissions/02_chmod_r_file_king_quarter/init.sh @@ -1,18 +1,18 @@ -#!/bin/bash +#!/bin/sh -_mission_init() { - if ! command -v man &> /dev/null; then +_mission_init() ( + if ! command -v man >/dev/null; then echo "$(eval_gettext "The command 'man' is required for mission \$MISSION_NAME. (Debian / Ubuntu: install package 'man-db')")" >&2 return 1 fi - local dir=$(eval_gettext '$GSH_HOME/Castle/Main_building/Throne_room/Kings_quarter') - local key=$RANDOM$RANDOM - echo "$key" > "$GSH_VAR/key" + dir=$(eval_gettext '$GSH_HOME/Castle/Main_building/Throne_room/Kings_quarter') + key=$(RANDOM)$(RANDOM) + echo "$key" > "$GSH_TMP/key" echo "$key" > "$dir/.$(gettext "secret_note")" echo "0123456789" > "$dir/$(gettext "note")" chmod -r "$dir/.$(gettext "secret_note")" -} +) _mission_init diff --git a/missions/permissions/02_chmod_r_file_king_quarter/static.sh b/missions/permissions/02_chmod_r_file_king_quarter/static.sh index 5b870670..ff2b3b1f 100644 --- a/missions/permissions/02_chmod_r_file_king_quarter/static.sh +++ b/missions/permissions/02_chmod_r_file_king_quarter/static.sh @@ -1,3 +1,3 @@ -#!/bin/bash +#!/bin/sh mkdir -p "$(eval_gettext '$GSH_HOME/Castle/Main_building/Throne_room/Kings_quarter')" diff --git a/missions/permissions/02_chmod_r_file_king_quarter/test.sh b/missions/permissions/02_chmod_r_file_king_quarter/test.sh index 723d5756..386d2082 100644 --- a/missions/permissions/02_chmod_r_file_king_quarter/test.sh +++ b/missions/permissions/02_chmod_r_file_king_quarter/test.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh dir=$(eval_gettext '$GSH_HOME/Castle/Main_building/Throne_room/Kings_quarter') chmod +r "$dir/.$(gettext "secret_note")" diff --git a/missions/permissions/03_chmod_rw_file_dir_throne_room/auto.sh b/missions/permissions/03_chmod_rw_file_dir_throne_room/auto.sh index 4fa19ac9..1c4790a2 100644 --- a/missions/permissions/03_chmod_rw_file_dir_throne_room/auto.sh +++ b/missions/permissions/03_chmod_rw_file_dir_throne_room/auto.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh safedir="$(eval_gettext '$GSH_HOME/Castle/Main_building/Throne_room')/$(gettext "Safe")" chmod 755 "$safedir" @@ -7,6 +7,6 @@ mv "$safedir/$(gettext "crown")" "$GSH_CHEST" key=$(tail -n 1 "$GSH_CHEST/$(gettext "crown")" | cut -c 4-6) -gsh check < <(echo $key) +echo "$key" | gsh check unset safedir key diff --git a/missions/permissions/03_chmod_rw_file_dir_throne_room/check.sh b/missions/permissions/03_chmod_rw_file_dir_throne_room/check.sh index 4a8c1e0b..72f23d86 100644 --- a/missions/permissions/03_chmod_rw_file_dir_throne_room/check.sh +++ b/missions/permissions/03_chmod_rw_file_dir_throne_room/check.sh @@ -1,34 +1,34 @@ -#!/bin/bash +#!/bin/sh -_mission_check() { - if ! [ -f "$GSH_CHEST/$(gettext "crown")" ] - then - echo "$(gettext "There is no crown in your chest!")" - return 1 - fi +_mission_check() ( + if ! [ -f "$GSH_CHEST/$(gettext "crown")" ] + then + echo "$(gettext "There is no crown in your chest!")" + return 1 + fi - if ! [ -r "$GSH_CHEST/$(gettext "crown")" ] - then - echo "$(gettext "The crown in your chest is not readable!")" - return 1 - fi + if ! [ -r "$GSH_CHEST/$(gettext "crown")" ] + then + echo "$(gettext "The crown in your chest is not readable!")" + return 1 + fi - if ! cmp -s "$GSH_CHEST/$(gettext "crown")" "$GSH_VAR/crown" - then - echo "$(gettext "The crown in your chest is a fake!")" - return 1 - fi + if ! cmp -s "$GSH_CHEST/$(gettext "crown")" "$GSH_TMP/crown" + then + echo "$(gettext "The crown in your chest is a fake!")" + return 1 + fi - local real_key=$(tail -n 1 "$GSH_VAR/crown" | cut -c 4-6) + real_key=$(tail -n 1 "$GSH_TMP/crown" | cut -c 4-6) - local given_key - read -erp "$(gettext "What are the 3 digits inscribed on the base of the crown?") " given_key - if [ "$real_key" -ne "$given_key" ] - then - echo "$(gettext "Those digits are not correct!")" - return 1 - fi - return 0 -} + printf "%s " "$(gettext "What are the 3 digits inscribed on the base of the crown?")" + read -r given_key + if [ "$real_key" -ne "$given_key" ] + then + echo "$(gettext "Those digits are not correct!")" + return 1 + fi + return 0 +) _mission_check diff --git a/missions/permissions/03_chmod_rw_file_dir_throne_room/clean.sh b/missions/permissions/03_chmod_rw_file_dir_throne_room/clean.sh index 9fe15313..9063a26c 100644 --- a/missions/permissions/03_chmod_rw_file_dir_throne_room/clean.sh +++ b/missions/permissions/03_chmod_rw_file_dir_throne_room/clean.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh safe="$(eval_gettext '$GSH_HOME/Castle/Main_building/Throne_room')/$(gettext "Safe")" @@ -19,5 +19,5 @@ esac # avoid problems when making a tar archive chmod 755 "$safe" chmod -f 644 "$safe/$(gettext "crown")" 2>/dev/null -rm -f "$GSH_VAR/crown" +rm -f "$GSH_TMP/crown" unset safe diff --git a/missions/permissions/03_chmod_rw_file_dir_throne_room/init.sh b/missions/permissions/03_chmod_rw_file_dir_throne_room/init.sh index d1d0c37a..17295e1c 100644 --- a/missions/permissions/03_chmod_rw_file_dir_throne_room/init.sh +++ b/missions/permissions/03_chmod_rw_file_dir_throne_room/init.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh [ -z "$GSH_CHEST" ] && GSH_CHEST="$(eval_gettext '$GSH_HOME/Forest/Hut/Chest')" mkdir -p "$GSH_CHEST" @@ -7,11 +7,11 @@ safe="$(eval_gettext '$GSH_HOME/Castle/Main_building/Throne_room')/$(gettext "Sa mkdir -p "$safe" chmod 755 "$safe" chmod 644 "$safe/$(gettext "crown")" 2> /dev/null -k=$(printf "000$(($RANDOM % 1000))" | tail -c3) +k=$(printf "%03d" "$(($(RANDOM) % 1000))") sed "s/KEY/$k/" "$MISSION_DIR/ascii-art/crown.txt" > "$safe/$(gettext "crown")" -cp "$safe/$(gettext "crown")" "$GSH_VAR"/crown +cp "$safe/$(gettext "crown")" "$GSH_TMP"/crown chmod 000 "$safe/$(gettext "crown")" chmod 000 "$safe" diff --git a/missions/permissions/03_chmod_rw_file_dir_throne_room/static.sh b/missions/permissions/03_chmod_rw_file_dir_throne_room/static.sh index 22fec313..29c04fb4 100644 --- a/missions/permissions/03_chmod_rw_file_dir_throne_room/static.sh +++ b/missions/permissions/03_chmod_rw_file_dir_throne_room/static.sh @@ -1,3 +1,3 @@ -#!/bin/bash +#!/bin/sh mkdir -p "$(eval_gettext '$GSH_HOME/Castle/Main_building/Throne_room')" diff --git a/missions/permissions/03_chmod_rw_file_dir_throne_room/test.sh b/missions/permissions/03_chmod_rw_file_dir_throne_room/test.sh index 77d2ae15..3cd9ee0e 100644 --- a/missions/permissions/03_chmod_rw_file_dir_throne_room/test.sh +++ b/missions/permissions/03_chmod_rw_file_dir_throne_room/test.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh gsh assert check false @@ -15,13 +15,13 @@ safe_dir="$(eval_gettext '$GSH_HOME/Castle/Main_building/Throne_room')/$(gettext chmod 755 "$safe_dir" chmod 644 "$safe_dir/$(gettext "crown")" mv -f "$safe_dir/$(gettext "crown")" "$GSH_CHEST" -gsh assert check false < <(echo "1234") +echo "1234" | gsh assert check false safe_dir="$(eval_gettext '$GSH_HOME/Castle/Main_building/Throne_room')/$(gettext "Safe")" chmod 755 "$safe_dir" chmod 644 "$safe_dir/$(gettext "crown")" mv -f "$safe_dir/$(gettext "crown")" "$GSH_CHEST" key=$(tail -n 1 "$GSH_CHEST/$(gettext "crown")" | cut -c 4-6) -gsh assert check true < <(echo "$key") +echo "$key" | gsh assert check true unset safe_dir key diff --git a/missions/pipe_intro_book_of_potions/00_shared/clean.sh b/missions/pipe_intro_book_of_potions/00_shared/clean.sh index c28c9df1..66ad3122 100644 --- a/missions/pipe_intro_book_of_potions/00_shared/clean.sh +++ b/missions/pipe_intro_book_of_potions/00_shared/clean.sh @@ -1,4 +1,6 @@ +#!/bin/sh + rm -f "$(eval_gettext '$GSH_HOME/Mountain/Cave')/servillus" sign_file "$MISSION_DIR/ascii-art/cauldron.txt" "$(eval_gettext '$GSH_HOME/Mountain/Cave')/$(gettext "cauldron")" -rm -rf "$GSH_VAR/book_of_potions" +rm -rf "$GSH_TMP/book_of_potions" diff --git a/missions/pipe_intro_book_of_potions/00_shared/init.sh b/missions/pipe_intro_book_of_potions/00_shared/init.sh index d293281e..58153c85 100644 --- a/missions/pipe_intro_book_of_potions/00_shared/init.sh +++ b/missions/pipe_intro_book_of_potions/00_shared/init.sh @@ -1,9 +1,9 @@ -#!/bin/bash +#!/bin/sh -_mission_init() { - if ! command -v install_potion_book.sh &> /dev/null +_mission_init() ( + if ! command -v install_potion_book.sh >/dev/null then - local DUMMY_MISSION=$(missionname ../00_shared) + DUMMY_MISSION=$(missionname ../00_shared) echo "$(eval_gettext "Dummy mission '\$DUMMY_MISSION' is required for mission \$MISSION_NB (\$MISSION_NAME).")" >&2 return 1 fi @@ -12,13 +12,13 @@ _mission_init() { install_potion_book.sh book "$(eval_gettext '$GSH_HOME/Mountain/Cave')/" # Install a copy for later checks. TODO: only do that in the check file? - install_potion_book.sh book "$GSH_VAR/book_of_potions" + install_potion_book.sh book "$GSH_TMP/book_of_potions" # put Servillus in the cave rm -f "$(eval_gettext '$GSH_HOME/Mountain/Cave')/$(gettext "cauldron")" sign_file "$MISSION_DIR/../00_shared/ascii-art/servillus.txt" "$(eval_gettext '$GSH_HOME/Mountain/Cave')/servillus" return 0 -} +) _mission_init diff --git a/missions/pipe_intro_book_of_potions/00_shared/sbin/install_potion_book.sh b/missions/pipe_intro_book_of_potions/00_shared/sbin/install_potion_book.sh index 10ef9e27..33d851cf 100755 --- a/missions/pipe_intro_book_of_potions/00_shared/sbin/install_potion_book.sh +++ b/missions/pipe_intro_book_of_potions/00_shared/sbin/install_potion_book.sh @@ -1,6 +1,6 @@ -#!/usr/bin/env bash +#!/bin/sh -source gettext.sh +. gettext.sh # Checking for the arguments. if [ "$#" -ne 2 ] @@ -21,17 +21,20 @@ fi # Takes installation directory as argument (with or without final '/'). install_book() { - local BOOK - BOOK="$1" - if [[ "$BOOK" == */ ]] - then - BOOK="$BOOK$(gettext "Book_of_potions")" - fi - + # if the install dir is given with a trailing '/', add the localized name at + # the end + case "$1" in + */) + BOOK="${1%%/}/$(gettext "Book_of_potions")" + ;; + *) + BOOK=$1 + ;; + esac mkdir -p "$BOOK" + # NOTE: option --supress-matched doesn't exist in freebsd, nor does the "{*}" repetition - csplit -ks -f "$BOOK/$(gettext "page")_" "$(eval_gettext '$MISSION_DIR/book_of_potions/en.txt')" "/^==.*/" "{99}" 2> /dev/null - local f + csplit -ks -f "$BOOK/$(gettext "page")_" "$(eval_gettext '$MISSION_DIR/book_of_potions/en.txt')" "/^==.*/" "{99}" 2>/dev/null for f in "$BOOK/$(gettext "page")_"* do sed-i "/^==.*/d" "$f" @@ -42,7 +45,6 @@ install_book() { # Takes destination file as argument. install_page5() { - local SRC SRC="$(eval_gettext '$MISSION_DIR/torn_page5/en.txt')" mkdir -p "$(basename "$1")" cp "$SRC" "$1" @@ -50,7 +52,6 @@ install_page5() { # Takes destination file as argument. install_page6() { - local SRC SRC="$(eval_gettext '$MISSION_DIR/torn_page6/en.txt')" mkdir -p "$(basename "$1")" cp "$SRC" "$1" @@ -63,8 +64,8 @@ case $1 in "page5") install_page5 "$2" ;; - "page5") - install_page5 "$2" + "page6") + install_page6 "$2" ;; *) echo "The first argument must be 'book', 'page5' or 'page6'." diff --git a/missions/pipe_intro_book_of_potions/00_shared/static.sh b/missions/pipe_intro_book_of_potions/00_shared/static.sh index 8748289d..13409884 100644 --- a/missions/pipe_intro_book_of_potions/00_shared/static.sh +++ b/missions/pipe_intro_book_of_potions/00_shared/static.sh @@ -1,2 +1,4 @@ +#!/bin/sh + mkdir -p "$(eval_gettext '$GSH_HOME/Mountain/Cave')" sign_file "$MISSION_DIR/ascii-art/cauldron.txt" "$(eval_gettext '$GSH_HOME/Mountain/Cave')/$(gettext "cauldron")" diff --git a/missions/pipe_intro_book_of_potions/01_head/auto.sh b/missions/pipe_intro_book_of_potions/01_head/auto.sh index bdfe0087..08500d78 100644 --- a/missions/pipe_intro_book_of_potions/01_head/auto.sh +++ b/missions/pipe_intro_book_of_potions/01_head/auto.sh @@ -1,3 +1,5 @@ +#!/bin/sh + cd "$(eval_gettext '$GSH_HOME/Mountain/Cave')" # commands from a sourced file aren't saved in the history, diff --git a/missions/pipe_intro_book_of_potions/01_head/check.sh b/missions/pipe_intro_book_of_potions/01_head/check.sh index 57d4a87f..2bc56292 100644 --- a/missions/pipe_intro_book_of_potions/01_head/check.sh +++ b/missions/pipe_intro_book_of_potions/01_head/check.sh @@ -1,48 +1,41 @@ -#!/bin/bash +#!/bin/sh -_mission_check() { - local cave - cave="$(eval_gettext '$GSH_HOME/Mountain/Cave')" +_mission_check() ( + cave="$(eval_gettext '$GSH_HOME/Mountain/Cave')" - local pc - pc=$(fc -nl -1 -1) + pc=$(fc -nl -1 -1) - local goal - goal=$(realpath "$cave") - local current - current=$(realpath "$PWD") + goal=$(realpath "$cave") + current=$(realpath "$PWD") - if ! diff -q "$cave/$(gettext 'Book_of_potions')" "$GSH_VAR/book_of_potions" > /dev/null - then - echo "$(gettext "You altered the book...")" + if ! diff -q "$cave/$(gettext 'Book_of_potions')" "$GSH_TMP/book_of_potions" >/dev/null + then + echo "$(gettext "You altered the book...")" + return 1 + fi + + if [ "$goal" != "$current" ] + then + echo "$(gettext "You are not in the cave with Servillus!")" + return 1 + fi + if ! echo "$pc" | grep -q 'head' + then + echo "$(gettext "You have not used the 'head' command!")" return 1 - fi - - if [ "$goal" != "$current" ] - then - echo "$(gettext "You are not in the cave with Servillus!")" - return 1 - fi - if [ -z "$(echo "$pc" | grep 'head')" ] - then - echo "$(gettext "You have not used the 'head' command!")" - return 1 - fi - - echo $pc | grep 'gsh\s\s*check' && return 1 - - local expected - expected="$(head -n 6 "$GSH_VAR/book_of_potions/$(gettext 'page')_07")" - local res - res="$(eval "$pc")" - - if [ "$res" != "$expected" ] - then - echo "$(gettext "Your previous command does not give the expected result...")" - return 1 - fi - return 0 -} + fi + + echo "$pc" | grep -q 'gsh\s\s*check' && return 1 + expected="$(head -n 6 "$GSH_TMP/book_of_potions/$(gettext 'page')_07")" + res="$(eval "$pc")" + + if [ "$res" != "$expected" ] + then + echo "$(gettext "Your previous command does not give the expected result...")" + return 1 + fi + return 0 +) _mission_check diff --git a/missions/pipe_intro_book_of_potions/01_head/test.sh b/missions/pipe_intro_book_of_potions/01_head/test.sh index a38b4a52..b2ad6a7e 100644 --- a/missions/pipe_intro_book_of_potions/01_head/test.sh +++ b/missions/pipe_intro_book_of_potions/01_head/test.sh @@ -1,3 +1,7 @@ +#!/bin/bash + +# the ``history`` command is not in POSIX + cd "$(eval_gettext '$GSH_HOME/Mountain/Cave')" history -s "head -n 6 $(gettext "Book_of_potions")/$(gettext "page")_07" @@ -26,7 +30,7 @@ history -d $((n-1)) history -d $((n-1)) unset n -sed-i '1d' $(gettext "Book_of_potions")/$(gettext "page")_07 +sed-i '1d' "$(gettext "Book_of_potions")/$(gettext "page")_07" history -s "head -n 6 $(gettext "Book_of_potions")/$(gettext "page")_07" history -s "gsh check" gsh assert check false diff --git a/missions/pipe_intro_book_of_potions/02_tail/auto.sh b/missions/pipe_intro_book_of_potions/02_tail/auto.sh index 11407308..ed48e6a7 100644 --- a/missions/pipe_intro_book_of_potions/02_tail/auto.sh +++ b/missions/pipe_intro_book_of_potions/02_tail/auto.sh @@ -1,3 +1,7 @@ +#!/bin/bash + +# the ``history`` command is not in POSIX + cd "$(eval_gettext '$GSH_HOME/Mountain/Cave')" # commands from a sourced file aren't saved in the history, diff --git a/missions/pipe_intro_book_of_potions/02_tail/check.sh b/missions/pipe_intro_book_of_potions/02_tail/check.sh index 3fabc093..2f0913b4 100644 --- a/missions/pipe_intro_book_of_potions/02_tail/check.sh +++ b/missions/pipe_intro_book_of_potions/02_tail/check.sh @@ -1,47 +1,40 @@ -#!/bin/bash - -_mission_check() { - local cave - cave="$(eval_gettext '$GSH_HOME/Mountain/Cave')" - - local pc - pc=$(fc -nl -1 -1) - - local goal - goal=$(realpath "$cave") - local current - current=$(realpath "$PWD") - - if ! diff -q "$cave/$(gettext 'Book_of_potions')" "$GSH_VAR/book_of_potions" > /dev/null - then - echo "$(gettext "You altered the book...")" - return 1 - fi - - if [ "$goal" != "$current" ] - then - echo "$(gettext "You are not in the cave with Servillus!")" - return 1 - fi - if [ -z "$(echo "$pc" | grep 'tail')" ] - then - echo "$(gettext "You have not used the 'tail' command!")" - return 1 - fi - - echo $pc | grep 'gsh\s\s*check' && return 1 - - local expected - expected="$(tail -n 9 "$GSH_VAR/book_of_potions/$(gettext 'page')_12")" - local res - res="$(eval "$pc")" - if [ "$res" != "$expected" ] - then - echo "$(gettext "Your previous command does not give the expected result...")" - return 1 - fi - return 0 -} - +#!/bin/sh + +_mission_check() ( + cave="$(eval_gettext '$GSH_HOME/Mountain/Cave')" + + pc=$(fc -nl -1 -1) + + goal=$(realpath "$cave") + current=$(realpath "$PWD") + + if ! diff -q "$cave/$(gettext 'Book_of_potions')" "$GSH_TMP/book_of_potions" > /dev/null + then + echo "$(gettext "You altered the book...")" + return 1 + fi + + if [ "$goal" != "$current" ] + then + echo "$(gettext "You are not in the cave with Servillus!")" + return 1 + fi + if ! echo "$pc" | grep -q 'tail' + then + echo "$(gettext "You have not used the 'tail' command!")" + return 1 + fi + + echo "$pc" | grep -q 'gsh\s\s*check' && return 1 + + expected="$(tail -n 9 "$GSH_TMP/book_of_potions/$(gettext 'page')_12")" + res="$(eval "$pc")" + if [ "$res" != "$expected" ] + then + echo "$(gettext "Your previous command does not give the expected result...")" + return 1 + fi + return 0 +) _mission_check diff --git a/missions/pipe_intro_book_of_potions/02_tail/test.sh b/missions/pipe_intro_book_of_potions/02_tail/test.sh index bf7322cd..87d9c7fa 100644 --- a/missions/pipe_intro_book_of_potions/02_tail/test.sh +++ b/missions/pipe_intro_book_of_potions/02_tail/test.sh @@ -1,3 +1,7 @@ +#!/bin/bash + +# the ``history`` command is not in POSIX + cd "$(eval_gettext '$GSH_HOME/Mountain/Cave')" history -s "tail -n 9 $(gettext "Book_of_potions")/$(gettext "page")_12" @@ -34,7 +38,7 @@ history -d $((n-1)) history -d $((n-1)) unset n -sed-i '1d' $(gettext "Book_of_potions")/$(gettext "page")_12 +sed-i '1d' "$(gettext "Book_of_potions")/$(gettext "page")_12" history -s "tail -n 9 $(gettext "Book_of_potions")/$(gettext "page")_12" history -s "gsh check" gsh assert check false diff --git a/missions/pipe_intro_book_of_potions/03_cat/auto.sh b/missions/pipe_intro_book_of_potions/03_cat/auto.sh index 09a351e5..91adf2e3 100644 --- a/missions/pipe_intro_book_of_potions/03_cat/auto.sh +++ b/missions/pipe_intro_book_of_potions/03_cat/auto.sh @@ -1,3 +1,7 @@ +#!/bin/bash + +# the ``history`` command is not in POSIX + cd "$(eval_gettext '$GSH_HOME/Mountain/Cave')" # commands from a sourced file aren't saved in the history, diff --git a/missions/pipe_intro_book_of_potions/03_cat/check.sh b/missions/pipe_intro_book_of_potions/03_cat/check.sh index f1726122..f1229eba 100644 --- a/missions/pipe_intro_book_of_potions/03_cat/check.sh +++ b/missions/pipe_intro_book_of_potions/03_cat/check.sh @@ -1,47 +1,40 @@ -#!/bin/bash - -_mission_check() { - local cave - cave="$(eval_gettext '$GSH_HOME/Mountain/Cave')" - - local pc - pc=$(fc -nl -1 -1) - - local goal - goal=$(realpath "$cave") - local current - current=$(realpath "$PWD") - - if ! diff -q "$cave/$(gettext 'Book_of_potions')" "$GSH_VAR/book_of_potions" > /dev/null - then - echo "$(gettext "You altered the book...")" - return 1 - fi - - if [ "$goal" != "$current" ] - then - echo "$(gettext "You are not in the cave with Servillus!")" - return 1 - fi - if [ -z "$(echo "$pc" | grep 'cat')" ] - then - echo "$(gettext "You have not used the 'cat' command!")" - return 1 - fi - - echo $pc | grep 'gsh\s\s*check' && return 1 - - local expected - expected="$(cat "$GSH_VAR/book_of_potions/$(gettext 'page')_0"[12])" - local res - res="$(eval "$pc")" - if [ "$res" != "$expected" ] - then - echo "$(gettext "Your previous command does not give the expected result...")" - return 1 - fi - return 0 -} - +#!/bin/sh + +_mission_check() ( + cave="$(eval_gettext '$GSH_HOME/Mountain/Cave')" + + pc=$(fc -nl -1 -1) + + goal=$(realpath "$cave") + current=$(realpath "$PWD") + + if ! diff -q "$cave/$(gettext 'Book_of_potions')" "$GSH_TMP/book_of_potions" > /dev/null + then + echo "$(gettext "You altered the book...")" + return 1 + fi + + if [ "$goal" != "$current" ] + then + echo "$(gettext "You are not in the cave with Servillus!")" + return 1 + fi + if ! echo "$pc" | grep -q 'cat' + then + echo "$(gettext "You have not used the 'cat' command!")" + return 1 + fi + + echo "$pc" | grep -q 'gsh\s\s*check' && return 1 + + expected="$(cat "$GSH_TMP/book_of_potions/$(gettext 'page')_0"[12])" + res="$(eval "$pc")" + if [ "$res" != "$expected" ] + then + echo "$(gettext "Your previous command does not give the expected result...")" + return 1 + fi + return 0 +) _mission_check diff --git a/missions/pipe_intro_book_of_potions/03_cat/test.sh b/missions/pipe_intro_book_of_potions/03_cat/test.sh index 125c19cd..cb129c5b 100644 --- a/missions/pipe_intro_book_of_potions/03_cat/test.sh +++ b/missions/pipe_intro_book_of_potions/03_cat/test.sh @@ -1,3 +1,7 @@ +#!/bin/bash + +# the ``history`` command is not in POSIX + cd "$(eval_gettext '$GSH_HOME/Mountain/Cave')" history -s "cat \"$(gettext "Book_of_potions")/$(gettext "page")_0\"[12]" @@ -42,7 +46,7 @@ history -d $((n-1)) history -d $((n-1)) unset n -sed-i '1d' $(gettext "Book_of_potions")/$(gettext "page")_01 +sed-i '1d' "$(gettext "Book_of_potions")/$(gettext "page")_01" history -s "cat \"$(gettext "Book_of_potions")/$(gettext "page")_0\"[12]" history -s "gsh check" gsh assert check false diff --git a/missions/pipe_intro_book_of_potions/04_pipe/auto.sh b/missions/pipe_intro_book_of_potions/04_pipe/auto.sh index f908830d..b2b861e5 100644 --- a/missions/pipe_intro_book_of_potions/04_pipe/auto.sh +++ b/missions/pipe_intro_book_of_potions/04_pipe/auto.sh @@ -1,3 +1,7 @@ +#!/bin/bash + +# the ``history` command is not in POSIX + cd "$(eval_gettext '$GSH_HOME/Mountain/Cave')" # commands from a sourced file aren't saved in the history, diff --git a/missions/pipe_intro_book_of_potions/04_pipe/check.sh b/missions/pipe_intro_book_of_potions/04_pipe/check.sh index 5570e45c..d2df5ad2 100644 --- a/missions/pipe_intro_book_of_potions/04_pipe/check.sh +++ b/missions/pipe_intro_book_of_potions/04_pipe/check.sh @@ -1,47 +1,40 @@ -#!/bin/bash - -_mission_check() { - local cave - cave="$(eval_gettext '$GSH_HOME/Mountain/Cave')" - - local pc - pc=$(fc -nl -1 -1) - - local goal - goal=$(realpath "$cave") - local current - current=$(realpath "$PWD") - - if ! diff -q "$cave/$(gettext 'Book_of_potions')" "$GSH_VAR/book_of_potions" > /dev/null - then - echo "$(gettext "You altered the book...")" - return 1 - fi - - if [ "$goal" != "$current" ] - then - echo "$(gettext "You are not in the cave with Servillus!")" - return 1 - fi - if [ -z "$(echo "$pc" | grep 'tail')" ] - then - echo "$(gettext "You have not used the 'tail' command!")" - return 1 - fi - - echo $pc | grep 'gsh\s\s*check' && return 1 - - local expected - expected="$(cat "$GSH_VAR/book_of_potions/$(gettext 'page')_0"[34] | tail -n 16)" - local res - res="$(eval "$pc")" - if [ "$res" != "$expected" ] - then - echo "$(gettext "Your previous command does not give the expected result...")" - return 1 - fi - return 0 -} - +#!/bin/sh + +_mission_check() ( + cave="$(eval_gettext '$GSH_HOME/Mountain/Cave')" + + pc=$(fc -nl -1 -1) + + goal=$(realpath "$cave") + current=$(realpath "$PWD") + + if ! diff -q "$cave/$(gettext 'Book_of_potions')" "$GSH_TMP/book_of_potions" > /dev/null + then + echo "$(gettext "You altered the book...")" + return 1 + fi + + if [ "$goal" != "$current" ] + then + echo "$(gettext "You are not in the cave with Servillus!")" + return 1 + fi + if ! echo "$pc" | grep -q 'tail' + then + echo "$(gettext "You have not used the 'tail' command!")" + return 1 + fi + + echo "$pc" | grep -q 'gsh\s\s*check' && return 1 + + expected="$(cat "$GSH_TMP/book_of_potions/$(gettext 'page')_0"[34] | tail -n 16)" + res="$(eval "$pc")" + if [ "$res" != "$expected" ] + then + echo "$(gettext "Your previous command does not give the expected result...")" + return 1 + fi + return 0 +) _mission_check diff --git a/missions/pipe_intro_book_of_potions/04_pipe/test.sh b/missions/pipe_intro_book_of_potions/04_pipe/test.sh index 3688a372..0f6c2506 100644 --- a/missions/pipe_intro_book_of_potions/04_pipe/test.sh +++ b/missions/pipe_intro_book_of_potions/04_pipe/test.sh @@ -1,3 +1,7 @@ +#!/bin/bash + +# the ``history`` command is not in POSIX + cd "$(eval_gettext '$GSH_HOME/Mountain/Cave')" history -s "cat \"$(gettext "Book_of_potions")/$(gettext 'page')_0\"[34] | tail -n 16" @@ -26,7 +30,7 @@ history -d $((n-2)) history -d $((n-2)) unset n -sed-i '1d' $(gettext "Book_of_potions")/$(gettext "page")_01 +sed-i '1d' "$(gettext "Book_of_potions")/$(gettext "page")_01" history -s "cat \"$(gettext "Book_of_potions")/$(gettext "page")_0\"[34] | tail -n 16" history -s "gsh check" gsh assert check false diff --git a/missions/pipe_intro_book_of_potions/05_pipe_head_tail/auto.sh b/missions/pipe_intro_book_of_potions/05_pipe_head_tail/auto.sh index 49f99fc6..577785ce 100644 --- a/missions/pipe_intro_book_of_potions/05_pipe_head_tail/auto.sh +++ b/missions/pipe_intro_book_of_potions/05_pipe_head_tail/auto.sh @@ -1,3 +1,7 @@ +#!/bin/bash + +# the ``history`` command is not in POSIX + cd "$(eval_gettext '$GSH_HOME/Mountain/Cave')" # commands from a sourced file aren't saved in the history, diff --git a/missions/pipe_intro_book_of_potions/05_pipe_head_tail/check.sh b/missions/pipe_intro_book_of_potions/05_pipe_head_tail/check.sh index 9f9dc05c..fe7103d0 100644 --- a/missions/pipe_intro_book_of_potions/05_pipe_head_tail/check.sh +++ b/missions/pipe_intro_book_of_potions/05_pipe_head_tail/check.sh @@ -1,50 +1,43 @@ -#!/bin/bash - -_mission_check() { - local cave - cave="$(eval_gettext '$GSH_HOME/Mountain/Cave')" - - local pc - pc=$(fc -nl -1 -1) - - local goal - goal=$(realpath "$cave") - local current - current=$(realpath "$PWD") - - if ! diff -q "$cave/$(gettext 'Book_of_potions')" "$GSH_VAR/book_of_potions" > /dev/null - then - echo "$(gettext "You altered the book...")" - return 1 - fi - - if [ "$goal" != "$current" ] - then - echo "$(gettext "You are not in the cave with Servillus!")" - return 1 - fi - if [ -n "$(echo "$pc" | grep -E 'awk|sed')" ] - then - echo "$(gettext "If you know awk or sed, what are you doing playing GameShell?")" - elif [ -z "$(echo "$pc" | grep 'tail' | grep 'head')" ] - then - echo "$(gettext "You have not used a combination of 'head' and 'tail'!")" - return 1 - fi - - echo $pc | grep 'gsh\s\s*check' && return 1 - - local expected - expected="$(head -n 6 "$GSH_VAR/book_of_potions/$(gettext 'page')_13" | tail -n 3)" - local res - res="$(eval "$pc")" - if [ "$res" != "$expected" ] - then - echo "$(gettext "Your previous command does not give the expected result...")" - return 1 - fi - return 0 -} - +#!/bin/sh + +_mission_check() ( + cave="$(eval_gettext '$GSH_HOME/Mountain/Cave')" + + pc=$(fc -nl -1 -1) + + goal=$(realpath "$cave") + current=$(realpath "$PWD") + + if ! diff -q "$cave/$(gettext 'Book_of_potions')" "$GSH_TMP/book_of_potions" > /dev/null + then + echo "$(gettext "You altered the book...")" + return 1 + fi + + if [ "$goal" != "$current" ] + then + echo "$(gettext "You are not in the cave with Servillus!")" + return 1 + fi + if echo "$pc" | grep -Eq 'awk|sed' + then + echo "$(gettext "If you know awk or sed, what are you doing playing GameShell?")" + elif [ -z "$(echo "$pc" | grep 'tail' | grep 'head')" ] + then + echo "$(gettext "You have not used a combination of 'head' and 'tail'!")" + return 1 + fi + + echo "$pc" | grep -q 'gsh\s\s*check' && return 1 + + expected="$(head -n 6 "$GSH_TMP/book_of_potions/$(gettext 'page')_13" | tail -n 3)" + res="$(eval "$pc")" + if [ "$res" != "$expected" ] + then + echo "$(gettext "Your previous command does not give the expected result...")" + return 1 + fi + return 0 +) _mission_check diff --git a/missions/pipe_intro_book_of_potions/05_pipe_head_tail/test.sh b/missions/pipe_intro_book_of_potions/05_pipe_head_tail/test.sh index bea5ae12..e0cee1c4 100644 --- a/missions/pipe_intro_book_of_potions/05_pipe_head_tail/test.sh +++ b/missions/pipe_intro_book_of_potions/05_pipe_head_tail/test.sh @@ -1,3 +1,7 @@ +#!/bin/bash + +# the ``history`` command is not in POSIX + cd "$(eval_gettext '$GSH_HOME/Mountain/Cave')" history -s "head -n 6 \"$(gettext "Book_of_potions")/$(gettext 'page')_13\" | tail -n 3" @@ -50,7 +54,7 @@ history -d $((n-2)) history -d $((n-2)) unset n -sed-i '1d' $(gettext "Book_of_potions")/$(gettext "page")_01 +sed-i '1d' "$(gettext "Book_of_potions")/$(gettext "page")_01" history -s "head -n 6 \"$(gettext "Book_of_potions")/$(gettext 'page')_13\" | tail -n 3" history -s "gsh check" gsh assert check false diff --git a/missions/pipes_merchant_stall/00_shared/clean.sh b/missions/pipes_merchant_stall/00_shared/clean.sh index e144ac69..b315d9b1 100644 --- a/missions/pipes_merchant_stall/00_shared/clean.sh +++ b/missions/pipes_merchant_stall/00_shared/clean.sh @@ -1,3 +1,5 @@ +#!/bin/sh + # need to be careful, as there are so many files in the Stall that # rm .../* may not work. ( @@ -5,10 +7,10 @@ find . -type f | xargs rm -f ) -rm -f "$GSH_VAR/nb_commands" "$GSH_VAR/last_command" +rm -f "$GSH_TMP/nb_commands" "$GSH_TMP/last_command" -PROMPT_COMMAND=$OLD_PROMPT_COMMAND -unset OLD_PROMPT_COMMAND +PS1=$_PS1 +unset _PS1 unset -f _cmd -rm -f "$GSH_VAR/nbUnpaid" "$GSH_VAR/amountKing" +rm -f "$GSH_TMP/nbUnpaid" "$GSH_TMP/amountKing" diff --git a/missions/pipes_merchant_stall/00_shared/count_commands.sh b/missions/pipes_merchant_stall/00_shared/count_commands.sh index 1c632d0a..729ed18b 100644 --- a/missions/pipes_merchant_stall/00_shared/count_commands.sh +++ b/missions/pipes_merchant_stall/00_shared/count_commands.sh @@ -1,33 +1,28 @@ -echo 0 > "$GSH_VAR/nb_commands" -history -a -fc -l | tail -n 1 | awk '{print $1}' > "$GSH_VAR/last_command" +#!/bin/sh -_cmd() { - touch "$HISTFILE" - history -a +# NOTE: debian's sh (dash) is compiled without fc, so this won't work! + +echo 0 > "$GSH_TMP/nb_commands" +fc -l | tail -n 1 | awk '{print $1}' > "$GSH_TMP/last_command" - [ -e "$GSH_VAR/nb_commands" ] || return +_count_cmd() ( + touch "$HISTFILE" + [ -e "$GSH_TMP/nb_commands" ] || return - local last_cmd_1=$(fc -l | tail -n 1 | awk '{print $1}') - local last_cmd_2=$(cat "$GSH_VAR/last_command") - echo "$last_cmd_1" > "$GSH_VAR/last_command" + last_cmd_1=$(fc -l | tail -n 1 | awk '{print $1}') + last_cmd_2=$(cat "$GSH_TMP/last_command") + echo "$last_cmd_1" > "$GSH_TMP/last_command" - local pc=$(tail -n1 "$HISTFILE") - local nb_cmd=$(cat "$GSH_VAR/nb_commands") + pc=$(fc -l | tail -n 1 | awk '{print $2}') + nb_cmd=$(cat "$GSH_TMP/nb_commands") - if [ "$last_cmd_1" != "$last_cmd_2" ] && [ -n "$pc" ] && ! echo "$pc" | grep -x "\s*gsh\s*\w*\s*" &> /dev/null + if [ "$last_cmd_1" != "$last_cmd_2" ] && [ -n "$pc" ] && ! echo "$pc" | grep -x '\s*gsh\s*\w*\s*' &>/dev/null then nb_cmd=$((nb_cmd+1)) - echo $nb_cmd > "$GSH_VAR/nb_commands" + echo $nb_cmd > "$GSH_TMP/nb_commands" fi - echo "($nb_cmd)" -} -export -f _cmd + echo "($nb_cmd) " +) -PROMPT_COMMAND=$(echo "$PROMPT_COMMAND" | sed 's|[[:blank:]]*;\?[[:blank:]]*_cmd.*||') -if [ -z "$PROMPT_COMMAND" ] -then - PROMPT_COMMAND="_cmd" -else - PROMPT_COMMAND="$PROMPT_COMMAND;_cmd" -fi +_PS1=$PS1 +PS1='$(_count_cmd)'$PS1 diff --git a/missions/pipes_merchant_stall/00_shared/sbin/generate_merchant_stall.awk b/missions/pipes_merchant_stall/00_shared/sbin/generate_merchant_stall.awk index 9afd8e2a..33dce51e 100644 --- a/missions/pipes_merchant_stall/00_shared/sbin/generate_merchant_stall.awk +++ b/missions/pipes_merchant_stall/00_shared/sbin/generate_merchant_stall.awk @@ -84,10 +84,10 @@ END { filename = dir "/" scroll; printf(transaction_template "\n", name, o, price, p) >> filename; } - filename = ENVIRON["GSH_VAR"]"/nbUnpaid"; + filename = ENVIRON["GSH_TMP"]"/nbUnpaid"; print nbUnpaid > filename; close(filename); - filename = ENVIRON["GSH_VAR"]"/amountKing"; + filename = ENVIRON["GSH_TMP"]"/amountKing"; print amountKing > filename; close(filename); diff --git a/missions/pipes_merchant_stall/00_shared/sbin/generate_merchant_stall.sh b/missions/pipes_merchant_stall/00_shared/sbin/generate_merchant_stall.sh index 478279c8..52454de6 100755 --- a/missions/pipes_merchant_stall/00_shared/sbin/generate_merchant_stall.sh +++ b/missions/pipes_merchant_stall/00_shared/sbin/generate_merchant_stall.sh @@ -23,11 +23,11 @@ awk -v dir="$dir" \ -v transaction_template="$transaction_template" \ -v nb_transactions=$nb_transactions \ -v nb_boring_objects=$nb_boring_objects \ - -v s_scroll=$s_scroll \ - -v s_boring=$s_boring \ - -v s_paid=$s_paid \ - -v nb_king=$nb_king \ - -v proba_paid=$proba_paid \ + -v s_scroll="$s_scroll" \ + -v s_boring="$s_boring" \ + -v s_paid="$s_paid" \ + -v nb_king="$nb_king" \ + -v proba_paid="$proba_paid" \ -v seed_file="$GSH_CONFIG/PRNG_seed" \ -f "$MISSION_DIR/sbin/generate_merchant_stall.awk" \ "$firstname_file" "$lastname_file" "$object_file" diff --git a/missions/pipes_merchant_stall/00_shared/static.sh b/missions/pipes_merchant_stall/00_shared/static.sh index e082e221..1fc57674 100644 --- a/missions/pipes_merchant_stall/00_shared/static.sh +++ b/missions/pipes_merchant_stall/00_shared/static.sh @@ -1 +1,3 @@ +#!/bin/sh + mkdir -p "$(eval_gettext '$GSH_HOME/Stall')" diff --git a/missions/pipes_merchant_stall/01_pipe_1/auto.sh b/missions/pipes_merchant_stall/01_pipe_1/auto.sh index 427191c4..e7e320e7 100644 --- a/missions/pipes_merchant_stall/01_pipe_1/auto.sh +++ b/missions/pipes_merchant_stall/01_pipe_1/auto.sh @@ -1,3 +1,7 @@ +#!/bin/bash +# +# FIXME? I use bash' process substitution to avoid creating a subshell + # beware, there are so many files that we may get "bash: /usr/bin/grep: Argument list too long" # so we use recursive grep on the diectory instead of globbing gsh check < <(grep -r "$(gettext "the King")" "$(eval_gettext '$GSH_HOME/Stall')" | diff --git a/missions/pipes_merchant_stall/01_pipe_1/check.sh b/missions/pipes_merchant_stall/01_pipe_1/check.sh index 3aff3b93..ae211da8 100644 --- a/missions/pipes_merchant_stall/01_pipe_1/check.sh +++ b/missions/pipes_merchant_stall/01_pipe_1/check.sh @@ -1,10 +1,10 @@ -#!/bin/bash +#!/bin/sh -_mission_check() { - local amount=$(cat "$GSH_VAR/amountKing") - local response - read -erp "$(gettext "How much does the king owe?") " response - local nb_cmd=$(cat "$GSH_VAR/nb_commands") +_mission_check() ( + amount=$(cat "$GSH_TMP/amountKing") + printf "%s " "$(gettext "How much does the king owe?")" + read -r response + nb_cmd=$(cat "$GSH_TMP/nb_commands") # TODO, check the "boring objects" are still here, to avoid the possibility # rm *boring* @@ -20,6 +20,6 @@ _mission_check() { return 1 fi return 0 -} +) _mission_check diff --git a/missions/pipes_merchant_stall/01_pipe_1/init.sh b/missions/pipes_merchant_stall/01_pipe_1/init.sh index ddd27be1..adb72a5b 100644 --- a/missions/pipes_merchant_stall/01_pipe_1/init.sh +++ b/missions/pipes_merchant_stall/01_pipe_1/init.sh @@ -1,7 +1,7 @@ -#!/bin/bash +#!/bin/sh _mission_init() { - if ! command -v generate_merchant_stall.sh &> /dev/null + if ! command -v generate_merchant_stall.sh >/dev/null then # FIXME: change message echo "$(eval_gettext "The script 'generate_merchant_stall.sh' is necessary for mission \$MISSION_NAME. @@ -15,4 +15,5 @@ Make sure the corresponding mission is included.")" >&2 return 0 } + _mission_init diff --git a/missions/pipes_merchant_stall/02_pipe_2/auto.sh b/missions/pipes_merchant_stall/02_pipe_2/auto.sh index 096cc2ca..4eaf09aa 100644 --- a/missions/pipes_merchant_stall/02_pipe_2/auto.sh +++ b/missions/pipes_merchant_stall/02_pipe_2/auto.sh @@ -1,3 +1,7 @@ +#!/bin/bash + +# FIXME? I use bash' process substitution to avoid creating a subshell + # beware, there are so many files that we may get "bash: /usr/bin/grep: Argument list too long" -# so we use recursive grep on the diectory instead of globbing +# so we use recursive grep on the directory instead of globbing gsh check < <(grep -rv "$(gettext "PAID")" "$(eval_gettext '$GSH_HOME/Stall')" | wc -l) diff --git a/missions/pipes_merchant_stall/02_pipe_2/check.sh b/missions/pipes_merchant_stall/02_pipe_2/check.sh index 9a0d5a13..e9c9ddf2 100644 --- a/missions/pipes_merchant_stall/02_pipe_2/check.sh +++ b/missions/pipes_merchant_stall/02_pipe_2/check.sh @@ -1,10 +1,10 @@ -#!/bin/bash +#!/bin/sh -_mission_check() { - local nb_unpaid=$(cat "$GSH_VAR/nbUnpaid") - local response - read -erp "$(gettext "How many unpaid items are there?") " response - nb_cmd=$(cat "$GSH_VAR/nb_commands") +_mission_check() ( + nb_unpaid=$(cat "$GSH_TMP/nbUnpaid") + printf "%s " "$(gettext "How many unpaid items are there?")" + read -r response + nb_cmd=$(cat "$GSH_TMP/nb_commands") # TODO, check the "boring objects" are still here, to avoid the possibility # rm *boring* @@ -20,6 +20,6 @@ _mission_check() { return 1 fi return 0 -} +) _mission_check diff --git a/missions/pipes_merchant_stall/02_pipe_2/init.sh b/missions/pipes_merchant_stall/02_pipe_2/init.sh index c65887aa..546a505d 100644 --- a/missions/pipes_merchant_stall/02_pipe_2/init.sh +++ b/missions/pipes_merchant_stall/02_pipe_2/init.sh @@ -1,7 +1,7 @@ -#!/bin/bash +#!/bin/sh _mission_init() { - if ! command -v generate_merchant_stall.sh &> /dev/null + if ! command -v generate_merchant_stall.sh >/dev/null then # FIXME: change message echo "$(eval_gettext "The script 'generate_merchant_stall.sh' is necessary for mission \$MISSION_NAME. @@ -15,5 +15,6 @@ Make sure the corresponding mission is included.")" >&2 return 0 } + _mission_init diff --git a/missions/processes/00_shared/init.sh b/missions/processes/00_shared/init.sh index 2b01a51c..c02421c0 100644 --- a/missions/processes/00_shared/init.sh +++ b/missions/processes/00_shared/init.sh @@ -3,9 +3,9 @@ # use a subshell, that should guarantee that no message is displayed when the # test-proc-name process is killed. _mission_init() ( - cp "$MISSION_DIR/test-proc-name.sh" "$GSH_VAR/test-proc-name" - chmod +x "$GSH_VAR/test-proc-name" - "$GSH_VAR/test-proc-name" & + cp "$MISSION_DIR/test-proc-name.sh" "$GSH_TMP/test-proc-name" + chmod +x "$GSH_TMP/test-proc-name" + "$GSH_TMP/test-proc-name" & PID=$! name=$(ps -cp $PID | grep $PID | grep -v sh) kill -9 $PID @@ -13,7 +13,7 @@ _mission_init() ( then echo "$(eval_gettext "Process names should be equal to the corresponding filename for mission \$MISSION_NAME.")" >&2 return 1 - elif ! command -v ps &> /dev/null + elif ! command -v ps >/dev/null then echo "$(eval_gettext "The command 'ps' is required for mission \$MISSION_NAME. (Debian / Ubuntu: install package 'psproc')")" diff --git a/missions/processes/00_shared/my_ps-linux b/missions/processes/00_shared/my_ps-linux new file mode 100755 index 00000000..5006858f --- /dev/null +++ b/missions/processes/00_shared/my_ps-linux @@ -0,0 +1,9 @@ +#!/bin/sh + +if [ -z "$*" ] +then + ps -o pid,ppid,comm +else + ps -o pid,ppid,comm -p $* +fi + diff --git a/missions/processes/00_shared/my_ps-macos b/missions/processes/00_shared/my_ps-macos new file mode 100755 index 00000000..30037d35 --- /dev/null +++ b/missions/processes/00_shared/my_ps-macos @@ -0,0 +1,9 @@ +#!/bin/sh + +if [ -z "$*" ] +then + ps -ceo pid,ppid,comm +else + ps -ceo pid,ppid,comm -p $* +fi + diff --git a/missions/processes/00_shared/static.sh b/missions/processes/00_shared/static.sh new file mode 100644 index 00000000..3f63a529 --- /dev/null +++ b/missions/processes/00_shared/static.sh @@ -0,0 +1,16 @@ +#!/bin/sh + +# for some unkwnown reason, `ps -o pid,ppid,comm` doesn't find any process +# on the "macos-latest" image on github. +# I'm not sure if that's a problem with github images, or with macos. +# So, we use a different command when we detect this problem! + +if ps -o pid,ppid,comm >/dev/null 2>/dev/null +then + # standard ps -o pid,ppid,comm + copy_bin "$MISSION_DIR/my_ps-linux" "$GSH_ROOT/.sbin/my_ps" +else + # with additional options ps -ceo pid,ppid,comm + copy_bin "$MISSION_DIR/my_ps-macos" "$GSH_ROOT/.sbin/my_ps" +fi + diff --git a/missions/processes/01_ps_kill/auto.sh b/missions/processes/01_ps_kill/auto.sh index b8e4d0a4..959b4cf0 100644 --- a/missions/processes/01_ps_kill/auto.sh +++ b/missions/processes/01_ps_kill/auto.sh @@ -1,2 +1,4 @@ -COLUMNS=512 ps -cA | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill -9 2> /dev/null +#!/bin/sh + +my_ps | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill -9 2> /dev/null gsh check diff --git a/missions/processes/01_ps_kill/check.sh b/missions/processes/01_ps_kill/check.sh index 539e4a12..d0293e81 100644 --- a/missions/processes/01_ps_kill/check.sh +++ b/missions/processes/01_ps_kill/check.sh @@ -1,19 +1,12 @@ -#!/bin/bash +#!/bin/sh -# we need to use ps -c to only get the command name and not the full command in -# macOS -# the command name is pretty long as it the processes are not in the path and -# are given as absolute path -# GNU ps truncates the output according to COLUMNS, even when output is not on -# a tty, hence we set COLUMNS to 512 which should be long enough. -_mission_check() { - local pid=$(cat "$GSH_VAR/spell.pid") - if COLUMNS=512 ps -cp $pid | grep "$(gettext "spell")" > /dev/null +_mission_check() ( + if my_ps $(cat "$GSH_TMP/spell.pid") | grep -q "$(gettext "spell")" then echo "$(gettext "The spell is still running!")" return 1 fi return 0 -} +) _mission_check diff --git a/missions/processes/01_ps_kill/clean.sh b/missions/processes/01_ps_kill/clean.sh index ba5b4f27..43e0f2cf 100644 --- a/missions/processes/01_ps_kill/clean.sh +++ b/missions/processes/01_ps_kill/clean.sh @@ -1,3 +1,7 @@ -kill -9 $(cat "$GSH_VAR/spell.pid") 2> /dev/null -rm -f "$GSH_VAR/spell.pid" "$GSH_VAR/$(gettext "spell")" -ps -cA | awk '/sleep|tail/ {print $1}' | xargs kill -9 2> /dev/null +#!/bin/sh + +kill -9 "$(cat "$GSH_TMP/spell.pid")" 2>/dev/null +rm -f "$GSH_TMP/spell.pid" "$GSH_TMP/$(gettext "spell")" +my_ps | awk '/sleep|tail/ {print $1}' | xargs kill -9 2>/dev/null + +set -o monitor # monitor background processes (default) diff --git a/missions/processes/01_ps_kill/goal.sh b/missions/processes/01_ps_kill/goal.sh index e59655aa..5ab178cb 100644 --- a/missions/processes/01_ps_kill/goal.sh +++ b/missions/processes/01_ps_kill/goal.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh export PS if ps -c | head -n1 | grep CLS 2>/dev/null >/dev/null diff --git a/missions/processes/01_ps_kill/init.sh b/missions/processes/01_ps_kill/init.sh index 30938689..889a6e6d 100644 --- a/missions/processes/01_ps_kill/init.sh +++ b/missions/processes/01_ps_kill/init.sh @@ -1,8 +1,7 @@ -#!/bin/bash +#!/bin/sh _mission_init() { - local CC if command -v gcc >/dev/null then if [ "$GSH_MODE" = DEBUG ] @@ -42,18 +41,20 @@ _mission_init() { exec 1>/dev/null exec 2>/dev/null fi - echo $CC "$MISSION_DIR/spell.c" -o "$GSH_VAR/$(gettext "spell")" - $CC "$MISSION_DIR/spell.c" -o "$GSH_VAR/$(gettext "spell")" + echo $CC "$MISSION_DIR/spell.c" -o "$GSH_TMP/$(gettext "spell")" + $CC "$MISSION_DIR/spell.c" -o "$GSH_TMP/$(gettext "spell")" ) || { echo "compilation failed" >&2; return 1; } else - cp "$MISSION_DIR/spell.sh" "$GSH_VAR/$(gettext "spell")" - chmod 755 "$GSH_VAR/$(gettext "spell")" + cp "$MISSION_DIR/spell.sh" "$GSH_TMP/$(gettext "spell")" + chmod 755 "$GSH_TMP/$(gettext "spell")" fi - "$GSH_VAR/$(gettext "spell")" & - local PID=$! - disown $PID - echo $PID > "$GSH_VAR"/spell.pid + "$GSH_TMP/$(gettext "spell")" & + echo $! > "$GSH_TMP"/spell.pid return 0 } -_mission_init +set +o monitor # do not monitor background processes +# FIXME: for some unknown reason, this doesn't work if we start with this +# mission directly! + +set +b; _mission_init diff --git a/missions/processes/01_ps_kill/spell.sh b/missions/processes/01_ps_kill/spell.sh index 7e9d9a4b..7f7f131e 100755 --- a/missions/processes/01_ps_kill/spell.sh +++ b/missions/processes/01_ps_kill/spell.sh @@ -12,7 +12,7 @@ DELAY=5 while true do sleep $DELAY & wait $! - INDENT=$(echo " " | cut -c1-$((2+RANDOM%15))) + INDENT=$(echo " " | cut -c1-$((2+$(RANDOM)%15))) cat <<'EOS' | sed "s/^/$INDENT/g" *#@* diff --git a/missions/processes/01_ps_kill/test.sh b/missions/processes/01_ps_kill/test.sh index bef211a8..d03f53f3 100644 --- a/missions/processes/01_ps_kill/test.sh +++ b/missions/processes/01_ps_kill/test.sh @@ -1,4 +1,6 @@ -COLUMNS=512 ps -cA | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill -9 2> /dev/null +#!/bin/sh + +my_ps | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill -9 2>/dev/null gsh assert check true gsh assert check false diff --git a/missions/processes/02_ps_kill_signal/auto.sh b/missions/processes/02_ps_kill_signal/auto.sh index 5a614988..2218f68a 100644 --- a/missions/processes/02_ps_kill_signal/auto.sh +++ b/missions/processes/02_ps_kill_signal/auto.sh @@ -1,3 +1,5 @@ -COLUMNS=512 ps -cA | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill 2> /dev/null -COLUMNS=512 ps -cA | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill -9 2> /dev/null +#!/bin/sh + +my_ps | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill 2>/dev/null +my_ps | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill -9 2>/dev/null gsh check diff --git a/missions/processes/02_ps_kill_signal/check.sh b/missions/processes/02_ps_kill_signal/check.sh index 0b621674..64e0f021 100644 --- a/missions/processes/02_ps_kill_signal/check.sh +++ b/missions/processes/02_ps_kill_signal/check.sh @@ -1,13 +1,7 @@ -#!/bin/bash +#!/bin/sh -# we need to use ps -c to only get the command name and not the full command in -# macOS -# the command name is pretty long as it the processes are not in the path and -# are given as absolute path -# GNU ps truncates the output according to COLUMNS, even when output is not on -# a tty, hence we set COLUMNS to 512 which should be long enough. -_mission_check() { - local nb_spells=$(COLUMNS=512 ps -cA | grep "$(gettext "spell")" | wc -l | tr -d ' ') +_mission_check() ( + nb_spells=$(my_ps | grep -c "$(gettext "spell")" | tr -d ' ') if [ "$nb_spells" -gt 0 ] then @@ -15,15 +9,14 @@ _mission_check() { return 1 fi - local nb=$(wc -l "$GSH_VAR/spell.pids" | awk '{print $1}') + nb=$(wc -l "$GSH_TMP/spell.pids" | awk '{print $1}') if [ "$nb" -eq 1 ] then echo "$(gettext "You haven't tried using the standard TERM signal on the spell.")" return 1 fi - return 0 -} +) _mission_check diff --git a/missions/processes/02_ps_kill_signal/clean.sh b/missions/processes/02_ps_kill_signal/clean.sh index e49a9084..d6444954 100644 --- a/missions/processes/02_ps_kill_signal/clean.sh +++ b/missions/processes/02_ps_kill_signal/clean.sh @@ -1,4 +1,8 @@ -kill -9 $(cat "$GSH_VAR/spell-term.pids" 2>/dev/null) 2> /dev/null -kill -9 $(cat "$GSH_VAR/spell.pids" 2>/dev/null) 2> /dev/null -ps -cA | awk '/sleep|tail/ {print $1}' | xargs kill -9 2> /dev/null -rm -f "$GSH_VAR/spell-term.pids" "$GSH_VAR/spell.pids" "$GSH_VAR/$(gettext "spell")" +#!/bin/sh + +kill -9 $(cat "$GSH_TMP/spell-term.pids" 2>/dev/null) 2>/dev/null +kill -9 $(cat "$GSH_TMP/spell.pids" 2>/dev/null) 2>/dev/null +my_ps | awk '/sleep|tail/ {print $1}' | xargs kill -9 2>/dev/null +rm -f "$GSH_TMP/spell-term.pids" "$GSH_TMP/spell.pids" "$GSH_TMP/$(gettext "spell")" + +set -o monitor # monitor background processes (default) diff --git a/missions/processes/02_ps_kill_signal/goal.sh b/missions/processes/02_ps_kill_signal/goal.sh index e59655aa..5ab178cb 100644 --- a/missions/processes/02_ps_kill_signal/goal.sh +++ b/missions/processes/02_ps_kill_signal/goal.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh export PS if ps -c | head -n1 | grep CLS 2>/dev/null >/dev/null diff --git a/missions/processes/02_ps_kill_signal/init.sh b/missions/processes/02_ps_kill_signal/init.sh index 63709285..c1e9cba2 100644 --- a/missions/processes/02_ps_kill_signal/init.sh +++ b/missions/processes/02_ps_kill_signal/init.sh @@ -1,8 +1,7 @@ -#!/bin/bash +#!/bin/sh _mission_init() { - local CC if command -v gcc >/dev/null then if [ "$GSH_MODE" = DEBUG ] @@ -48,25 +47,27 @@ _mission_init() { # "-lintl" to the compiler, so we have to try several things! { echo "GSH: compiling spell.c, first try" >&2 - echo $CC "$MISSION_DIR/spell.c" -lpthread -o "$GSH_VAR/$(gettext "spell")" - $CC "$MISSION_DIR/spell.c" -lpthread -o "$GSH_VAR/$(gettext "spell")" + echo $CC "$MISSION_DIR/spell.c" -lpthread -o "$GSH_TMP/$(gettext "spell")" + $CC "$MISSION_DIR/spell.c" -lpthread -o "$GSH_TMP/$(gettext "spell")" } || { echo "GSH: compiling spell.c, second try" - echo $CC -I/usr/local/include/ -L/usr/local/lib "$MISSION_DIR/spell.c" -lintl -lpthread -o "$GSH_VAR/$(gettext "spell")" - $CC -I/usr/local/include/ -L/usr/local/lib "$MISSION_DIR/spell.c" -lintl -lpthread -o "$GSH_VAR/$(gettext "spell")" + echo $CC -I/usr/local/include/ -L/usr/local/lib "$MISSION_DIR/spell.c" -lintl -lpthread -o "$GSH_TMP/$(gettext "spell")" + $CC -I/usr/local/include/ -L/usr/local/lib "$MISSION_DIR/spell.c" -lintl -lpthread -o "$GSH_TMP/$(gettext "spell")" } ) || { echo "compilation failed" >&2; return 1; } else - cp "$MISSION_DIR/spell.sh" "$GSH_VAR/$(gettext "spell")" - chmod 755 "$GSH_VAR/$(gettext "spell")" + cp "$MISSION_DIR/spell.sh" "$GSH_TMP/$(gettext "spell")" + chmod 755 "$GSH_TMP/$(gettext "spell")" fi - "$GSH_VAR/$(gettext "spell")" & - local PID=$! - disown $PID - echo $PID > "$GSH_VAR"/spell.pids + "$GSH_TMP/$(gettext "spell")" & + echo $! > "$GSH_TMP"/spell.pids return 0 } +set +o monitor # do not monitor background processes +# FIXME: for some unknown reason, this doesn't work if we start with this +# mission directly! + _mission_init diff --git a/missions/processes/02_ps_kill_signal/spell.c b/missions/processes/02_ps_kill_signal/spell.c index ea7bb68b..8596e821 100644 --- a/missions/processes/02_ps_kill_signal/spell.c +++ b/missions/processes/02_ps_kill_signal/spell.c @@ -1,3 +1,7 @@ +// FIXME: the semaphores can probably be safely removed if we open the pid file +// with O_APPEND and use plain "write" +// Same for stdout, using a single "write" for printing a lump of coal should +// be enough to ensure non-overlapping. #include #include #include @@ -93,7 +97,7 @@ int main() // define the path to the file in which to write the children's PIDs. wordexp_t result; - wordexp("$GSH_VAR/spell.pids", &result, 0); + wordexp("$GSH_TMP/spell.pids", &result, 0); // if the path contains spaces, the result is contained in several words! int pos = 0; for (int i = 0; result.we_wordv[i] != NULL; i++) { diff --git a/missions/processes/02_ps_kill_signal/spell.sh b/missions/processes/02_ps_kill_signal/spell.sh index a0c7e1e6..aba3dfe2 100755 --- a/missions/processes/02_ps_kill_signal/spell.sh +++ b/missions/processes/02_ps_kill_signal/spell.sh @@ -5,7 +5,7 @@ trap spawn TERM spawn() { echo "$(gettext "You'll need to do better than that to kill my spell!")" "$0" & - echo $! >> "$GSH_VAR/spell.pids" + echo $! >> "$GSH_TMP/spell.pids" } DELAY=5 diff --git a/missions/processes/02_ps_kill_signal/test.sh b/missions/processes/02_ps_kill_signal/test.sh index 6de2fdf7..5363f88b 100644 --- a/missions/processes/02_ps_kill_signal/test.sh +++ b/missions/processes/02_ps_kill_signal/test.sh @@ -1,11 +1,13 @@ -COLUMNS=512 ps -cA | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill 2> /dev/null -COLUMNS=512 ps -cA | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill -9 2> /dev/null +#!/bin/sh + +my_ps | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill 2> /dev/null +my_ps | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill -9 2> /dev/null gsh assert check true -COLUMNS=512 ps -cA | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill -9 2> /dev/null +my_ps | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill -9 2> /dev/null gsh assert check false -COLUMNS=512 ps -cA | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill 2> /dev/null -COLUMNS=512 ps -cA | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill 2> /dev/null -COLUMNS=512 ps -cA | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | head -n2 | xargs kill -9 2> /dev/null +my_ps | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill 2> /dev/null +my_ps | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill 2> /dev/null +my_ps | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | head -n2 | xargs kill -9 2> /dev/null gsh assert check false diff --git a/missions/processes/03_pstree_kill/auto.sh b/missions/processes/03_pstree_kill/auto.sh index 2030a40d..181560bc 100644 --- a/missions/processes/03_pstree_kill/auto.sh +++ b/missions/processes/03_pstree_kill/auto.sh @@ -1,11 +1,11 @@ -kill_imp_spell() { - local p=$(COLUMNS=512 ps -cA | grep "$(gettext "mischievous_imp")" | awk '{print $1}') - # argh, ps -Ao ... doesn't find any process in macos-latest - # and ps -cAo ... doesn't find any process in ubuntu-latest - # let's try both! - COLUMNS=512 ps -cAo pid,comm,ppid 2>/dev/null | grep "$p$" | grep "$(gettext "spell")" | awk '{print $1}' | xargs kill -9 2>/dev/null - COLUMNS=512 ps -Ao pid,comm,ppid | grep "$p$" | grep "$(gettext "spell")" | awk '{print $1}' | xargs kill -9 2>/dev/null -} +#!/bin/sh + +kill_imp_spell() ( + p=$(my_ps | grep "$(gettext "mischievous_imp")" | awk '{print $1}') + my_ps | + awk -v PID="$p" -v spell="$(gettext "spell")" '($2 == PID) && ($3 ~ spell) {print $1}' | + xargs kill -9 2>/dev/null +) cellar=$(eval_gettext '$GSH_HOME/Castle/Cellar') diff --git a/missions/processes/03_pstree_kill/check.sh b/missions/processes/03_pstree_kill/check.sh index 328a6d55..e13d6bea 100644 --- a/missions/processes/03_pstree_kill/check.sh +++ b/missions/processes/03_pstree_kill/check.sh @@ -1,63 +1,56 @@ -#!/bin/bash +#!/bin/sh -# we need to use ps -c to only get the command name and not the full command in -# macOS -# the command name is pretty long as it the processes are not in the path and -# are given as absolute path -# GNU ps truncates the output according to COLUMNS, even when output is not on -# a tty, hence we set COLUMNS to 512 which should be long enough. -_mission_check() { +_mission_check() ( - local pid - pid=$(cat "$GSH_VAR"/fairy.pid) - if ! COLUMNS=512 ps -cp $pid | grep "$(gettext "nice_fairy")" > /dev/null + pid=$(cat "$GSH_TMP"/fairy.pid) + if ! my_ps "$pid" | grep -q "$(gettext "nice_fairy")" then echo "$(gettext "Did you kill the fairy?")" return 1 fi - pid=$(cat "$GSH_VAR"/imp.pid) - if ! COLUMNS=512 ps -cp $pid | grep "$(gettext "mischievous_imp")" > /dev/null + pid=$(cat "$GSH_TMP"/imp.pid) + if ! my_ps "$pid" | grep -q "$(gettext "mischievous_imp")" then echo "$(gettext "Did you kill the imp?")" return 1 fi - local nb=$(COLUMNS=512 ps -cp $(cat "$GSH_VAR"/fairy_spell.pids) | grep "$(gettext "spell")" | wc -l) + nb=$(my_ps $(cat "$GSH_TMP"/fairy_spell.pids) | grep -c "$(gettext "spell")") if [ "$nb" -lt 3 ] then echo "$(gettext "Did you remove some of the fairy's spells?")" return 1 fi - local nb=$(COLUMNS=512 ps -cp $(cat "$GSH_VAR"/imp_spell.pids) | grep "$(gettext "spell")" | wc -l) + nb=$(my_ps $(cat "$GSH_TMP"/imp_spell.pids) | grep -c "$(gettext "spell")") if [ "$nb" -ne 0 ] then echo "$(gettext "Are you sure you removed all the imp's spells?")" return 1 fi - local cellar=$(eval_gettext '$GSH_HOME/Castle/Cellar') - local coals=$(find "$cellar" -name "*_$(gettext "coal")" | wc -l) + cellar=$(eval_gettext '$GSH_HOME/Castle/Cellar') + coals=$(find "$cellar" -name "*_$(gettext "coal")" | wc -l) if [ "$coals" -ne 0 ] then echo "$(gettext "There still is some coal in the cellar!")" return 1 fi - local nl="" - while ! [ -e "$GSH_VAR/snowflakes.list" ] + nl="" + while ! [ -e "$GSH_TMP/snowflakes.list" ] do sleep 0.5 printf "." - nl="\n" + nl='\n' done printf "$nl" cd "$cellar" - sort "$GSH_VAR"/snowflakes.list 2>/dev/null | uniq > "$GSH_VAR"/snowflakes-generated - ls *_"$(gettext "snowflake")" 2>/dev/null | sort | uniq > "$GSH_VAR"/snowflakes-present + sort "$GSH_TMP"/snowflakes.list 2>/dev/null | uniq > "$GSH_TMP"/snowflakes-generated + ls -- *_"$(gettext "snowflake")" 2>/dev/null | sort | uniq > "$GSH_TMP"/snowflakes-present # only check for missing snowflakes, and ignore snowflakes that might be present but are # not in the list - local nb=$(comm -1 -3 "$GSH_VAR"/snowflakes-present "$GSH_VAR"/snowflakes-generated | wc -l) + nb=$(comm -1 -3 "$GSH_TMP"/snowflakes-present "$GSH_TMP"/snowflakes-generated | wc -l) if [ "$nb" -gt 0 ] then @@ -65,6 +58,6 @@ _mission_check() { return 1 fi return 0 -} +) _mission_check diff --git a/missions/processes/03_pstree_kill/clean.sh b/missions/processes/03_pstree_kill/clean.sh index 92738320..f169d810 100644 --- a/missions/processes/03_pstree_kill/clean.sh +++ b/missions/processes/03_pstree_kill/clean.sh @@ -1,19 +1,24 @@ -ps -ec | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill -9 &> /dev/null -ps -ec | awk -v imp="$(gettext "mischievous_imp")" '$0 ~ imp {print $1}' | xargs kill -9 &> /dev/null -ps -ec | awk -v fairy="$(gettext "nice_fairy")" '$0 ~ fairy {print $1}' | xargs kill -9 &> /dev/null -ps -ec | awk '$0 ~ /sleep|tail/ {print $1}' | xargs kill -9 &> /dev/null -rm -rf "$GSH_VAR/fairy" "$GSH_VAR/imp" -rm -f "$GSH_VAR/snowflakes.list" -rm -f "$GSH_VAR/coals.list" -rm -f "$GSH_VAR/snowflakes-"{generated,present} -rm -f "$GSH_VAR"/{fairy_spell,imp_spell}.pids -rm -f "$GSH_VAR"/{fairy,imp}.pid -rm -f "$GSH_VAR"/{fairy,imp}.pid -rm -f "$GSH_VAR/$(gettext "nice_fairy")" -rm -f "$GSH_VAR/$(gettext "mischievous_imp")" +#!/bin/sh + +my_ps | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill -9 2>/dev/null +my_ps | awk -v imp="$(gettext "mischievous_imp")" '$0 ~ imp {print $1}' | xargs kill -9 2>/dev/null +my_ps | awk -v fairy="$(gettext "nice_fairy")" '$0 ~ fairy {print $1}' | xargs kill -9 2>/dev/null +my_ps | awk '$0 ~ /sleep|tail/ {print $1}' | xargs kill -9 2>/dev/null +rm -rf "$GSH_TMP/fairy" "$GSH_TMP/imp" +rm -f "$GSH_TMP/snowflakes.list" +rm -f "$GSH_TMP/coals.list" +rm -f "$GSH_TMP/snowflakes-generated" +rm -f "$GSH_TMP/snowflakes-present" +rm -f "$GSH_TMP/fairy_spell.pids" +rm -f "$GSH_TMP/imp_spell.pids" +rm -f "$GSH_TMP/fairy.pid" +rm -f "$GSH_TMP/imp.pid" +rm -f "$GSH_TMP/$(gettext "nice_fairy")" +rm -f "$GSH_TMP/$(gettext "mischievous_imp")" ( cd "$(eval_gettext '$GSH_HOME/Castle/Cellar')" # keep at most 10 snowflakes find . -name "*_$(gettext "snowflake")" | sed '1,10d' | xargs rm -f find . -name "*_$(gettext "coal")" | xargs rm -f ) +set -o monitor # monitor background processes (default) diff --git a/missions/processes/03_pstree_kill/fairy/spell.sh b/missions/processes/03_pstree_kill/fairy/spell.sh index 60ad9cd3..8df1032d 100644 --- a/missions/processes/03_pstree_kill/fairy/spell.sh +++ b/missions/processes/03_pstree_kill/fairy/spell.sh @@ -1,17 +1,16 @@ -#!/bin/bash +#!/bin/sh . gettext.sh DELAY=3 OFFSET=$1 -sleep ${OFFSET:-1} +sleep "${OFFSET:-1}" while true do - INDENT=$(echo " " | cut -c1-$((2+RANDOM%15))) - filename="$(eval_gettext '$GSH_HOME/Castle/Cellar')/${RANDOM}_$(gettext "snowflake")" - sign_file "$MISSION_DIR/ascii-art/snowflake-$((RANDOM%4)).txt" "$filename" - echo "${filename#$(eval_gettext '$GSH_HOME/Castle/Cellar')/}" >> "$GSH_VAR/snowflakes.list" - sleep $DELAY & wait $! + filename="$(eval_gettext '$GSH_HOME/Castle/Cellar')/$(RANDOM)_$(gettext "snowflake")" + sign_file "$MISSION_DIR/ascii-art/snowflake-$(($(RANDOM)%4)).txt" "$filename" + echo "${filename#$(eval_gettext '$GSH_HOME/Castle/Cellar')/}" >> "$GSH_TMP/snowflakes.list" + sleep $DELAY & wait $! done diff --git a/missions/processes/03_pstree_kill/imp/spell.sh b/missions/processes/03_pstree_kill/imp/spell.sh index 3c8f140d..775a03e6 100644 --- a/missions/processes/03_pstree_kill/imp/spell.sh +++ b/missions/processes/03_pstree_kill/imp/spell.sh @@ -1,17 +1,16 @@ -#!/bin/bash +#!/bin/sh . gettext.sh DELAY=3 OFFSET=$1 -sleep ${OFFSET:-1} +sleep "${OFFSET:-1}" while true do - INDENT=$(echo " " | cut -c1-$((2+RANDOM%15))) - filename="$(eval_gettext '$GSH_HOME/Castle/Cellar')/${RANDOM}_$(gettext "coal")" - sign_file "$MISSION_DIR/ascii-art/coal-$((RANDOM%4)).txt" "$filename" - echo "${filename#$(eval_gettext '$GSH_HOME/Castle/Cellar')/}" >> "$GSH_VAR/coals.list" - sleep $DELAY & wait $! + filename="$(eval_gettext '$GSH_HOME/Castle/Cellar')/$(RANDOM)_$(gettext "coal")" + sign_file "$MISSION_DIR/ascii-art/coal-$(($(RANDOM)%4)).txt" "$filename" + echo "${filename#$(eval_gettext '$GSH_HOME/Castle/Cellar')/}" >> "$GSH_TMP/coals.list" + sleep $DELAY & wait $! done diff --git a/missions/processes/03_pstree_kill/init.sh b/missions/processes/03_pstree_kill/init.sh index 25317e66..19c6d9cc 100644 --- a/missions/processes/03_pstree_kill/init.sh +++ b/missions/processes/03_pstree_kill/init.sh @@ -1,8 +1,7 @@ -#!/bin/bash +#!/bin/sh _mission_init() { - local CC if command -v gcc >/dev/null then if [ "$GSH_MODE" = DEBUG ] @@ -53,57 +52,55 @@ _mission_init() { # under BSD, libintl is installed in /usr/local { echo "GSH: compiling process.c, first try" >&2 - echo $CC "$MISSION_DIR/process.c" -o "$GSH_VAR/$(gettext "nice_fairy")" - $CC "$MISSION_DIR/process.c" -o "$GSH_VAR/$(gettext "nice_fairy")" - echo $CC "$MISSION_DIR/process.c" -o "$GSH_VAR/$(gettext "mischievous_imp")" - $CC "$MISSION_DIR/process.c" -o "$GSH_VAR/$(gettext "mischievous_imp")" + echo $CC "$MISSION_DIR/process.c" -o "$GSH_TMP/$(gettext "nice_fairy")" + $CC "$MISSION_DIR/process.c" -o "$GSH_TMP/$(gettext "nice_fairy")" + echo $CC "$MISSION_DIR/process.c" -o "$GSH_TMP/$(gettext "mischievous_imp")" + $CC "$MISSION_DIR/process.c" -o "$GSH_TMP/$(gettext "mischievous_imp")" } || { echo "GSH: compiling process.c, second try" - echo $CC -I/usr/local/include/ -L/usr/local/lib "$MISSION_DIR/process.c" -lintl -o "$GSH_VAR/$(gettext "nice_fairy")" - $CC -I/usr/local/include/ -L/usr/local/lib "$MISSION_DIR/process.c" -lintl -o "$GSH_VAR/$(gettext "nice_fairy")" - echo $CC -I/usr/local/include/ -L/usr/local/lib "$MISSION_DIR/process.c" -lintl -o "$GSH_VAR/$(gettext "mischievous_imp")" - $CC -I/usr/local/include/ -L/usr/local/lib "$MISSION_DIR/process.c" -lintl -o "$GSH_VAR/$(gettext "mischievous_imp")" + echo $CC -I/usr/local/include/ -L/usr/local/lib "$MISSION_DIR/process.c" -lintl -o "$GSH_TMP/$(gettext "nice_fairy")" + $CC -I/usr/local/include/ -L/usr/local/lib "$MISSION_DIR/process.c" -lintl -o "$GSH_TMP/$(gettext "nice_fairy")" + echo $CC -I/usr/local/include/ -L/usr/local/lib "$MISSION_DIR/process.c" -lintl -o "$GSH_TMP/$(gettext "mischievous_imp")" + $CC -I/usr/local/include/ -L/usr/local/lib "$MISSION_DIR/process.c" -lintl -o "$GSH_TMP/$(gettext "mischievous_imp")" } || { echo "compilation failed" >&2; return 1; } - mkdir -p "$GSH_VAR/fairy/" - $CC -D WHO=FAIRY "$MISSION_DIR/spell.c" -lpthread -o "$GSH_VAR/fairy/$(gettext "spell")" + mkdir -p "$GSH_TMP/fairy/" + $CC -D WHO=FAIRY "$MISSION_DIR/spell.c" -lpthread -o "$GSH_TMP/fairy/$(gettext "spell")" - mkdir -p "$GSH_VAR/imp/" - $CC -D WHO=IMP "$MISSION_DIR/spell.c" -lpthread -o "$GSH_VAR/imp/$(gettext "spell")" + mkdir -p "$GSH_TMP/imp/" + $CC -D WHO=IMP "$MISSION_DIR/spell.c" -lpthread -o "$GSH_TMP/imp/$(gettext "spell")" ) unset CC else - local BASH_PATH=$(command -v bash) + cp "$MISSION_DIR/nice_fairy.sh" "$GSH_TMP/$(gettext "nice_fairy")" + chmod 755 "$GSH_TMP/$(gettext "nice_fairy")" - { echo "#!$BASH_PATH" ; sed "1d" "$MISSION_DIR/nice_fairy.sh" ; } > "$GSH_VAR/$(gettext "nice_fairy")" - chmod 755 "$GSH_VAR/$(gettext "nice_fairy")" + mkdir -p "$GSH_TMP/fairy/" + cp "$MISSION_DIR/fairy/spell.sh" "$GSH_TMP/fairy/$(gettext "spell")" + chmod 755 "$GSH_TMP/fairy/$(gettext "spell")" - mkdir -p "$GSH_VAR/fairy/" - cp "$MISSION_DIR/fairy/spell.sh" "$GSH_VAR/fairy/$(gettext "spell")" - chmod 755 "$GSH_VAR/fairy/$(gettext "spell")" + cp "$MISSION_DIR/mischievous_imp.sh" "$GSH_TMP/$(gettext "mischievous_imp")" + chmod 755 "$GSH_TMP/$(gettext "mischievous_imp")" - { echo "#!$BASH_PATH" ; sed "1d" "$MISSION_DIR/mischievous_imp.sh" ; } > "$GSH_VAR/$(gettext "mischievous_imp")" - chmod 755 "$GSH_VAR/$(gettext "mischievous_imp")" - - mkdir -p "$GSH_VAR/imp/" - cp "$MISSION_DIR/imp/spell.sh" "$GSH_VAR/imp/$(gettext "spell")" - chmod 755 "$GSH_VAR/imp/$(gettext "spell")" + mkdir -p "$GSH_TMP/imp/" + cp "$MISSION_DIR/imp/spell.sh" "$GSH_TMP/imp/$(gettext "spell")" + chmod 755 "$GSH_TMP/imp/$(gettext "spell")" fi - "$GSH_VAR/$(gettext "nice_fairy")" & - local PID=$! - disown $PID - echo $PID > "$GSH_VAR/fairy.pid" + "$GSH_TMP/$(gettext "nice_fairy")" & + echo $! > "$GSH_TMP/fairy.pid" - "$GSH_VAR/$(gettext "mischievous_imp")" & - local PID=$! - disown $PID - echo $PID > "$GSH_VAR/imp.pid" + "$GSH_TMP/$(gettext "mischievous_imp")" & + echo $! > "$GSH_TMP/imp.pid" return 0 } +set +o monitor # do not monitor background processes +# FIXME: for some unknown reason, this doesn't work if we start with this +# mission directly! + _mission_init diff --git a/missions/processes/03_pstree_kill/mischievous_imp.sh b/missions/processes/03_pstree_kill/mischievous_imp.sh index ef970acc..370704d2 100755 --- a/missions/processes/03_pstree_kill/mischievous_imp.sh +++ b/missions/processes/03_pstree_kill/mischievous_imp.sh @@ -2,17 +2,14 @@ . gettext.sh -dir=$(eval_gettext '$GSH_HOME/Castle/Cellar') -logfile=$GSH_VAR/coals +"$GSH_TMP/imp/$(gettext "spell")" 0 & +printf "$!," > "$GSH_TMP/imp_spell.pids" -"$GSH_VAR"/imp/$(gettext "spell") 0 & -printf "$!," > "$GSH_VAR/imp_spell.pids" +"$GSH_TMP/imp/$(gettext "spell")" 1 & +printf "$!," >> "$GSH_TMP/imp_spell.pids" -"$GSH_VAR"/imp/$(gettext "spell") 1 & -printf "$!," >> "$GSH_VAR/imp_spell.pids" - -"$GSH_VAR"/imp/$(gettext "spell") 2 & -printf "$!" >> "$GSH_VAR/imp_spell.pids" +"$GSH_TMP/imp/$(gettext "spell")" 2 & +printf "$!" >> "$GSH_TMP/imp_spell.pids" trap "" TERM INT diff --git a/missions/processes/03_pstree_kill/nice_fairy.sh b/missions/processes/03_pstree_kill/nice_fairy.sh index 8af79662..ca4c4b3d 100755 --- a/missions/processes/03_pstree_kill/nice_fairy.sh +++ b/missions/processes/03_pstree_kill/nice_fairy.sh @@ -2,17 +2,14 @@ . gettext.sh -dir=$(eval_gettext '$GSH_HOME/Castle/Cellar') -logfile=$GSH_VAR/snowflakes +"$GSH_TMP/fairy/$(gettext "spell")" 0 & +printf "$!," > "$GSH_TMP/fairy_spell.pids" -"$GSH_VAR"/fairy/$(gettext "spell") 0 & -printf "$!," > "$GSH_VAR/fairy_spell.pids" +"$GSH_TMP/fairy/$(gettext "spell")" 1 & +printf "$!," >> "$GSH_TMP/fairy_spell.pids" -"$GSH_VAR"/fairy/$(gettext "spell") 1 & -printf "$!," >> "$GSH_VAR/fairy_spell.pids" - -"$GSH_VAR"/fairy/$(gettext "spell") 2 & -printf "$!" >> "$GSH_VAR/fairy_spell.pids" +"$GSH_TMP/fairy/$(gettext "spell")" 2 & +printf "$!" >> "$GSH_TMP/fairy_spell.pids" trap "" TERM INT diff --git a/missions/processes/03_pstree_kill/process.c b/missions/processes/03_pstree_kill/process.c index 7f189e7a..2366b034 100644 --- a/missions/processes/03_pstree_kill/process.c +++ b/missions/processes/03_pstree_kill/process.c @@ -60,10 +60,10 @@ int main(int argc, char** argv) wordexp_t result; switch (who) { case IMP: - wordexp("$GSH_VAR/imp_spell.pids", &result, 0); + wordexp("$GSH_TMP/imp_spell.pids", &result, 0); break; case FAIRY: - wordexp("$GSH_VAR/fairy_spell.pids", &result, 0); + wordexp("$GSH_TMP/fairy_spell.pids", &result, 0); break; } // if the path contains spaces, the result is contained in several words! @@ -81,10 +81,10 @@ int main(int argc, char** argv) switch (who) { case IMP: - wordexp("$GSH_VAR/imp/$(gettext 'spell')", &result, 0); + wordexp("$GSH_TMP/imp/$(gettext 'spell')", &result, 0); break; case FAIRY: - wordexp("$GSH_VAR/fairy/$(gettext 'spell')", &result, 0); + wordexp("$GSH_TMP/fairy/$(gettext 'spell')", &result, 0); break; } pos = 0; diff --git a/missions/processes/03_pstree_kill/spell.c b/missions/processes/03_pstree_kill/spell.c index 5b2952af..61e39d83 100644 --- a/missions/processes/03_pstree_kill/spell.c +++ b/missions/processes/03_pstree_kill/spell.c @@ -1,3 +1,6 @@ +// FIXME: the semaphores can probably be safely removed if we open pid file +// with O_APPEND and use plain "write" + #include #include #include @@ -20,15 +23,15 @@ // template for the input files #define IMP_INPUT_TEMPLATE "$MISSION_DIR/ascii-art/coal-%d.txt" #define FAIRY_INPUT_TEMPLATE "$MISSION_DIR/ascii-art/snowflake-%d.txt" -#define INPUT_TEMPLATE \ +#define INPUT_TEMPLATE \ (WHO == IMP ? IMP_INPUT_TEMPLATE : FAIRY_INPUT_TEMPLATE) // template for the output files -#define IMP_OUTPUT_TEMPLATE \ +#define IMP_OUTPUT_TEMPLATE \ "$(gettext '$GSH_HOME/Castle/Cellar')/%d_$(gettext 'coal')" -#define FAIRY_OUTPUT_TEMPLATE \ +#define FAIRY_OUTPUT_TEMPLATE \ "$(gettext '$GSH_HOME/Castle/Cellar')/%d_$(gettext 'snowflake')" -#define OUTPUT_TEMPLATE \ +#define OUTPUT_TEMPLATE \ (WHO == IMP ? IMP_OUTPUT_TEMPLATE : FAIRY_OUTPUT_TEMPLATE) char input_template[1024]; // template for the input ASCII-art files @@ -40,8 +43,8 @@ char output_file[1024]; // actual path for the current output file char line[1024]; // content of the ASCII-art file -#define LOG_FILE \ - (WHO == IMP ? "$GSH_VAR/coals.list" : "$GSH_VAR/snowflakes.list") +#define LOG_FILE \ + (WHO == IMP ? "$GSH_TMP/coals.list" : "$GSH_TMP/snowflakes.list") char log_file[1024]; // path for the log file #define MAX_SPACES 30 diff --git a/missions/processes/03_pstree_kill/static.sh b/missions/processes/03_pstree_kill/static.sh index 5e62570a..bba65303 100644 --- a/missions/processes/03_pstree_kill/static.sh +++ b/missions/processes/03_pstree_kill/static.sh @@ -1,2 +1,4 @@ +#!/bin/sh + mkdir -p "$(eval_gettext '$GSH_HOME/Castle/Cellar')" diff --git a/missions/processes/03_pstree_kill/test.sh b/missions/processes/03_pstree_kill/test.sh index 4f5a5a03..2ece6594 100644 --- a/missions/processes/03_pstree_kill/test.sh +++ b/missions/processes/03_pstree_kill/test.sh @@ -1,23 +1,23 @@ -kill_imp_spell() { - local p=$(COLUMNS=512 ps -cA | grep "$(gettext "mischievous_imp")" | awk '{print $1}') - # argh, ps -eo ... doesn't find any process in macos-latest - # and ps -cAo ... doesn't find any process in ubuntu-latest - # let's try both! - COLUMNS=512 ps -cAo pid,comm,ppid 2>/dev/null | grep "$p$" | grep "$(gettext "spell")" | awk '{print $1}' | xargs kill -9 2>/dev/null - COLUMNS=512 ps -eo pid,comm,ppid | grep "$p$" | grep "$(gettext "spell")" | awk '{print $1}' | xargs kill -9 2>/dev/null -} +#!/bin/sh + +kill_imp_spell() ( + p=$(my_ps | grep "$(gettext "mischievous_imp")" | awk '{print $1}') + my_ps | + awk -v PID="$p" -v spell="$(gettext "spell")" '($2 == PID) && ($3 ~ spell) {print $1}' | + xargs kill -9 2>/dev/null +) cellar=$(eval_gettext '$GSH_HOME/Castle/Cellar') -stand_by() { - local nl="" - while ! [ -e "$GSH_VAR/snowflakes.list" ] || ! [ -e "$GSH_VAR/coals.list" ] +stand_by() ( + nl="" + while ! [ -e "$GSH_TMP/snowflakes.list" ] || ! [ -e "$GSH_TMP/coals.list" ] do sleep 0.5 printf "." - nl="\n" + nl='\n' done printf "$nl" -} +) stand_by kill_imp_spell diff --git a/missions/stdin_stdout_stderr/01_stdin_additions/auto.sh b/missions/stdin_stdout_stderr/01_stdin_additions/auto.sh index 1fbbb2f0..b59b60ce 100644 --- a/missions/stdin_stdout_stderr/01_stdin_additions/auto.sh +++ b/missions/stdin_stdout_stderr/01_stdin_additions/auto.sh @@ -1 +1,3 @@ -gsh check < "$GSH_VAR/additions.txt" +#!/bin/sh + +gsh check < "$GSH_TMP/additions.txt" diff --git a/missions/stdin_stdout_stderr/01_stdin_additions/check.sh b/missions/stdin_stdout_stderr/01_stdin_additions/check.sh index 679671d1..2e727b7f 100644 --- a/missions/stdin_stdout_stderr/01_stdin_additions/check.sh +++ b/missions/stdin_stdout_stderr/01_stdin_additions/check.sh @@ -1,15 +1,14 @@ -#!/bin/bash +#!/bin/sh -_mission_check() { - exec 3< "$GSH_VAR/arith.txt" - local line - while IFS='' read -r -u 3 line +_mission_check() ( + exec 3< "$GSH_TMP/arith.txt" + while IFS='' read -r line <&3 do - local question="$(echo "$line" | cut -d"|" -f1)" - local result="$(echo "$line" | cut -d"|" -f2)" + question="$(echo "$line" | cut -d"|" -f1)" + result="$(echo "$line" | cut -d"|" -f2)" - local response - read -erp "$question" response + printf "%s" "$question" + read -r response case "$response" in "" | *[!0-9]*) echo "$(gettext "That's not even a number!")" @@ -25,6 +24,6 @@ _mission_check() { esac done return 0 -} +) _mission_check diff --git a/missions/stdin_stdout_stderr/01_stdin_additions/clean.sh b/missions/stdin_stdout_stderr/01_stdin_additions/clean.sh index 951ce109..4f72e4cd 100644 --- a/missions/stdin_stdout_stderr/01_stdin_additions/clean.sh +++ b/missions/stdin_stdout_stderr/01_stdin_additions/clean.sh @@ -1,2 +1,4 @@ -rm -f "$GSH_VAR/arith.txt" "$GSH_VAR/additions.txt" +#!/bin/sh + +rm -f "$GSH_TMP/arith.txt" "$GSH_TMP/additions.txt" diff --git a/missions/stdin_stdout_stderr/01_stdin_additions/init.sh b/missions/stdin_stdout_stderr/01_stdin_additions/init.sh index bdf17965..86d3d648 100644 --- a/missions/stdin_stdout_stderr/01_stdin_additions/init.sh +++ b/missions/stdin_stdout_stderr/01_stdin_additions/init.sh @@ -1,19 +1,19 @@ -#!/bin/bash +#!/bin/sh -_mission_init() { - local results=$GSH_VAR/additions.txt +_mission_init() ( + results=$GSH_TMP/additions.txt rm -f "$results" - local questions=$GSH_VAR/arith.txt + questions=$GSH_TMP/arith.txt rm -f "$questions" for _ in $(seq 5) do - local a=$((1+RANDOM%100)) - local b=$((1+RANDOM%100)) - local r=$((a + b)) + a=$((1+$(RANDOM)%100)) + b=$((1+$(RANDOM)%100)) + r=$((a + b)) echo $r >> "$results" echo "$a + $b = ?? |$r" >> "$questions" done -} +) _mission_init diff --git a/missions/stdin_stdout_stderr/02_stdin_redirection_multiplications/auto.sh b/missions/stdin_stdout_stderr/02_stdin_redirection_multiplications/auto.sh index 641e3b45..b5176040 100644 --- a/missions/stdin_stdout_stderr/02_stdin_redirection_multiplications/auto.sh +++ b/missions/stdin_stdout_stderr/02_stdin_redirection_multiplications/auto.sh @@ -1 +1,3 @@ +#!/bin/sh + gsh check < "$(eval_gettext '$GSH_HOME/Castle/Main_building/Library')/$(gettext "Mathematics_101")" diff --git a/missions/stdin_stdout_stderr/02_stdin_redirection_multiplications/check.sh b/missions/stdin_stdout_stderr/02_stdin_redirection_multiplications/check.sh index 1611762b..0dd36dc5 100644 --- a/missions/stdin_stdout_stderr/02_stdin_redirection_multiplications/check.sh +++ b/missions/stdin_stdout_stderr/02_stdin_redirection_multiplications/check.sh @@ -1,21 +1,19 @@ -#!/bin/bash +#!/bin/sh -_mission_check() { - local time_limit=$(( $(date +%s) + 10 )) +_mission_check() ( + time_limit=$(( $(date +%s) + 10 )) - exec 3< "$GSH_VAR/arith.txt" - local line - while IFS='' read -response -u 3 line + exec 3< "$GSH_TMP/arith.txt" + while IFS='' read -r line <&3 do - local question="$(echo "$line" | cut -d"|" -f1)" - local result="$(echo "$line" | cut -d"|" -f2)" + question="$(echo "$line" | cut -d"|" -f1)" + result="$(echo "$line" | cut -d"|" -f2)" - local response - read -erp "$question" response + printf "%s" "$question" + read -r response if [ "$time_limit" -le "$(date +%s)" ] then echo "$(gettext "Too slow! You need to give the answers in less than 10 seconds...")" - OK="" break fi @@ -31,7 +29,6 @@ _mission_check() { ;; esac done - -} +) _mission_check diff --git a/missions/stdin_stdout_stderr/02_stdin_redirection_multiplications/clean.sh b/missions/stdin_stdout_stderr/02_stdin_redirection_multiplications/clean.sh index 777178ec..56b8b6cd 100644 --- a/missions/stdin_stdout_stderr/02_stdin_redirection_multiplications/clean.sh +++ b/missions/stdin_stdout_stderr/02_stdin_redirection_multiplications/clean.sh @@ -1,2 +1,4 @@ -rm -f "$GSH_VAR/arith.txt" +#!/bin/sh + +rm -f "$GSH_TMP/arith.txt" diff --git a/missions/stdin_stdout_stderr/02_stdin_redirection_multiplications/init.sh b/missions/stdin_stdout_stderr/02_stdin_redirection_multiplications/init.sh index a13dfb31..3e3278ec 100644 --- a/missions/stdin_stdout_stderr/02_stdin_redirection_multiplications/init.sh +++ b/missions/stdin_stdout_stderr/02_stdin_redirection_multiplications/init.sh @@ -1,6 +1,6 @@ -#!/bin/bash +#!/bin/sh -_mission_init() { +_mission_init() ( cat > "$(eval_gettext '$GSH_HOME/Castle/Main_building/Library')/$(gettext "Greek_Latin_and_other_modern_languages")" < "$(eval_gettext '$GSH_HOME/Castle/Main_building/Library')/.$(gettext "How_to_cheat_for_exams")" - local book="$(eval_gettext '$GSH_HOME/Castle/Main_building/Library')/$(gettext "Mathematics_101")" + book="$(eval_gettext '$GSH_HOME/Castle/Main_building/Library')/$(gettext "Mathematics_101")" rm -f "$book" - local questions=$GSH_VAR/arith.txt + questions=$GSH_TMP/arith.txt rm -f "$questions" - for _ in $(seq 100) + RANDOM 200 | for _ in $(seq 100) do - local a=$((1+RANDOM%100)) - local b=$((1+RANDOM%100)) - local r=$((a * b)) + read a + read b + a=$((1+a%100)) + b=$((1+b%100)) + r=$((a * b)) echo $r >> "$book" echo "$a * $b = ?? |$r" >> "$questions" done -} +) _mission_init diff --git a/missions/stdin_stdout_stderr/02_stdin_redirection_multiplications/static.sh b/missions/stdin_stdout_stderr/02_stdin_redirection_multiplications/static.sh index 77a54bdd..40ea2944 100644 --- a/missions/stdin_stdout_stderr/02_stdin_redirection_multiplications/static.sh +++ b/missions/stdin_stdout_stderr/02_stdin_redirection_multiplications/static.sh @@ -1,3 +1,5 @@ +#!/bin/sh + mkdir -p "$(eval_gettext '$GSH_HOME/Castle/Main_building/Library')" touch "$(eval_gettext '$GSH_HOME/Castle/Main_building/Library')/$(gettext "Mathematics_101")" touch "$(eval_gettext '$GSH_HOME/Castle/Main_building/Library')/$(gettext "Greek_Latin_and_other_modern_languages")" diff --git a/missions/stdin_stdout_stderr/03_stdout_redirection_inventory/auto.sh b/missions/stdin_stdout_stderr/03_stdout_redirection_inventory/auto.sh index 8bc83bbf..b31b9bec 100644 --- a/missions/stdin_stdout_stderr/03_stdout_redirection_inventory/auto.sh +++ b/missions/stdin_stdout_stderr/03_stdout_redirection_inventory/auto.sh @@ -1,3 +1,5 @@ +#!/bin/sh + cd "$(eval_gettext '$GSH_HOME/Castle/Main_building/Library/Merlin_s_office')" ls "$(gettext "grimoire")"_* > "$(gettext "Drawer")/$(gettext "inventory.txt")" gsh check diff --git a/missions/stdin_stdout_stderr/03_stdout_redirection_inventory/check.sh b/missions/stdin_stdout_stderr/03_stdout_redirection_inventory/check.sh index be55fd2f..29862448 100644 --- a/missions/stdin_stdout_stderr/03_stdout_redirection_inventory/check.sh +++ b/missions/stdin_stdout_stderr/03_stdout_redirection_inventory/check.sh @@ -1,26 +1,27 @@ -#!/bin/bash +#!/bin/sh -_mission_check() { - local office - office="$(eval_gettext '$GSH_HOME/Castle/Main_building/Library/Merlin_s_office')" - local inventory_file="$(gettext "inventory.txt")" +_mission_check() ( + office="$(eval_gettext '$GSH_HOME/Castle/Main_building/Library/Merlin_s_office')" + inventory_file="$(gettext "inventory.txt")" - if [ ! -f "$office/$(gettext "Drawer")/$inventory_file" ] - then - echo "$(eval_gettext 'There is no $inventory_file in the drawer...')" - return 1 - fi + if [ ! -f "$office/$(gettext "Drawer")/$inventory_file" ] + then + echo "$(eval_gettext 'There is no $inventory_file in the drawer...')" + return 1 + fi - if ! cmp -s <(sort "$office/$(gettext "Drawer")/$inventory_file") "$GSH_VAR/inventory_grimoires" - then - echo "$(eval_gettext 'The content of $inventory_file is invalid. + temp_file=$(mktemp) + sort "$office/$(gettext "Drawer")/$inventory_file" >"$temp_file" + if ! cmp -s "$temp_file" "$GSH_TMP/inventory_grimoires" + then + echo "$(eval_gettext 'The content of $inventory_file is invalid. You can check its content with the command $ less $inventory_file')" - return 1 - fi - - return 0 -} - + rm -f "$temp_file" + return 1 + fi + rm -f "$temp_file" + return 0 +) _mission_check diff --git a/missions/stdin_stdout_stderr/03_stdout_redirection_inventory/clean.sh b/missions/stdin_stdout_stderr/03_stdout_redirection_inventory/clean.sh index 7770aa65..5a363ca5 100644 --- a/missions/stdin_stdout_stderr/03_stdout_redirection_inventory/clean.sh +++ b/missions/stdin_stdout_stderr/03_stdout_redirection_inventory/clean.sh @@ -1,2 +1,4 @@ -rm -f "$GSH_VAR/inventory_grimoires" +#!/bin/sh + +rm -f "$GSH_TMP/inventory_grimoires" diff --git a/missions/stdin_stdout_stderr/03_stdout_redirection_inventory/init.sh b/missions/stdin_stdout_stderr/03_stdout_redirection_inventory/init.sh index 3b4979bc..0211ec0a 100644 --- a/missions/stdin_stdout_stderr/03_stdout_redirection_inventory/init.sh +++ b/missions/stdin_stdout_stderr/03_stdout_redirection_inventory/init.sh @@ -1,30 +1,28 @@ -#!/bin/bash +#!/bin/sh -_mission_init() { - local office="$(eval_gettext '$GSH_HOME/Castle/Main_building/Library/Merlin_s_office')" +_mission_init() ( # subshell to avoid changing directory + office="$(eval_gettext '$GSH_HOME/Castle/Main_building/Library/Merlin_s_office')" find "$office" -type f -name "$(gettext "grimoire")_*" -print0 | xargs -0 rm -f - local i - for i in $(seq 100) + RANDOM 200 | for i in $(seq 100) do - local file="$office/$(gettext "grimoire")_$(checksum $RANDOM)" + read RANDOM + file="$office/$(gettext "grimoire")_$RANDOM" random_string 100 > "$file" - if [ $(( RANDOM % 2 )) -eq 0 ] + read RANDOM + if [ $((RANDOM%2)) -eq 0 ] then chmod -r "$file" fi [ $((i%3)) -eq 0 ] && printf "." done - echo - ( # subshell to avoid changing directory - cd "$office" - ls $(gettext "grimoire")_* | sort > "$GSH_VAR/inventory_grimoires" - ) + # subshell to avoid changing directory + cd "$office" + ls "$(gettext "grimoire")"_* | sort > "$GSH_TMP/inventory_grimoires" return 0 - -} +) . progress_bar.sh _mission_init diff --git a/missions/stdin_stdout_stderr/03_stdout_redirection_inventory/static.sh b/missions/stdin_stdout_stderr/03_stdout_redirection_inventory/static.sh index e4e33fbb..448452ea 100644 --- a/missions/stdin_stdout_stderr/03_stdout_redirection_inventory/static.sh +++ b/missions/stdin_stdout_stderr/03_stdout_redirection_inventory/static.sh @@ -1,2 +1,4 @@ +#!/bin/sh + mkdir -p "$(eval_gettext '$GSH_HOME/Castle/Main_building/Library/Merlin_s_office')/$(gettext "Drawer")" sign_file "$MISSION_DIR/ascii-art/candle.txt" "$(eval_gettext '$GSH_HOME/Castle/Main_building/Library/Merlin_s_office')/$(gettext "candle")" diff --git a/missions/stdin_stdout_stderr/04_stderr_dev-null_grimoires/auto.sh b/missions/stdin_stdout_stderr/04_stderr_dev-null_grimoires/auto.sh index 51b518e0..19d2ecb0 100644 --- a/missions/stdin_stdout_stderr/04_stderr_dev-null_grimoires/auto.sh +++ b/missions/stdin_stdout_stderr/04_stderr_dev-null_grimoires/auto.sh @@ -1,3 +1,7 @@ +#!/bin/bash + +# ``history`` command is not in POSIX + cd "$(eval_gettext '$GSH_HOME/Castle/Main_building/Library/Merlin_s_office')" # les commandes ne sont pas dans l'historique, il faut les y ajouter à la main ! diff --git a/missions/stdin_stdout_stderr/04_stderr_dev-null_grimoires/check.sh b/missions/stdin_stdout_stderr/04_stderr_dev-null_grimoires/check.sh index 2ddaea82..ad18864e 100644 --- a/missions/stdin_stdout_stderr/04_stderr_dev-null_grimoires/check.sh +++ b/missions/stdin_stdout_stderr/04_stderr_dev-null_grimoires/check.sh @@ -1,7 +1,6 @@ -#!/bin/bash +#!/bin/sh -_mission_check() { - local pc +_mission_check() ( # TODO: for some unknown reason, redirecting the output of fc into another # command shifts the results: it then sees the "gsh check" command that # was used to run this function @@ -10,21 +9,24 @@ _mission_check() { # command pc=$(fc -nl -1 -1) - echo $pc | grep 'gsh\s\s*check' && return 1 + echo "$pc" | grep -q 'gsh\s\s*check' && return 1 - if [ -z "$(echo "$pc" | grep 'grep')" ] + if ! echo "$pc" | grep -q 'grep' then echo "$(gettext "Your previous command doesn't use the 'grep' command...")" return 1 fi - if ! diff -q "$GSH_VAR/list_grimoires_GSH" <(eval "$pc" | sort) > /dev/null + temp_file=$(mktemp) + eval "$pc" | sort >"$temp_file" + if cmp -s "$GSH_TMP/list_grimoires_GSH" "$temp_file" then - return 1 + rm -f "$temp_file" + return 0 else - return 0 + rm -f "$temp_file" + return 1 fi -} - +) _mission_check diff --git a/missions/stdin_stdout_stderr/04_stderr_dev-null_grimoires/clean.sh b/missions/stdin_stdout_stderr/04_stderr_dev-null_grimoires/clean.sh index b9a4b576..20cdc9ca 100644 --- a/missions/stdin_stdout_stderr/04_stderr_dev-null_grimoires/clean.sh +++ b/missions/stdin_stdout_stderr/04_stderr_dev-null_grimoires/clean.sh @@ -1,13 +1,13 @@ -#!/bin/bash +#!/bin/sh -if [ -f "$GSH_VAR/list_grimoires_RO" ] +if [ -f "$GSH_TMP/list_grimoires_RO" ] then while IFS= read file do rm "$file" - done < "$GSH_VAR/list_grimoires_RO" + done < "$GSH_TMP/list_grimoires_RO" fi unset file -rm -f "$GSH_VAR/list_grimoires_RO" -rm -f "$GSH_VAR/list_grimoires_GSH" +rm -f "$GSH_TMP/list_grimoires_RO" +rm -f "$GSH_TMP/list_grimoires_GSH" diff --git a/missions/stdin_stdout_stderr/04_stderr_dev-null_grimoires/init.sh b/missions/stdin_stdout_stderr/04_stderr_dev-null_grimoires/init.sh index 73b3171b..aae210db 100644 --- a/missions/stdin_stdout_stderr/04_stderr_dev-null_grimoires/init.sh +++ b/missions/stdin_stdout_stderr/04_stderr_dev-null_grimoires/init.sh @@ -1,23 +1,25 @@ -#!/bin/bash +#!/bin/sh _mission_init() ( #subshell to avoid changing directory - local bib="$(eval_gettext '$GSH_HOME/Castle/Main_building/Library/Merlin_s_office')" + bib="$(eval_gettext '$GSH_HOME/Castle/Main_building/Library/Merlin_s_office')" find "$bib" -type f -name "$(gettext "grimoire")_*" -print0 | xargs -0 rm -f - rm -f "$GSH_VAR/list_grimoires_RO" + rm -f "$GSH_TMP/list_grimoires_RO" - local i - for i in $(seq 100) + RANDOM 600 | for i in $(seq 100) do - local file="$bib/$(gettext "grimoire")_$(random_string "$((8+RANDOM%24))")" + read RANDOM + file="$bib/$(gettext "grimoire")_$(random_string "$((8+RANDOM%24))")" + read RANDOM random_string $((100 + RANDOM%100)) > "$file" + read RANDOM if [ $((RANDOM % 2)) -eq 0 ] then # note: case insensitive matching isn't POSIX! sed-i "s/[gG][sS][hH]//g" "$file" else - local pattern + read RANDOM case "$((RANDOM % 8))" in 0) pattern=gsh ;; 1) pattern=gsH ;; @@ -28,21 +30,23 @@ _mission_init() ( #subshell to avoid changing directory 6) pattern=GSh ;; 7) pattern=GSH ;; esac - local n=$((1 + RANDOM%100)) + read RANDOM + n=$((1 + RANDOM%100)) sed-i "s/\\(.\\{$n\\}\\)/\\1$pattern/" "$file" fi + read RANDOM if [ $((RANDOM % 2)) -eq 0 ] then chmod -r "$file" - echo "$file" >> "$GSH_VAR/list_grimoires_RO" + echo "$file" >> "$GSH_TMP/list_grimoires_RO" fi [ $((i%10)) -eq 0 ] && printf "." done - printf "\n" + printf '\n' cd "$bib" - grep -il "gsh" * 2> /dev/null | sort > "$GSH_VAR/list_grimoires_GSH" + grep -il -- "gsh" * 2> /dev/null | sort > "$GSH_TMP/list_grimoires_GSH" ) . progress_bar.sh _mission_init diff --git a/missions/stdin_stdout_stderr/04_stderr_dev-null_grimoires/static.sh b/missions/stdin_stdout_stderr/04_stderr_dev-null_grimoires/static.sh index b9ad8491..dc5cfe15 100644 --- a/missions/stdin_stdout_stderr/04_stderr_dev-null_grimoires/static.sh +++ b/missions/stdin_stdout_stderr/04_stderr_dev-null_grimoires/static.sh @@ -1 +1,3 @@ +#!!/bin/sh + mkdir -p "$(eval_gettext '$GSH_HOME/Castle/Main_building/Library/Merlin_s_office')" diff --git a/missions/stdin_stdout_stderr/05_stdout_stderr_redirection_merlin/auto.sh b/missions/stdin_stdout_stderr/05_stdout_stderr_redirection_merlin/auto.sh index 00d5ee06..98624d7a 100644 --- a/missions/stdin_stdout_stderr/05_stdout_stderr_redirection_merlin/auto.sh +++ b/missions/stdin_stdout_stderr/05_stdout_stderr_redirection_merlin/auto.sh @@ -1,2 +1,8 @@ -gsh check < <("$(eval_gettext '$GSH_HOME/Castle/Observatory')"/merlin 2>&1 1>/dev/null) +#!/bin/sh + +key=$(mktemp) +"$(eval_gettext '$GSH_HOME/Castle/Observatory')"/merlin 2>"$key" + +gsh check <"$key" +rm -f "$key" diff --git a/missions/stdin_stdout_stderr/05_stdout_stderr_redirection_merlin/check.sh b/missions/stdin_stdout_stderr/05_stdout_stderr_redirection_merlin/check.sh index bc91edd7..fae1cc06 100644 --- a/missions/stdin_stdout_stderr/05_stdout_stderr_redirection_merlin/check.sh +++ b/missions/stdin_stdout_stderr/05_stdout_stderr_redirection_merlin/check.sh @@ -1,10 +1,10 @@ -#!/bin/bash +#!/bin/sh -_mission_check() { - local secret=$(cat "$GSH_VAR/secret_key") +_mission_check() ( + secret=$(cat "$GSH_TMP/secret_key") - local r - read -erp "$(gettext "What is the secret key?") " r + printf "%s " "$(gettext "What is the secret key?")" + read -r r if [ "$secret" != "$r" ] then @@ -19,6 +19,6 @@ _mission_check() { return 1 fi return 0 -} +) _mission_check diff --git a/missions/stdin_stdout_stderr/05_stdout_stderr_redirection_merlin/clean.sh b/missions/stdin_stdout_stderr/05_stdout_stderr_redirection_merlin/clean.sh index 10a9fb95..e348b0f2 100644 --- a/missions/stdin_stdout_stderr/05_stdout_stderr_redirection_merlin/clean.sh +++ b/missions/stdin_stdout_stderr/05_stdout_stderr_redirection_merlin/clean.sh @@ -1 +1,3 @@ -rm -f "$GSH_VAR/secret_key" +#!/bin/sh + +rm -f "$GSH_TMP/secret_key" diff --git a/missions/stdin_stdout_stderr/05_stdout_stderr_redirection_merlin/init.sh b/missions/stdin_stdout_stderr/05_stdout_stderr_redirection_merlin/init.sh index fbab0a76..245958fe 100644 --- a/missions/stdin_stdout_stderr/05_stdout_stderr_redirection_merlin/init.sh +++ b/missions/stdin_stdout_stderr/05_stdout_stderr_redirection_merlin/init.sh @@ -1,11 +1,10 @@ -#!/bin/bash +#!/bin/sh -_mission_init() { +_mission_init() ( mkdir -p "$(eval_gettext '$GSH_HOME/Castle/Observatory')" - random_string 200 > "$GSH_VAR/secret_key" + random_string 200 > "$GSH_TMP/secret_key" - local CC if command -v cc >/dev/null then CC=cc @@ -37,20 +36,20 @@ _mission_init() { # "-lintl" to the compiler, so we have to try several things! { echo "GSH: compiling merlin.c, first try" >&2 - echo $CC "$MISSION_DIR/merlin.c" -lpthread -o "$GSH_VAR/merlin" - $CC "$MISSION_DIR/merlin.c" -lpthread -o "$GSH_VAR/merlin" + echo $CC "$MISSION_DIR/merlin.c" -lpthread -o "$GSH_TMP/merlin" + $CC "$MISSION_DIR/merlin.c" -lpthread -o "$GSH_TMP/merlin" } || { echo "GSH: compiling merlin.c, second try" - echo $CC -I/usr/local/include/ -L/usr/local/lib "$MISSION_DIR/merlin.c" -lintl -lpthread -o "$GSH_VAR/merlin" - $CC -I/usr/local/include/ -L/usr/local/lib "$MISSION_DIR/merlin.c" -lintl -lpthread -o "$GSH_VAR/merlin" + echo $CC -I/usr/local/include/ -L/usr/local/lib "$MISSION_DIR/merlin.c" -lintl -lpthread -o "$GSH_TMP/merlin" + $CC -I/usr/local/include/ -L/usr/local/lib "$MISSION_DIR/merlin.c" -lintl -lpthread -o "$GSH_TMP/merlin" } ) || { echo "compilation failed" >&2; return 1; } - copy_bin "$GSH_VAR/merlin" "$(eval_gettext '$GSH_HOME/Castle/Observatory')/merlin" + copy_bin "$GSH_TMP/merlin" "$(eval_gettext '$GSH_HOME/Castle/Observatory')/merlin" else copy_bin "$MISSION_DIR"/merlin.sh "$(eval_gettext '$GSH_HOME/Castle/Observatory')/merlin" chmod 755 "$(eval_gettext '$GSH_HOME/Castle/Observatory')/merlin" fi -} +) _mission_init diff --git a/missions/stdin_stdout_stderr/05_stdout_stderr_redirection_merlin/merlin.c b/missions/stdin_stdout_stderr/05_stdout_stderr_redirection_merlin/merlin.c index 6077ed92..e3366543 100644 --- a/missions/stdin_stdout_stderr/05_stdout_stderr_redirection_merlin/merlin.c +++ b/missions/stdin_stdout_stderr/05_stdout_stderr_redirection_merlin/merlin.c @@ -45,9 +45,9 @@ int main(int argc, char** argv) // the message char* msg = gettext("THESECRETKEYISONSTDERR"); - // get the key from "$GSH_VAR/secret_key" + // get the key from "$GSH_TMP/secret_key" wordexp_t result; - wordexp("$GSH_VAR/secret_key", &result, 0); + wordexp("$GSH_TMP/secret_key", &result, 0); // if the path contains spaces, the result is contained in several words! int pos = 0; for (int i = 0; result.we_wordv[i] != NULL; i++) { diff --git a/missions/stdin_stdout_stderr/05_stdout_stderr_redirection_merlin/merlin.sh b/missions/stdin_stdout_stderr/05_stdout_stderr_redirection_merlin/merlin.sh index 4ed25d3d..f72d771f 100755 --- a/missions/stdin_stdout_stderr/05_stdout_stderr_redirection_merlin/merlin.sh +++ b/missions/stdin_stdout_stderr/05_stdout_stderr_redirection_merlin/merlin.sh @@ -7,13 +7,13 @@ main() { if [ -n "$*" ] then cmd_name=$(basename "$0") - msg="$(gettext 'Error: %s takes no argument.')" + msg=$(gettext 'Error: %s takes no argument.') printf "$msg\n" "$cmd_name" >&2 return 1 fi msg=$(gettext "THESECRETKEYISONSTDERR") - key=$(cat "$GSH_VAR/secret_key") + key=$(cat "$GSH_TMP/secret_key") I=${#msg} J=${#key} diff --git a/missions/stdin_stdout_stderr/05_stdout_stderr_redirection_merlin/static.sh b/missions/stdin_stdout_stderr/05_stdout_stderr_redirection_merlin/static.sh index ced5499c..bcd0a359 100644 --- a/missions/stdin_stdout_stderr/05_stdout_stderr_redirection_merlin/static.sh +++ b/missions/stdin_stdout_stderr/05_stdout_stderr_redirection_merlin/static.sh @@ -1,2 +1,4 @@ +#!/bin/sh + mkdir -p "$(eval_gettext '$GSH_HOME/Castle/Observatory')" sign_file "$MISSION_DIR/ascii-art/moon.txt" "$(eval_gettext '$GSH_HOME/Castle/Observatory')/$(gettext "star_chart")" diff --git a/start.sh b/start.sh index 12f41dd6..512130be 100755 --- a/start.sh +++ b/start.sh @@ -192,7 +192,7 @@ Do you want to remove it and start a new game? [y/N]') " # remove all the game data rm -rf "$GSH_HOME" rm -rf "$GSH_CONFIG" - rm -rf "$GSH_VAR" + rm -rf "$GSH_TMP" rm -rf "$GSH_BIN" rm -rf "$GSH_SBIN" @@ -217,7 +217,8 @@ Do you want to remove it and start a new game? [y/N]') " mkdir -p "$GSH_BIN" mkdir -p "$GSH_SBIN" - mkdir -p "$GSH_VAR" + mkdir -p "$GSH_TMP" + export TMPDIR="$GSH_TMP" # id of player @@ -354,8 +355,10 @@ Do you want to remove it and start a new game? [y/N]') " if [ -f "$MISSION_DIR/gshrc" ] then BASHRC_FILE=$GSH_CONFIG/gshrc_${FULL_NB}_$(basename "$MISSION_DIR").sh - echo "export MISSION_DIR=\"$MISSION_DIR\"" > "$BASHRC_FILE" - echo "export TEXTDOMAIN=\"$DOMAIN\"" >> "$BASHRC_FILE" + { + echo "export MISSION_DIR=\"$MISSION_DIR\""; + echo "export TEXTDOMAIN=\"$DOMAIN\""; + } >"$BASHRC_FILE" cat "$MISSION_DIR/gshrc" >> "$BASHRC_FILE" echo "export TEXTDOMAIN=gsh" >> "$BASHRC_FILE" unset BASHRC_FILE diff --git a/utils/archive.sh b/utils/archive.sh index d409dc2b..2097de1b 100755 --- a/utils/archive.sh +++ b/utils/archive.sh @@ -90,7 +90,14 @@ shift $((OPTIND - 1)) OUTPUT_DIR=$(dirname "$NAME") NAME=$(basename "$NAME") -TMP_DIR=$(mktemp -d) +# don't use mktemp -d, which might not exist +while true +do + n=$((n+1)) + TMP_DIR="$OUTPUT_DIR/tmp-$(printf "%03d" $n)" + [ -e "$TMP_DIR" ] || break +done +mkdir "$TMP_DIR" mkdir "$TMP_DIR/$NAME" diff --git a/utils/new_mission.sh b/utils/new_mission.sh index 1327b7a5..0ae93968 100755 --- a/utils/new_mission.sh +++ b/utils/new_mission.sh @@ -143,7 +143,7 @@ new_auto_file() { # This file is not required. When it exists, it is used to automatically # validate the mission. It should end with a succesful `gsh check` command. -# It is sometimes possible to "cheat" by using any hidden data in $GSH_VAR, +# It is sometimes possible to "cheat" by using any hidden data in $GSH_TMP, # but it is better to do it the "intended" way. # If you write this file, rename it to auto.sh EOF