我想创建一个代码来获得以下结果:
echo -e "\t\tPlease enter your name?"
read name
name="${name,,}"
for i in $(echo "Hello " $name) ; do
echo ${i^}
done | sed 's/^/\t\t/;s/$/, here is your order:/'
如果我输入如下名称:mike denver
输出是:
<here come out the right space of two tabs>Hello, here is your order:
<here come out the right space of two tabs>Mike, here is your order:
<here come out the right space of two tabs>Denver, here is your order:
基本上,它根据我放置的选项卡数量 ( ) 从左侧创建右侧空间\t
,但它会Hello
为输入的每个名称重复打印加号行。我不明白如何将 sed 命令放在外面。我的预期结果应该是这样的:
<here come out the right space of two tabs>Hello, Mike Denver here is your order:
答案1
与使用循环相比for
,这样做要容易得多:
echo -e "\t\tPlease enter your name?"
read -a name
name="${name}"
echo -e "\t\tHello ${name[@]^}, here is your order:"
输出:
Please enter your name?
mike denver
Hello Mike Denver, here is your order:
参数两边的双引号echo
将扩展变量并以所需的格式一次性打印所有内容。
答案2
您可以使用数组作为名称和printf
:
read -p $'\t\tPlease enter your name?\n' -a name
name=( "${name[@],,}" )
printf '\t\tHello %s, here is your order:\n' "${name[*]^}"