这个猪拉丁语翻译适用于所有以元音开头的单词; 但是,当参数以辅音开头时,此脚本不会打印任何内容
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 )) doneMy 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 fiInstead 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更多推荐
发布评论