Permalink
Browse files

status codes, if statements, and test/[

  • Loading branch information...
1 parent ae76fa8 commit d86f2cf6ebde19accc8b7986129177dc8b0607be @singpolyma committed Apr 19, 2012
Showing with 163 additions and 1 deletion.
  1. +1 −1 step13
  2. +26 −0 step14
  3. +38 −0 step15
  4. +32 −0 step16
  5. +35 −0 step17
  6. +31 −0 step18
View
2 step13
@@ -46,4 +46,4 @@ while [ "`cat saved`" != "1st thing
fakeshell
done
-echo "That's all we have so far, more soon."
+./step14
View
26 step14
@@ -0,0 +1,26 @@
+#!/bin/sh
+
+fakeshell() {
+ echo
+ oldifs="$IFS"
+ IFS=""
+ read -p "$ " -r COMMAND
+ IFS="$oldifs"
+ if [ "$COMMAND" = "exit" ]; then exit; fi
+ OUTPUT="`eval "$COMMAND" 2>&1`"
+ echo "$OUTPUT"
+ echo
+}
+
+BOLD="\033[1m"
+NORMAL="\033[0m"
+
+echo
+
+OUTPUT=""
+while ! echo "$OUTPUT" | grep 'No such file' > /dev/null; do
+ printf "Try typing ${BOLD}mv nothere here${NORMAL}\n"
+ fakeshell
+done
+
+./step15
View
38 step15
@@ -0,0 +1,38 @@
+#!/bin/sh
+
+fakeshell() {
+ echo
+ oldifs="$IFS"
+ IFS=""
+ read -p "$ " -r COMMAND
+ IFS="$oldifs"
+ if [ "$COMMAND" = "exit" ]; then exit; fi
+ OUTPUT="`eval "$COMMAND" 2>&1`"
+ echo "$OUTPUT"
+ echo
+}
+
+BOLD="\033[1m"
+NORMAL="\033[0m"
+
+printf "${BOLD}mv${NORMAL} is the command to move or rename a file. "
+printf "When we try to move a file that doesn't exist, we get an error. "
+printf "How can we detect this error automatically?"
+echo; echo
+printf "Well, we could try to search the output for the error message, "
+printf "but what if the message changes? Or what if there is a "
+printf "different error?"
+echo; echo
+printf "To solve this problem, commands have what is called an "
+printf "${BOLD}exit status code${NORMAL}, which is a number they give "
+printf "to the shell when they exit to say if they had an error or not. "
+printf "0 always means 'success' and any other number is some kind of "
+printf "error."
+
+OUTPUT=""
+while ! echo "$OUTPUT" | grep 'No such file' > /dev/null; do
+ printf "Try typing ${BOLD}mv nothere here; echo \$?${NORMAL}\n"
+ fakeshell
+done
+
+./step16
View
32 step16
@@ -0,0 +1,32 @@
+#!/bin/sh
+
+fakeshell() {
+ echo
+ oldifs="$IFS"
+ IFS=""
+ read -p "$ " -r COMMAND
+ IFS="$oldifs"
+ if [ "$COMMAND" = "exit" ]; then exit; fi
+ OUTPUT="`eval "$COMMAND" 2>&1`"
+ echo "$OUTPUT"
+ echo
+}
+
+BOLD="\033[1m"
+NORMAL="\033[0m"
+
+printf "Right after the error message, a number is printed. The number "
+printf "is not a 0, so this indicates an error. The shell lets you "
+printf "get the ${BOLD}exit status code${NORMAL} of the most recent "
+printf "command using ${BOLD}\$?${NORMAL}."
+echo; echo
+printf "What can we do with this? Well, let's start with something "
+printf "simple.\n"
+
+OUTPUT=""
+while ! echo "$OUTPUT" | grep 'not worked' > /dev/null; do
+ printf "Try typing ${BOLD}if mv nothere here; then echo worked; else echo not worked; fi${NORMAL}\n"
+ fakeshell
+done
+
+./step17
View
35 step17
@@ -0,0 +1,35 @@
+#!/bin/sh
+
+fakeshell() {
+ echo
+ oldifs="$IFS"
+ IFS=""
+ read -p "$ " -r COMMAND
+ IFS="$oldifs"
+ if [ "$COMMAND" = "exit" ]; then exit; fi
+ OUTPUT="`eval "$COMMAND" 2>&1`"
+ echo "$OUTPUT"
+ echo
+}
+
+BOLD="\033[1m"
+NORMAL="\033[0m"
+
+printf "${BOLD}if [COMMAND]; then [COMMANDS]; else [COMMANDS]; fi${NORMAL} "
+printf "is the way we get the shell to run different commands depending "
+printf "on whether the command fails or not. There are several commands "
+printf "designed specifically for use with these sorts of constructs "
+printf "including ${BOLD}true${NORMAL}, which always succeeds, "
+printf "${BOLD}false${NORMAL}, which alsways fails, and "
+printf "${BOLD}test${NORMAL} (also known as ${BOLD}[${NORMAL}), "
+printf "which tests some condition and succeeds or fails based on if "
+printf "the condition is true or not."
+echo; echo
+
+OUTPUT=""
+while ! echo "$OUTPUT" | grep '^worked' > /dev/null; do
+ printf "Try typing ${BOLD}if test \"hai\" = \"hai\"; then echo worked; else echo not worked; fi${NORMAL}\n"
+ fakeshell
+done
+
+./step18
View
31 step18
@@ -0,0 +1,31 @@
+#!/bin/sh
+
+fakeshell() {
+ echo
+ oldifs="$IFS"
+ IFS=""
+ read -p "$ " -r COMMAND
+ IFS="$oldifs"
+ if [ "$COMMAND" = "exit" ]; then exit; fi
+ OUTPUT="`eval "$COMMAND" 2>&1`"
+ echo "$OUTPUT"
+ echo
+}
+
+BOLD="\033[1m"
+NORMAL="\033[0m"
+
+printf "Because it is used so much, ${BOLD}test${NORMAL} has another "
+printf "name, ${BOLD}[${NORMAL}. It works exactly the same when you use "
+printf "this other name, except that you have to pass it a "
+printf "${BOLD}]${NORMAL} as the last argument, which sounds a bit weird, "
+printf "but makes things look nice."
+echo; echo
+
+OUTPUT=""
+while ! echo "$OUTPUT" | grep '^worked' > /dev/null; do
+ printf "Try typing ${BOLD}if [ \"hai\" = \"hai\" ]; then echo worked; else echo not worked; fi${NORMAL}\n"
+ fakeshell
+done
+
+echo "That's all we have so far, more soon."

0 comments on commit d86f2cf

Please sign in to comment.