I get easily confused when nesting the various control constructs in
bash shell scripts.
Here we have an `while' inside an `if' inside a `case' inside a
`while'.
(and the thigh bones connected to the knee bone...)
Probably should be easy to get the format right, but not for me. Not
all is shown here. For breivity I've snipped a couple of `here
documents' and commments. When I try to run this with the "-w" flag
and 3 args. -w(1) "something"(2) non-file(3)
(using a non-file name to test)
I get this error... :
line 71: syntax error near unexpected token `;'
line 71: ` done ;; '
[I've rearranged the double colons the fis and the
dones many times and the error message changes accordingly,
but having a problem seeing my mistake]
I'd like some advice on how to debug this kind of problem easily.
Also a related syntactical question: In the `if' clause I've
attempted to capture the argument count:
` cnt="$#"'
And presented it for a file test:
if [ -f "$($cnt)" ] ;then
Trying to determine if last arg is infact a file.
But not sure if this is how that would be done.
Pseudo script:
^^^^^^^^^^^^^^^^^^^^^^
[`Here docs' snipped [...] various usage statements]
What is wrong in this picture?
This script, with -w flag expects the user to insert data and hit ^d to write
it to the selected file.
[ -z "$1" ] && {
usage1
exit 1
}
while getopts "hHw:r:" opt; do
case "$opt" in
w) cnt="$#"
## wants to see if last arg is legit filename
if [ -f "$($cnt)" ] ;then
## save OPTARG for writing
wdata=`echo "Something: $OPTARG"`
shift
echo "$wdata" >> $1
date +"%b %d %T %Y" >> $1
echo "Hit ^d when done to write data to $2"
while read line
do
echo $line >> $1
done ;;
else
echo ""
echo "WAIT...$cnt either doesn't exist or is not a regular file"
echo "type \`$(basename $0) -h' for minimal usage statement"
echo "type \`$(basename $0) -H' for maximum usage statement"
echo ""
exit 1
fi
r) awk '/^Keywords:.*'"$OPTARG"'/,/\&\&$/' $3 ;;
h) usage1 ;;
H) usage2 ;;
*) usage1 ;;
esac
done