egrep "^$1$" /usr/share/dict/words > /dev/null <-- Takes the first argument when executing the shell script and looks through words
if [ $? -eq 0 ]; then
echo Not a good password
else
echo Maybe a good password
fiThere is a problem with this script. The script depends on the fact that there is exactly one argument.
We can add the following as an error check:
This will check if there is one argument and if not it will show the usage by printing $0 which is the path of the file and "password" to indicate that a parameter is expected.
if [ ${#} -ne 1 ]; then
echo "Usage: $0 password"
exit 1
fi- e.g.) Print #s from 1 to ${1}
x=1
while [ ${x} -le $1 ];
do
echo ${x}
x=$((x + 1))
doneWe can modify the above by adding some checks:
usage () {
echo "Usage: $0 limit" 1>&2
echo " where limit is at least 1" 1>&2
exit 1
}This will check for the right number of arguments (1):
if [$# -ne 1]; then
usage
fiThis will check if the argument is less than 1:
if [ $1 -lt 1 ]; then
usage
fiNote: this redirects the stderr and writes to the err.log file.
./count 2> err.log
- e.g.) Rename all .c files to .cc
We have the command: mv file.c file.cc
To do this with variable file names:
file=hello.c
mv ${file} ${file%c}cc%c will remove the trailing suffix
cc will concatenate the new suffix
#abc removes 'abc' prefix - e.g.) abchello.c -> hello.c
Loop Syntax
for x in a b c d; <-- iterator = x, (a b c d) elements to iterate over
do echo $x; <-- do something
doneNote: we can change the delimiter. For instance, read a comma-separated CSV file
Going back to our example we can write our script to rename the files:
for name in *.c; do
mv ${name} ${name%c}cc
donee.g.) How many times does word $1 appear in file $2?
x=0
for word in (cat "$2"); do <-- brackets are necessary
if [ ${word} = "${1}" ]; then (check for word)
x=$((x + 1))
fi
done
echo $xNote: the " " around
e.g.) Write a script that tells you when you get paid by using cal and obtaining the last Friday of the month
answer () {
if [ $1 -eq 31 ]; then
echo "on the ${1}st."
else
echo "on the ${1}th."
fi
}
if [ ${#} -ne 0 ]; then
echo "Usage: $0"
fi
answer cal | awk ' { print $6 } ' | egrep [0-9] | tail -n 1A variation of this is adding the ability to specify a month
answer () {
if [ $2 ]; then
preamble=${2}
else
preamble="This month"
fi
if [ $1 -eq 31 ]; then
echo "${preamble}'s payday is on the ${1}st."
else
echo "${preamble}'s payday is on the ${1}th."
fi
}
answer `cal $1 $2 | awk '{print $6}' | grep "[0-9]" | tail -1` $1- Writing tests before coding
- Functional testing - test features
- positive/negative
- edge cases - boundaries
- corner cases - two boundaries meet