clear
echo "loading"
sleep 0.6
clear
echo "Loading"
sleep 0.6
clear
echo "lOading"
sleep 0.6
clear
echo "loAding"
sleep 0.6
clear
echo "loaDing"
sleep 0.6
clear
echo "loadIng"
sleep 0.6
clear
echo "loadiNg"
sleep 0.6
clear
echo "loadinG"
sleep 0.6
clear
echo "loading."
sleep 0.6
clear
echo "loading.."
sleep 0.6
clear
echo "loading..."
sleep 0.6
clear
echo "loading"
sleep 0.6
clear
答案1
你可以避免对不同的字母大小写组合进行硬编码,并通过使用 shell 的参数扩展功能依次对字符串进行切分,并把一个字符转为大写。
同样,您可以用来printf
插入回车符来代替echo
默认的换行符 - 这样,您就可以避免clear
在每一步都必须调用。
例如:
#!/bin/bash
clear
str='loading'
dly=0.6
printf '%s\r' "$str"
sleep "$dly"
for ((i=0;i<${#str};i++)); do
l=${str:0:i}; r=${str:i}
printf '%s%s\r' "${l}" "${r^}"
sleep "$dly"
done
for ellipsis in . .. ... " "; do
printf '%s%s\r' "${str}" "${ellipsis}"
sleep "$dly"
done