-2

I'm trying to make my bash script process the first character in a variable and work until it reaches the last. Here's what I have so far:

#!/bin/bash

echo Word?
read -r -p '' foo
# $foo is set to 'Antarctica' by user.

wordlength=${#foo}
$wordlength says 10, so start on character 1.
'A' is first letter received in $foo, so echo '{a,A}'
'n' is second letter received in $foo, so echo '{n,N}'
't' is third letter received in $foo, so echo '{t,T}'
'a' is fourth letter received in $foo, so echo '{a,A}'
........
'i' is eighth letter received in $foo, so echo '{i,I}'
'c' is ninth letter received in $foo, so echo '{c,C}'
'a' is tenth letter received in $foo, so echo '{a,A}'

And here's what it would look like on the user's end:

Word?

Antarctica

{a,A}{n,N}{t,T}{a,A}{r,R}{c,C}{t,T}{i,I}{c,C}{a,A}

Which is what it would output exactly. Anyone know how to do this?


Edit: I guess they could be linked like this? $wordlength is 10, so begin with 1 and go to 10.

if 1st letter of $foo is A, echo '{a,A}'
if 2st letter of $foo is n, echo '{n,N}'
.....

closed as unclear what you're asking by dr01, Anthon, Anthony Geoghegan, Jeff Schaller, Romeo Ninov Jul 26 '17 at 11:11

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Can you please clarify? – dr01 Jul 26 '17 at 7:27
  • The user would enter a word, and the script would echo corresponding text for each letter in order. – Terkey-Juice Jul 26 '17 at 7:49
  • 2
    Can you explain how the "A" in Antartica is mapped to 0.0 ? Similarly for other characters... – user218374 Jul 26 '17 at 7:58
  • {0.0} is just a sample text string. It doesn't mean anything. I am wondering how to map strings of text to characters. – Terkey-Juice Jul 26 '17 at 14:47
  • There, edited it to make it much simpler as to what I want to do. Don't know why I didn't specify before. – Terkey-Juice Jul 26 '17 at 15:01
4

To iterate on each character of a string in ksh93 or bash or zsh:

string=whatever
for ((i = 0; i < ${#string}; i++)); do
   printf '%s\n' "Character $((i + 1)): ${string: i:1}"
done

In zsh (same in yash except that yash doesn't support the for ((...)) syntax), see also:

for ((i = 1; i <= ${#string}; i++)); do
   printf '%s\n' "Character $i: $string[i]"
done

Or using the s (for split) parameter expansion flag with the empty string as the separator:

for c (${(s"")string}) something with "$c"

For mapping character to string, you'd use either a case construct like:

case $c in
  (A) s='{O.O}';;
  (a) s='{q-p}';;
  ...
esac

Or an associative array:

  • zsh:

    typeset -A map
    map=(
      A  '{O.O}'
      a  '{q-p}'
      ...
    )
    s=$map[$c]
    
  • ksh93/bash:

    typeset -A map
    map=(
      [A]='{O.O}'
      [a]='{q-p}'
      ...
    )
    s=${map[$c]}
    

Portably (with standard sh syntax), you could also do:

map='|A={O.O}|a={q-p}|...'
s=${map#*"|$c="}
s=${s%%"|"*}

(assuming none of the strings contain | characters).

Or you could also invoke a proper text-processing utility (use the shell to invoke commands, as that's what it's been designed for and a text processing utility to process text).

STRING=whatever awk -F= '{map[$1] = $2}
   END {
     s = ENVIRON["STRING"]
     l = length(s)
     for (i = 1; i <= l; i++) {
       c = substr(s, i, 1)
       print map[c]
     }
   }' << EOF
A={O.O}
a={q-p}
...
EOF

(though beware that some implementations of awk like mawk only support single-byte characters).

  • This is not producing the "desired" output as the OP mentions... – user218374 Jul 26 '17 at 8:00
  • @Rakesh, true, that part being unclear to me, I had initially left that out. See edit though. – Stéphane Chazelas Jul 26 '17 at 9:20
  • I'm sorry guys if this is confusing. Basically, I want it to echo things from a list of strings based on what letters are typed into $foo. – Terkey-Juice Jul 26 '17 at 14:41
  • Yes, that's what I meant, mapping text to letters. – Terkey-Juice Jul 26 '17 at 14:42
  • @Terkey-Juice, so does this answer answer your question? – Stéphane Chazelas Jul 26 '17 at 14:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.