user1658296
user1658296

Reputation: 1418

bash script gives directory listing as well when tokenizing

Here in the code below, line is a line of strings returned as the output of a command. When I run the script, it gives me all the tokens of the string, but appends a listing of the directory to it as well. I really can't figure out what I am doing wrong.

                    for word in $line
                    do 
                            inner_count=$((inner_count + 1)) 
                            echo $word 
                    done

Here is the entire piece of code:

    while read -r line
    do
            if [ "$count" = "2" ]; 
            then
                    inner_count=0
                    #parse each line
                    #if [ "$debug" = "1" ] ; then  printf "%s\n" "$line" > /dev/kmsg ; fi
                    for word in $line
                    do 
                            if [ "$inner_count" = "0" ]; then tmp1="$word" ; fi
                            if [ "$inner_count" = "4" ]; then temp2="$word" ;fi 
                            inner_count=$((inner_count + 1)) 
                    done

            fi
    count=$((count + 1))
    done < <(batctl tg)

Upvotes: 0

Views: 80

Answers (1)

Mat
Mat

Reputation: 206729

The most likely issue that I can think of that could produce this would be that there is a * in $line, and the shell is expanding that (globbing). You can disable globbing with set -f.

Try:

set -f   # disable globbing
for word in $line
do 
  inner_count=$((inner_count + 1)) 
  echo "$word"
done
set +f   # re-enable globbing

Upvotes: 2

Related Questions