3

I read this stackoverflow question...

Bash: check user input is correct

which does most of what I want however rather then checking it's just an integer I need to check it's an integer in a variable range....

The script looks for files in a directory and then assigns a number to them...

  1. File 1
  2. File 2
  3. File 3 etc....

The user chooses the the number and the script then executes commands against that file.....the variable $FILELIST is the total number of files.

Taking the example from the previous stackoverflow I tried.....

FILENUM=""
while [[ ! ($FILENUM =~ ^[0-$FILELIST]+$) ]]; do
    echo " "
    echo "Please enter the file number: "
    read -p "1 - $FILELIST" FILENUM < /dev/tty
done

echo "$FILENUM"

However this is throwing a syntax error: unexpected "(" (expecting "do") in the while line and I'm not sure why, I suspect $FILELIST has to be bracketed somehow but an explanation as to why the above works would help me understand the problem.

Thanks

Community
  • 1
  • 1
user2109420
  • 31
  • 1
  • 3
  • I should add this is in a busybox shell so that may be the problem? – user2109420 Feb 26 '13 at 02:03
  • Hmm, as a first comment, the regex `^[0-$FILELIST]+$` is probably not what you want. Say `$FILELIST` is 100, then the regex is `^[0-100]+$` which will only match numbers with 0s and 1s in them, as opposed to numbers between 0 and 100... – mathematical.coffee Feb 26 '13 at 02:05
  • I tried changing the line to test for a 0-9 variable (replacing $FILELIST with 9) and it throws the same error, so I'm starting to think maybe this is a busybox limitation? – user2109420 Feb 26 '13 at 02:09
  • Thanks M.C but the filelist will never be more then 9 so no problem there, I'm really trying to understand why the error is getting thrown to help my coding for the future rather then just following someone else's example. – user2109420 Feb 26 '13 at 02:12
  • I don't know much about Busybox, but [Wiki](http://en.wikipedia.org/wiki/BusyBox) says it uses the **ash** shell not **bash**, so you may need to re-tag your question and look up some ash syntax (again I can't help you here) – mathematical.coffee Feb 26 '13 at 02:14

1 Answers1

1

bash-specific answers:

You don't need to reinvent the wheel: use the select builtin:

cd /path/to/directory
PS3="Select a file: "
select file in *; do
    if [[ $file ]]; then break; fi
done
echo "You selected '$file'"
echo "You selected file number $REPLY"

To check a number is within a certain range, I'd write:

if (( 0 <= $number && $number <= $max )); then echo "in range"; fi

Since you're using ash you might use this as a reference: http://manpages.debian.net/cgi-bin/man.cgi?query=dash

while true; do
    FILENUM=""
    echo 
    echo "Please enter the file number: "
    read -p "1 - $FILELIST" FILENUM < /dev/tty
    if expr "$FILENUM" : '[0-9]\+$' &&
       [ $FILENUM -gt 0 ] && 
       [ $FILENUM -le $FILELIST ]
    then
        break
    fi
done
echo "$FILENUM"
glenn jackman
  • 238,783
  • 38
  • 220
  • 352