0

First of all, I will leave the code I wrote below, but I would like to tell you a problem I had before.

Problem 1 : I get the error "too many arguments" when I type any character and leave a space and then type a character again. How can I fix this?" For Example; "Write Anything: " ab cd This Place is faulty.

Problem 2 : "How can I print the letters corresponding to the text I have written side by side."

Thanks in advance to anyone who can help me with this.

write_exs_from_user() {
    read -p "Write Anything: " str
    for (( i = 0; i < ${#str}; ++i ));
    do
        if [ ${str:$i:1} = "a" -o ${str:$i:1} = "A" ]
        then
            echo "
    *
   * *
  * * *
 *     *
*       *
"
        elif [ ${str:$i:1} = "b" -o ${str:$i:1} = "B" ]
        then
            echo "
* * * *
*      *
* * * *
*      *
* * * * 
"
        elif [ ${str:$i:1} = "c" -o ${str:$i:1} = "C" ]
        then
            echo "
* * * * *
*      
* 
*      
* * * * * 
"
        elif [ ${str:$i:1} = "d" -o ${str:$i:1} = "D" ]
        then
            echo "
* * * *
*      *
*       *
*      *
* * * * 
"
        elif [ ${str:$i:1} = "e" -o ${str:$i:1} = "E" ]
        then
            echo "
* * * * *
*      
* * * * *
*      
* * * * * 
"
        elif [ ${str:$i:1} = "f" -o ${str:$i:1} = "F" ]
        then
            echo "
* * * * *
*      
* * * * *
*      
* 
"
        elif [ ${str:$i:1} = "g" -o ${str:$i:1} = "G" ]
        then
            echo "  
 * * * *          
*       *
*
*   * * *
*        *
* * * * *
"
        elif [ ${str:$i:1} = "h" -o ${str:$i:1} = "H" ]
        then
            echo "
*     *
*     *
* * * *
*     *
*     *
"
        elif [ ${str:$i:1} = "i" -o ${str:$i:1} = "I" ]
        then
            echo "
 *
* *
 *

" elif [ ${str:$i:1} = "j" -o ${str:$i:1} = "J" ] then echo " * * * *

  • *
    

" elif [ ${str:$i:1} = "k" -o ${str:$i:1} = "K" ] then echo "

  • *
    
  • *
    

" elif [ ${str:$i:1} = "l" -o ${str:$i:1} = "L" ] then echo "


" elif [ ${str:$i:1} = "m" -o ${str:$i:1} = "M" ] then echo " * *




  •         *
    

" elif [ ${str:$i:1} = "n" -o ${str:$i:1} = "N" ] then echo " * *




  •   *      
    

" elif [ ${str:$i:1} = "o" -o ${str:$i:1} = "O" ] then echo "


  •   *
    
  •     *
    
  •     *
    
  •     *
    
  •     *
    
  •   *
    

" elif [ ${str:$i:1} = "p" -o ${str:$i:1} = "P" ] then echo "


  •    *
    

" elif [ ${str:$i:1} = "q" -o ${str:$i:1} = "Q" ] then echo "


  •   *
    
  •     *
    
  •     *
    
  •     *
    

  •   *
    

" elif [ ${str:$i:1} = "r" -o ${str:$i:1} = "R" ] then echo "


  •  *
    
  • *
    

  • *
    
  •  *
    
  •   *
    

"
elif [ ${str:$i:1} = "s" -o ${str:$i:1} = "S" ] then echo "


  •   *
    

   *
  •  *
    

" elif [ ${str:$i:1} = "t" -o ${str:$i:1} = "T" ] then echo "


*
*
*
*

" elif [ ${str:$i:1} = "u" -o ${str:$i:1} = "U" ] then echo "

  •   *
    
  •   *
    
  •   *
    
  •   *
    

" elif [ ${str:$i:1} = "V" -o ${str:$i:1} = "v" ] then echo "

  •   *
    
  • *
    

" elif [ ${str:$i:1} = "w" -o ${str:$i:1} = "W" ] then echo "

  •       *
    
  •     *
    


*   *

" elif [ ${str:$i:1} = "x" -o ${str:$i:1} = "X" ] then echo "

" elif [ ${str:$i:1} = "y" -o ${str:$i:1} = "Y" ] then echo "

  • *
    
  • *
    

  *

" elif [ ${str:$i:1} = "z" -o ${str:$i:1} = "Z" ] then echo "


   *
  *
 *
*

" else echo "Please Write String !" fi done }

write_exs_from_user

1 Answers1

1

Welcome to the Unix & Linux Stack Exchange!

First Problem

For your first problem, the issue is that bash cannot determine if there is a space between [ and =. Bash evaluates any command invocation (note that [ is just another command) as a set of arguments, surrounded by any number of spaces, for example, [ a = A ] is evaluated as a list of the arguments [, a, =, A and ]. After bash evaluates ${str:$i:1} and gets a string, it evaluates the command as if that string was written into the command, for example, it'll read [ $x = "a" ], where x is a (space) as [ = "a" ]. And because there is nothing to distinguish the space $x turned into from any of the other spaces, it gets ignored. Counter-intuitively, you can wrap $x in double quotes ("$x") to tell bash to treat it's contents as an argument, even if that argument is a set of spaces. So, the solution is to wrap ${str:$1:1} in double quotes, meaning that bash will then read it as a list of the arguments [, , =, "a", and ], meaning that the [ command will correctly identify that the left operand is a space.

Interestingly, [ is probably emitting a wrong error here, it should instead should be emitting unary operator expected.

For more information on how double quotes work, checkout this question.

Second Problem

For the second problem, probably the best way to handle an issue like this is to create a variable for each row of expected output before the for loop. Then instead of echoing ASCII art of a character, for each row of the ASCII character you can concatenate to the corresponding row variable, for example, line1="$line1 * * * * *". Then, after the loop, you can use printf on each line, for example, printf '%s' "$line1".

Please note that in the future you should not ask two questions in one post, instead create two different posts for each one.

Danii
  • 91