I am trying make a script that combines arrays on demand. Here is the script:
#! /bin/bash
declare -A code
code=( [H]="h" [E]="e" [L]="l" [P]="p" [M]="m" [E]="e" )
I need to print "help me" - without quotes and in one line - when I enter ./filename.bash "HELP ME" at command prompt. here is what I am using.
code=1ドル;
for (( i = 0; i < ${#code[@]} ; i = $i + 1 ));
do;
echo ${code[@]:$i:1};
done
jimmij
48.6k20 gold badges135 silver badges140 bronze badges
1 Answer 1
Try this with script.sh "HELP ME"
:
#!/bin/bash
input=1ドル;
declare -A code
code=( [H]="h" [E]="e" [L]="l" [P]="p" [M]="m" [E]="e" )
for ((i=0; i<${#input}; i++))
do
if [[ "${input:$i:1}" = " " ]]; then # whitespace?
echo -n " "
else
echo -n "${code[${input:$i:1}]}"
fi
done
Output:
help me
answered May 11, 2015 at 4:52
-
Thank you! You are a life saver. The only other problem that I am now having is that when I add "please." after echo -n "${code[${input:$i:1}]}" it prints out hpleaseepleaselpleasepplease and so on. Also echo -n prints behind the command prompt rather than after the command prompt.Jack Spencer– Jack Spencer2015年05月11日 06:19:47 +00:00Commented May 11, 2015 at 6:19
-
If you want
help me please.
add as last additional line:echo " please."
Cyrus– Cyrus2015年05月11日 17:22:26 +00:00Commented May 11, 2015 at 17:22
You must log in to answer this question.
Explore related questions
See similar questions with these tags.
lang-bash
1ドル
? you should be iterating over1ドル
, using each of its characters to index into yourcode
array, surely?tr "a-zA-Z" "A-Za-z" <<<"HELP ME"
– and, regarding associative arrays, you can use a space for the index[' ']=' '
(if it helps).tr "EHLMP" "ehlmp" <<< "HELP ME"