home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 46.1 Tips for Debugging Shell Scripts Chapter 46
Shell Script Debugging and Gotchas
Next: 46.3 Bourne Shell Debugger Shows a Shell Variable
 

46.2 Quoting Trouble? Think, Then Use echo

Q: I can't get the following shell script to work:

Q:

case $col2 in
"income") awk '{if($2=='$col2') {   /* THIS LINE IS THE PROBLEM */
          /* I CAN'T GET AWK TO RECOGNIZE EITHER '$col2' or '$2' */
                .
                .

          } ' $file1 ;;

A: It is clear from this code fragment that awk is supposed to compare $2 with "income" . If you think about it (or change awk to echo above), you will see that you have given the following to awk :

A:

{if($2==income) { /* THIS LINE IS THE PROBLEM */

A: What does awk do with this? It compares $2 with the contents of the variable income . If income has not been set, it compares it with zero or with the null string. Instead, you want:

A:

{ if ($2 == "income") {

A: which you can say with:

A:

case $col2 in
income)
        awk '
        {
                if ($2 == "'$col2'") {
                        ... awk code ...
                }
        }' $file1;;

Replacing commands with echo in shell scripts is a handy debugging trick.

- CT in net.unix on Usenet, 1 November 1986


Previous: 46.1 Tips for Debugging Shell Scripts UNIX Power Tools Next: 46.3 Bourne Shell Debugger Shows a Shell Variable
46.1 Tips for Debugging Shell Scripts Book Index 46.3 Bourne Shell Debugger Shows a Shell Variable

The UNIX CD Bookshelf Navigation The UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System