3

The below code is not working.

I've given explanation for all the lines:

#!/bin/ksh
cat example.txt | while read LINE                 # reading line from file
do
    var=$LINE                                     # assigning line to variable
    echo $var                                     # printing the line
    H_OR_T="${var:0:6}"                           # taking substring from the line 
    echo $H_OR_T                                  # printing the substring

up to here the code is working fine.

If H_OR_T variable holds the hardcoded value (i.e., M$9001 kindly take a look on if condition below) I should enter the loop. But here I'm getting arithmetical errors.

    if [[ $H_OR_T = "M$9001" ] || [ $H_OR_T = "M$9002" ]]; 
       then
       echo "************** MOVING HEADER OR TRAILER RECORD TO DOMESTIC FILE ************"
       awk '{print $0}' example.txt > domestic.txt
    else
       echo "**************** MOVING RECORD TO LOGGER FILE **********************"
       awk '{print $0}' example.txt > logger.txt
    fi
done

3 Answers 3

5

Not a ksh expert but I think you have two problems in your code.

First problem: You have to single quote M$9001 otherwise the shell will try to expand $9001.

Second problem: You do not nest [ ] inside [ ]. Either use [[ ]] or two [ ].

You should also quote $H_OR_T just in case it expands to something funny.

Here is your code probably fixed:

if [[ "$H_OR_T" = 'M$9001' || "$H_OR_T" = 'M$9002' ]];

Or to be POSIX compliant and more portable:

if [ "$H_OR_T" = 'M$9001' ] || [ "$H_OR_T" = 'M$9002' ];

Note that there are subtle differences between [ ] and [[ ]]. The ksh documenation is a bit sparse on this topic. This is the best I found: http://www.kornshell.com/doc/faq.html question number 10.

Q10. What is the difference between [...] and [[...]]?

A10. The [[...]] is processed as part of the shell grammar whereas [...] is processed like any other command. Operators and operands are detected when the command is read, not after expansions are performed. The shell does not do word splitting or pathname generation inside [[...]]. This allows patterns to be specified for string matching purposes.

2
  • 3
    I think the preferred form is if [...] || [...]. According to Greg's wiki, this form is POSIX-compliant. Commented Aug 20, 2013 at 12:19
  • @JosephR. I totally agree with you. People don't seem to read that wiki much often. [ is a POSIX compliant keyword. ksh/bash suggest you use [[ keyword in stead when you are writing for bash or ksh. if [ "$foo" = "$bar" ] is #sh. Commented Aug 20, 2013 at 12:47
3

Use single quotes for M$9001 if you are comparing with an exact value.

if [[ "$H_OR_T" = 'M$9001' ]]; then
  ...
fi
0

There are more problems with this script. The useless use of cat is one of the examples. If we take the first part of the script we could have written it as

while read -r line; do echo "$line"; done < example.txt

Taking the if.. else part, what is the use of awk {print $0}'

if [[ "$H_OR_T" == 'M$9001' ]]; then
 ....
 ....
fi

In this case 'M$9001' is taken literally.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.