猪拉丁在巴什(Pig latin in Bash)

编程入门 行业动态 更新时间:2024-10-23 23:22:49
猪拉丁在巴什(Pig latin in Bash)

这个猪拉丁语翻译适用于所有以元音开头的单词; 但是,当参数以辅音开头时,此脚本不会打印任何内容

PIGIFY=$1 vowels=(a e i o u A E I O U) CHECK=0 for element in $vowels; do if [[ $element == ${PIGIFY:0:1} ]] then echo ${PIGIFY}ay break fi if [ $CHECK -eq 9 ] then echo ${PIGIFY:1}${PIGIFY:0:1}way fi CHECK=$(( CHECK+1 )) done

我的脚本会尝试将参数的第一个字母与所有可能的元音进行比较,包括大写字母。 一旦我们找到U并且我们发现该单词不以U开头,则计数器应该等于9,然后我希望第二部分被回显。 我想知道我的第二个条件是否是正确的形式,或者我是否搞砸了我的计数器的增量。

This pig latin translator works for all words that start with a vowel; however, this script does not print anything when an argument starts with a consonant

PIGIFY=$1 vowels=(a e i o u A E I O U) CHECK=0 for element in $vowels; do if [[ $element == ${PIGIFY:0:1} ]] then echo ${PIGIFY}ay break fi if [ $CHECK -eq 9 ] then echo ${PIGIFY:1}${PIGIFY:0:1}way fi CHECK=$(( CHECK+1 )) done

My script will try to compare the first letter of the argument to all possible vowels, including capital letters. Once we have reacher U and we find that the word does not start with U, the counter should be equal to 9, and then I want the second part to be echoed. I'm wondering if my second conditional is in right form or if I messed up my increment for my counter.

最满意答案

不要试图在for循环中尝试所有这些逻辑,只需在找到元音时设置变量,打破循环,然后根据新变量执行回声:

PIGIFY=$1 vowels=(a e i o u A E I O U) for element in $vowels; do if [[ $element == ${PIGIFY:0:1} ]] then startswithvowel=true break fi done if [ $startswithvowel ]; then echo ${PIGIFY}ay else echo ${PIGIFY:1}${PIGIFY:0:1}way fi

Instead of trying to do all of this logic inside your for loop, just set a variable if a vowel is found, break the loop, and then do your echo afterwards based on the new variable:

PIGIFY=$1 vowels=(a e i o u A E I O U) for element in $vowels; do if [[ $element == ${PIGIFY:0:1} ]] then startswithvowel=true break fi done if [ $startswithvowel ]; then echo ${PIGIFY}ay else echo ${PIGIFY:1}${PIGIFY:0:1}way fi

更多推荐

本文发布于:2023-08-01 04:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1353847.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Pig   latin   Bash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!