summaryrefslogtreecommitdiffstats
path: root/guides/shell-workshop/session.html
diff options
context:
space:
mode:
Diffstat (limited to 'guides/shell-workshop/session.html')
-rw-r--r--guides/shell-workshop/session.html381
1 files changed, 381 insertions, 0 deletions
diff --git a/guides/shell-workshop/session.html b/guides/shell-workshop/session.html
new file mode 100644
index 0000000..1a5da6e
--- /dev/null
+++ b/guides/shell-workshop/session.html
@@ -0,0 +1,381 @@
+<h2>Shell Input</h2>
+
+<pre><code>sh@sleipnir1:~$ vim hello.sh
+sh@sleipnir1:~$ cat hello.sh
+echo Hello world
+sh@sleipnir1:~$ sh hello.sh
+Hello world
+sh@sleipnir1:~$ sh -c 'echo Hello world'
+Hello world
+sh@sleipnir1:~$ echo Hello world
+Hello world
+</code></pre>
+
+<h2>Comments, Magic Number Behavior, and File Modes</h2>
+
+<pre><code>sh@sleipnir1:~$ vim hello.sh
+sh@sleipnir1:~$ cat hello.sh
+#! /bin/sh
+# This is a comment
+echo Hello world
+sh@sleipnir1:~$ ./hello.sh
+-su: ./hello.sh: Permission denied
+sh@sleipnir1:~$ ls -l hello.sh
+-rw-r--r-- 1 sh sh 48 Mar 30 13:26 hello.sh
+sh@sleipnir1:~$ chmod a+x hello.sh
+sh@sleipnir1:~$ ls -l hello.sh
+-rwxr-xr-x 1 sh sh 48 Mar 30 13:26 hello.sh
+sh@sleipnir1:~$ ./hello.sh
+Hello world
+</code></pre>
+
+<h2>Field Splitting and Quoting</h2>
+
+<pre><code>sh@sleipnir1:~$ mkdir foo bar
+sh@sleipnir1:~$ ls
+bar foo hello.sh
+sh@sleipnir1:~$ rmdir foo bar
+sh@sleipnir1:~$ mkdir foo\ bar
+sh@sleipnir1:~$ ls
+foo bar hello.sh
+sh@sleipnir1:~$ rmdir foo\ bar
+sh@sleipnir1:~$ ls
+hello.sh
+sh@sleipnir1:~$ mkdir 'foo bar
+&gt; baz'
+sh@sleipnir1:~$ ls
+foo bar?baz hello.sh
+sh@sleipnir1:~$ rmdir 'foo bar
+&gt; baz'
+sh@sleipnir1:~$ ls
+hello.sh
+sh@sleipnir1:~$ mkdir "foo bar baz"
+sh@sleipnir1:~$ ls
+foo bar baz hello.sh
+</code></pre>
+
+<h2>Tilde Expansion</h2>
+
+<pre><code>sh@sleipnir1:~$ echo ~
+/home/sh
+sh@sleipnir1:~$ ls ~sh-foo
+another-directory another-file bar baz foo some-directory some-file
+sh@sleipnir1:~$ ls ~sh-foo/some-directory
+Hey, this is a cool file!
+</code></pre>
+
+<h2>Pathname Expansion</h2>
+
+<pre><code>sh@sleipnir1:~$ echo ~sh-foo/*-file
+/home/sh-foo/another-file /home/sh-foo/some-file
+sh@sleipnir1:~$ echo ~sh-foo/ba*/file
+/home/sh-foo/bar/file /home/sh-foo/baz/file
+sh@sleipnir1:~$ echo ~sh-foo/ba[a-r]/file
+/home/sh-foo/bar/file
+sh@sleipnir1:~$ echo ~sh-foo/ba[p-z]/file
+/home/sh-foo/bar/file /home/sh-foo/baz/file
+</code></pre>
+
+<h2>Parameter Assignment</h2>
+
+<pre><code>sh@sleipnir1:~$ myparam=foo
+sh@sleipnir1:~$ mynum=1
+</code></pre>
+
+<h2>Parameter Expansion</h2>
+
+<pre><code>sh@sleipnir1:~$ echo ${myparam}
+foo
+sh@sleipnir1:~$ echo ${nullparam:-default}
+default
+sh@sleipnir1:~$ echo ${#myparam}
+3
+sh@sleipnir1:~$ file=main.c
+sh@sleipnir1:~$ echo ${file%.c}.o
+main.o
+sh@sleipnir1:~$ dir=foo/bar/baz
+sh@sleipnir1:~$ echo ${dir##*/}
+baz
+</code></pre>
+
+<h2>Positional Parameters</h2>
+
+<pre><code>sh@sleipnir1:~$ vim pos-params.sh
+sh@sleipnir1:~$ cat pos-params.sh
+#! /bin/sh
+echo ${1}
+echo ${2}
+sh@sleipnir1:~$ chmod a+x pos-params.sh
+sh@sleipnir1:~$ ./pos-params.sh foo
+foo
+
+sh@sleipnir1:~$ sh pos-params.sh foo bar baz
+foo
+bar
+</code></pre>
+
+<h2>Special Parameters</h2>
+
+<pre><code>sh@sleipnir1:~$ vim spec-params.sh
+sh@sleipnir1:~$ cat spec-params.sh
+#! /bin/sh
+
+echo Number of positional parameters: ${#}
+echo Positional parameters: ${@}
+echo Script name: ${0}
+sh@sleipnir1:~$ sh spec-params.sh foo bar baz
+Number of positional parameters: 3
+Positional parameters: foo bar baz
+Script name: spec-params.sh
+</code></pre>
+
+<h2>Exit Statuses</h2>
+
+<pre><code>sh@sleipnir1:~$ vim spec-params.sh
+sh@sleipnir1:~$ cat spec-params.sh
+#! /bin/sh
+
+echo Number of positional parameters: ${#}
+echo Positional parameters: ${@}
+echo Script name: ${0}
+true
+echo ${?}
+false
+echo ${?}
+sh@sleipnir1:~$ sh spec-params.sh foo bar baz
+Number of positional parameters: 3
+Positional parameters: foo bar baz
+Script name: spec-params.sh
+0
+1
+</code></pre>
+
+<h2>Shell Variables and Links <code>.</code> and <code>..</code></h2>
+
+<pre><code>sh@sleipnir1:~$ echo ${HOME}
+/home/sh
+sh@sleipnir1:~$ echo ${PWD}
+/home/sh
+sh@sleipnir1:~$ ls
+foo bar baz hello.sh pos-params.sh spec-params.sh
+sh@sleipnir1:~$ cd foo\ bar\ baz/
+sh@sleipnir1:~/foo bar baz$ echo ${PWD}
+/home/sh/foo bar baz
+sh@sleipnir1:~/foo bar baz$ ls .
+sh@sleipnir1:~/foo bar baz$ mkdir file1 file2
+sh@sleipnir1:~/foo bar baz$ ls .
+file1 file2
+sh@sleipnir1:~/foo bar baz$ ls ..
+foo bar baz hello.sh pos-params.sh spec-params.sh
+sh@sleipnir1:~/foo bar baz$ cd ..
+sh@sleipnir1:~$ echo ${PWD}
+/home/sh
+sh@sleipnir1:~$ PS1='$ '
+$ PS1='PROMPT&gt; '
+PROMPT&gt; echo Hello
+Hello
+PROMPT&gt; PS1='$ '
+$ echo Hello \
+&gt; World
+Hello World
+</code></pre>
+
+<h2>Command Substitution</h2>
+
+<pre><code>$ filepath=$(basename ~sh-foo/bar/file)
+$ echo ${filepath}
+file
+</code></pre>
+
+<h2>Arithmetic Expansion</h2>
+
+<pre><code>$ echo $((2+3))
+5
+$ echo $((0x10 + 4))
+20
+$ i=0
+$ i=$(($i + 1))
+$ echo ${i}
+1
+$ i=$(($i + 1))
+$ echo ${i}
+2
+$ i=$(($i + 1))
+$ echo ${i}
+3
+</code></pre>
+
+<h2>Simple Commands</h2>
+
+<pre><code>$ i=0 echo Hello
+Hello
+</code></pre>
+
+<h2>Command Search and Execution</h2>
+
+<pre><code>$ echo ${PATH}
+/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
+$ ./hello.sh
+Hello world
+$ hello.sh
+-su: hello.sh: command not found
+$ ls
+foo bar baz hello.sh pos-params.sh spec-params.sh
+$ PATH=${PATH}:${HOME}
+$ echo ${PATH}
+/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/home/sh
+$ hello.sh
+Hello world
+</code></pre>
+
+<h2>Pipelines</h2>
+
+<pre><code>$ ! true
+$ echo ${?}
+1
+$ ! false
+$ echo ${?}
+0
+$ true
+$ echo ${?}
+0
+$ false
+$ echo ${?}
+1
+</code></pre>
+
+<h2>Lists</h2>
+
+<pre><code>$ false &amp;&amp; echo foo || echo bar
+bar
+$ true || echo foo &amp;&amp; echo bar
+bar
+</code></pre>
+
+<h2><code>test</code> Command and <code>if</code> Construct</h2>
+
+<pre><code>$ [ foo = foo ] &amp;&amp; echo equal || echo not equal
+equal
+$ if [ foo = foo ]; then echo equal else echo not equal; fi
+equal else echo not equal
+$ if [ foo = foo ]; then echo equal; else echo not equal; fi
+equal
+$ if [ foo = foo ]; then
+&gt; echo equal
+&gt; else
+&gt; echo not equal
+&gt; fi
+equal
+</code></pre>
+
+<h2><code>while</code> Loop</h2>
+
+<pre><code>$ i=0
+$ while [ ${i} -lt 5 ]; do
+&gt; echo ${i}
+&gt; i=$(($i + 1))
+&gt; done
+0
+1
+2
+3
+4
+$ vim param-loop.sh
+$ cat param-loop.sh
+while [ ${#} -gt 0 ]; do
+ echo Parameter: ${1}
+ shift
+done
+$ sh param-loop.sh foo bar baz
+Parameter: foo
+Parameter: bar
+Parameter: baz
+</code></pre>
+
+<h2><code>for</code> Loop</h2>
+
+<pre><code>$ for param in foo bar baz; do
+&gt; echo ${param}
+&gt; done
+foo
+bar
+baz
+</code></pre>
+
+<h2><code>case</code> Construct</h2>
+
+<pre><code>$ param=foo
+$ case ${param} in
+&gt; f*)
+&gt; echo starts with f
+&gt; ;;
+&gt; b*)
+&gt; echo starts with b
+&gt; ;;
+&gt; esac
+starts with f
+</code></pre>
+
+<h2>Functions</h2>
+
+<pre><code>$ print_hello()
+&gt; {
+&gt; echo Hello world
+&gt; }
+$ print_hello
+Hello world
+$ print_params()
+&gt; {
+&gt; echo ${@}
+&gt; }
+$ print_params 1 2 3
+1 2 3
+</code></pre>
+
+<h2>Input/Output Redirection</h2>
+
+<pre><code>$ echo Hello world &gt; hello-file
+$ cat hello-file
+Hello world
+$ echo Hello world &gt; hello-file
+$ echo Hello world &gt; hello-file
+$ echo Hello world &gt; hello-file
+$ cat hello-file
+Hello world
+$ echo Hello world &gt;&gt; hello-file
+$ echo Hello world &gt;&gt; hello-file
+$ cat hello-file
+Hello world
+Hello world
+Hello world
+$ vim read-file.sh
+$ cat read-file.sh
+while read line; do
+ echo Line: ${line}
+done &lt; myfile.txt
+$ sh read-file.sh
+Line: line one
+Line: line two
+Line: some other line
+$ vim read-file.sh
+$ cat read-file.sh
+while read field1 field2; do
+ echo Line: ${field1}, ${field2}
+done &lt; myfile.txt
+$ sh read-file.sh
+Line: line, one
+Line: line, two
+Line: some, other line
+$ echo Hello world &gt; /dev/null
+$ sh -c not-a-command
+sh: 1: not-a-command: not found
+$ sh -c not-a-command 2&gt; /dev/null
+$ while read line; do
+&gt; echo Line: ${line}
+&gt; done &lt;&lt;EOF
+&gt; this is input
+&gt; some more input
+&gt; EOF
+Line: this is input
+Line: some more input
+</code></pre>