-1

My code looks like this

foo=`cat $words`
for (( i=0; i<${#foo}; i++ )); do
    new=${foo:$i:1}
    while IFS=read -r line 
    do
        if [[ $new=="#" ]]
        then 
            echo "<rect x="20" y="20" fill="black" />"
        elif [[ $new==[A-Z] ]]
        then
            echo "<rect x="20" y="20" fill="white" />"
        fi
    done < "$new"
done

I get an error message containing the following

crossword: line 21: F: No such file or directory
2       crossword: line 21: G: No such file or directory
3       crossword: line 21: #: No such file or directory
4       crossword: line 21: 
5       : No such file or directory
6       crossword: line 21: Z: No such file or directory
7       crossword: line 21: #: No such file or directory
8       crossword: line 21: E: No such file or directory
9       crossword: line 21: 

My desired output should have looked like this

 <rect x="20" y="20" fill="white" />
 <rect x="20" y="20" fill="white" />
 <rect x="20" y="20" fill="black" />

 <rect x="20" y="20" fill="white" />
 <rect x="20" y="20" fill="black" />
 <rect x="20" y="20" fill="white" />

Because the content of the file is

FG#
Z#E

Where # will be replaced by black and the letters with white

GJO2
  • 25
  • 6
  • 1
    It's better to use sed or awk for actual text processing; bash is a decent shell, but doing text manipulation with a shell is not ideal. – Jeff Schaller Dec 16 '22 at 17:20
  • You need spaces around `==` in the conditions. – Barmar Dec 16 '22 at 17:22
  • What is **it** in the statement `I get an error message that it is not a directory`? Which line of code is associated with that error message? [edit] your question to show the exact error message along with whatever command you executed to produce it. – Ed Morton Dec 16 '22 at 17:24
  • What are you intending with `done < "$new"`? `<` needs to be followed by a filename, but `$new` just contains one character from the file. And you do `read -r line`, but never use `$line` for anything. There seems to be no need for the `while` loop at all, the `for` loop should be enough. – Barmar Dec 16 '22 at 17:27
  • If you do need the `while` loop, there should be a space between `IFS=` and `read`. And you need single quotes around the argument to `echo`, otherwise the quotes inside the tag will match the delimiter quotes. – Barmar Dec 16 '22 at 17:29
  • 1
    First thing you need to do is paste this script into shellcheck.net. – Barmar Dec 16 '22 at 17:30
  • You're showing us error messages claiming to come from line 21 of a script that's 14 lines long. Please post a minimal, complete script that demonstrates your problem and the error messages from THAT script, not from some other script. – Ed Morton Dec 16 '22 at 17:37
  • The error messages are from `< "$new"` as I explained above. You're trying to use each character in the file as the name of an input file to read from. Why are you doing that? – Barmar Dec 16 '22 at 17:46
  • Where are the incrementing numbers before each error message coming from? Does the script have `echo $i` that you didn't show us? – Barmar Dec 16 '22 at 17:47

2 Answers2

2

This entire thing can be done with a single sed command:

sed -e 's|#|<rect x="20" y="20" fill="black" />\n|g' -e 's|[A-Z]|<rect x="20" y="20" fill="white" />\n|g' "$words"
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

We'd need more information to be able to debug whatever problem you're having but here's how to really do what you appear to be trying to do using any awk:

$ cat tst.sh
#!/usr/bin/env bash

awk '
    {
        lgth = length($0)
        for ( i=1; i<=lgth; i++ ) {
            char = substr($0,i,1)
            fill = (char == "#" ? "black" : "white")
            printf "<rect x=\"20\" y=\"20\" fill=\"%s\" />\n", fill
        }
        print ""
    }
' "${@:--}"

$ ./tst.sh file
<rect x="20" y="20" fill="white" />
<rect x="20" y="20" fill="white" />
<rect x="20" y="20" fill="black" />

<rect x="20" y="20" fill="white" />
<rect x="20" y="20" fill="black" />
<rect x="20" y="20" fill="white" />

or maybe:

$ cat tst.sh
#!/usr/bin/env bash

awk '
    {
        lgth = length($0)
        for ( i=1; i<=lgth; i++ ) {
            char = substr($0,i,1)
            fill = ""
            if ( char == "#" ) {
                fill = "black"
            }
            else if ( char ~ /[A-Z]/ ) {
                fill = "white"
            }
            if ( fill != "" ) {
                printf "<rect x=\"20\" y=\"20\" fill=\"%s\" />\n", fill
            }
        }
        print ""
    }
' "${@:--}"

,p>

$ ./tst.sh file
<rect x="20" y="20" fill="white" />
<rect x="20" y="20" fill="white" />
<rect x="20" y="20" fill="black" />

<rect x="20" y="20" fill="white" />
<rect x="20" y="20" fill="black" />
<rect x="20" y="20" fill="white" />
Ed Morton
  • 188,023
  • 17
  • 78
  • 185