JohnMcPherson 12 Junior Poster in Training

Hello, my name is John McPherson. I work for the DOL, in the Uneployment Insurance office, and I am working on an application that uses bash scripts as the controller running the application. I am trying to add a capability to email the output files to the users, which are the states that use the application. The case statement chooses which state to send the email to( or optionally not send an email if state does not want it), is erroring off. I am not sure what the problem is with code. The case statemnet in question is:

## print or display
    if test $print_flag
    then
      if [ $run = FF ]
      then
        rm -f picktbls
        /bfm/devt/comexec/prtmnu $showfl ##select tables >temp.d
        act=`cat temp.d`
        if [ "$act" = QUIT ]
        then ##canceled
          bfm_sigon
        elif [ "$act" != ALL ]
        then
          showfl=picktbls
        fi
      fi
                case $OPT1 in
                  ARKANSAS)
                    mailx -s "BFM Output" $ARUSER1 < $showfl;;
                  CONNECTICUT)
                    mailx -s "BFM Output" $CTUSER1 < $showfl;;
                  DC2)
                    mailx -s "BFM Output" $DCUSER3 < $showfl;;
                  GEORGIA)
                    mailx -s "BFM Output" $GAUSER1 < $showfl;;
                  INDIANA)
                    mailx -s "BFM Output" $INUSER1 $INUSER2 < $showfl;;
                  KANSAS)
                    mailx -s "BFM Output" $KSUSER1 $KSUSER2 < $showfl;;
                  KENTUCKY)
                    mailx -s "BFM Output" $KYUSER < $showfl;;
                  LOUISIANA)
                    mailx -s "BFM Output" $LAUSER1 $LAUSER2 < $showfl;;
                  MAINE)
                    mailx -s "BFM Output" $MEUSER1 $MEUSER2 $MEUSER3 < $showfl;;
                  MARYLAND)
                    mailx -s "BFM Output" $MDUSER2 < $showfl;;
                  MINNESOTA)
                    mailx -s "BFM Output" $MNUSER1 < $showfl;;
                  NORTH CAROLINA)
                    mailx -s "BFM Output" $NCUSER7 < $showfl;;
                  SOUTH CAROLINA)
                    mailx -s "BFM Output" $SCUSER1 $SCUSER2 < $showfl;;
                  VERMONT)
                    mailx -s "BFM Output" $VTUSER1 $VTUSER3 < $showfl;;
                  WEST VIRGINIA)
                    mailx -s "BFM Output" $WVUSER1 < $showfl;;
                  WISCONSIN)
                    mailx -s "BFM Output" $WIUSER1 < $showfl;;
                  *)
                    ;;
                esac

When I try to run a state's BFM with this code included in the bash script, the script will error off without any error messages. I bring up Putty, enter the state userid and password, and Putty immediately exits. When I comment out the case statement, the state's BFM runs fine. I have checked other case statements in this script that I know work, and they are written the same way this case statement is written. There are other mailx statements in this script that I know work, and they are written as I have these mailx statements. I have Googled the case statement in bash scripts, and the examples I have found match how I have written this case statement. I have tried putting quotes around the case in variable, and that didn't help. I have tried it without a dollar sign in front of the filename variable, and that didn't help. I have verified that the State variable names are exactly how they read into the bash script. Any help with this issue is greatly appreciated.

Dani AI

Generated

Very likely cause: some case labels contain literal spaces (for example, state names with two words). Quoting the variable being matched (for instance case "$OPT1" in) does not prevent the shell tokenizer from splitting unquoted labels in the source text; an unquoted label with a space turns into separate tokens and produces a syntax error that can make a login script terminate silently (which matches the Putty behavior described).

Fixes and quick checks:

  • Quote or escape any pattern that contains spaces (or use a compact state code instead). Also quote the filename in the redirection to avoid word-splitting: < "$showfl". Example pattern layout:
case "$OPT1" in
  "NORTH CAROLINA")
    mailx -s "BFM Output" "$NCUSER1" < "$showfl"
    ;;
  "SOUTH CAROLINA")
    mailx -s "BFM Output" "$SCUSER1" "$SCUSER2" < "$showfl"
    ;;
  *)
    ;;
esac
  • Run a syntax check and trace to see the exact failure:
bash -n ./script.sh      # syntax-only check
bash -x ./script.sh      # run with trace to show where it dies
file ./script.sh         # detect DOS CR/LF line endings
dos2unix ./script.sh     # convert CRLF -> LF if needed

Other notes: confirm every if has a matching fi and that esac is present for every case. When the script runs as an SSH login script, stderr may not be visible—capture errors to a file (./script.sh 2>/tmp/err) or run the script interactively after login to see the error text. Also verify mailx is found in the PATH for non-interactive shells (use command -v mailx or specify the full path). These steps should expose the syntax error and resolve the immediate termination.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.