2014-01-24 41 views
1

如何在bash脚本中使用用户输入不超过300个字符的变量,并显示用户类型剩余的字符数?输入提示可在输入时动态反映字符数

在这种情况下,字符将是与来自get-iplayer的订阅源对应的数字,最多4个字符与下一个空格分隔。

相关的脚本如下 -

{ 
    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 
}; 
+0

“它显示剩余的字符数” - 连续?另外,你能提供一个简短的脚本来提供正确的输入,所以代码可以被测试吗? –

+0

这就是为什么你不把它放在*评论*。你也错过了*点*。我们没有'iplayer',我们不想安装一个。 –

+0

提供了其他信息,但您不需要'get-iplayer',它只是一个脚本问题。 – boudiccas

回答

1

据我了解的问题。这是关于有一个输入提示,将随着数字输入动态更新。

这是一个解决方案,它基于对answer的一个小修改,几乎在两年前发布在这个网站上。

取一个脚本,将stty cbreak模式中的每个字符读入一个名为$result的变量,并相应地更新提示。

#!/bin/bash 

# Input a line with a dynamically updated prompt 
# and print it out 

ask() {      
    n=$1     # the limit on the input length (<1000) 
    if [ -z "$2" ] ; then # the name of variable to hold the input 
     echo "Usage $0: <number> <name>"; 
     return 2; 
    fi 
    result="" # temporary variable to hold the partial input 
    while $(true); do 
     printf '[%03d]> %s' "$n" "$result" 
     stty cbreak 
     REPLY=$(dd if=/dev/tty bs=1 count=1 2> /dev/null) 
     stty -cbreak 


     test "$REPLY" == "$(printf '\n')" && { 
      printf "\n" 
      eval "$2=$result" 
      return 0 
     } 
     test "$REPLY" == "$(printf '\177')" && { 
      # On my terminal 0x7F is the erase character 
      result=${result:0:-1} 
      ((n = $n + 1)) 
      printf "\r\033[K" 
      continue 
     } 


     result="${result}$REPLY" 
     ((n = $n - 1)) 
     if [ $n -eq 0 ] ; then 
      printf "\n" 
      eval "$2=$result" 
      return 1 
     fi 
     printf "\r\033[K" # to clear the line 
    done 
} 

echo "Please input the tv programme numbers to download [max 300 characters]" 
ask 300 'tvbox' 
echo "$tvbox" 
# ... here goes the code to fetch the files ... 

这个脚本是成功的一半烤,因为它不能正确处理光标移动转义字符作为read一样。但它可能会让你走上正轨。

+0

我只是尝试这样做,它的第一个“问”,它是“问300‘tvbox’”的说法无法“问:找不到命令”。我不知道如何进一步发展,对不起。 – boudiccas

+0

http://paste.debian.net/78286/我用一个引擎收录,因为我已经还包括剧本太,它不应该从那里删除。 – boudiccas

+0

我已编辑脚本以使用此新功能。请看看http://paste.debian.net/78290/ –

1

如果您有由空格分隔的词串,你可以遍历它是这样的:

str="hello world nice to meet you" 
for word in $str; do 
    echo "word=$word" 
done 

在另一方面,如果get-iplayer --get需要一个参数是由空格分隔的字组成的字符串,您需要引用该变量:

/usr/bin/get-iplayer --get "$tvbox" 

我从您的评论认为,如果你输入 “123 456 789 234 567 890 345”,你需要调用程序,如:

/usr/bin/get-iplayer --get "123 456 789 234" 
/usr/bin/get-iplayer --get "567 890 345" 

如果这是真的:

printf "%s\n" $tbbox | paste - - - - | while read four; do 
    /usr/bin/get-iplayer --get "$four" 
done 

nums=($tvbox) # this is now an array 
for ((i=0; i < ${#nums[@]}; i+=4)); do 
    /usr/bin/get-iplayer --get "${nums[@]:$i:4}" 
done 
+0

get-iplayer只是请求数字,在由空格隔开的四个最大块中。 – boudiccas

+0

也许我不清楚 - 输入的格式应该是“1234 357 98 5566 102 303 6633”。一个块中不得超过4个数字,与其邻居隔开一个空格,可以有3个数字的程序可以下载,也可以有33个,但我想限制为200或300个字符,并显示剩余的字符数与空格计为一个数字。说得通? – boudiccas