我如何在 bash 脚本中输入一个包含 300 个字符的变量,并显示剩余的字符数?在这种情况下,字符将是与 get-iplayer 的 feed 相对应的数字,每个块中最多有 4 个字符,每个块之间用空格隔开。相关脚本如下 -
!#/bin/bash
{
read -n1 -p "Do you want to download some tv programmes? [y/n/q] " ynq ;
case "$ynq" in
[Yy]) echo
read -n300 -p "Please input the tv programme numbers to download [max 300 characters] " 'tvbox'
echo
cd /media/$USER/back2/proggies/
/usr/bin/get-iplayer --get $tvbox
;;
[Nn]) echo;; # moves on to next question in the script
[Qq]) echo; exit;; # quits
* ) echo "Thank you ";;
esac
};
我正在寻找“tvbox”中剩余字符的倒计时,从 300 开始。输入的数字范围为 15 到 2000,并以空格分隔,但这些空格也会计入最终总数。可以做到吗?
答案1
每次读取一个程序号并提供可用字符数的倒计时:
while true
do
((chars_left = 300 -${#tvbox}))
read -p "Input a program number of up to $chars_left characters or type 'quit' when done: " new_pgm
[ "$new_pgm" = quit ] && break
if [ $chars_left -gt ${#new_pgm} ]
then
tvbox="$tvbox $new_pgm"
else
echo "Sorry. You are over your character limit."
break
fi
done