Richard's Linux Ch 10

=================
Linux is case sensitive
=================

Ch 10. Programming the Bourne Again Shell

Do not name your shell script test (a Linux utility has the same name).

To set vim to have a tabstop of 4 characters (windows system):
Go to vi, type the following at command mode:
:set ts=4 et sw=2 sts=2
or simpler option
:set ts=4
to reset to original
:set ts=8 et sw=4 sts=4

Example 1 (eg01_ifl), if ... then ... fi
vi eg01_ifl
chmod u+x eg01_ifl
./eg01_ifl -------------- can run in vi command mode :!./eg01_ifl

Example 2 (eg02_chkargs)
test -eq: compares two integers
$# special parameter takes on the value of number of command line arguments
./eg02_chkargs abc

Example 3 (eg03_is_ordfile)
$1 : first argument
-f of test: option exists and is an ordinary file
other test options, check Linux textbook pg 401

Example 4 (eg04_chkargs2)
Usage message, 1>&2 redirect output to standard error

Example 5 (eg05_out)
Create your own option

Example 6 (eg06_if3)
if ... then ... elif

Degugging shell scripts
use -x option to display each command before it executes
$ bash -x eg06_if3
or you can put set -x anywhere in script to turn debugging on
or you can put set +x anywhere in script to turn debugging off

Example 7 (eg07_for)
for ... in ... do ... done
list all directory in home directory (to check test options: man test or help test | less)

Example 8 (eg08_forarg)
use for to list arguments
$ ./eg08_forarg abc 123 Mary John

Example 9 (eg09_while)
while ... do ... done

Example 10 (eg10_until)
until ... do ... done

Example 11 (eg11_locktty)
Lock terminal, ask user for key to lock and unlock
If you forgot the key, login from another virtual terminal, and kill the process

Example 12 (eg12_case)
case ... in ... esac
convert to uppercase of a variable

Example 13 (eg13_case2)
case ... in ... esac
Seperate alternatives in case (can use other patterns)

Example 14 (eg14_answer)
while and case to ask for answer

Example 15 (eg15_select)
Select ... in ... do ... done
A simple way to create a menu using while

Example 16 (eg16_array)
Array variables, arithmetic evaluation, logical evaluation (conditional expressions)

 

Something Extra
$ clear ----------------- clear screen
$ date +%r ----------- show time (12-hour clock), + means format