Search code examples
bashloopsgrepzenity

How to grep a string in until loop in bash?


I work on a script compressing files. I want to do an 'until loop' til' the content of variable matches the pattern. The script is using zenity. This is the major part:

part="0"
pattern="^([0-9]{1}[0-9]*([km])$"
until `grep -E "$pattern" "$part"` ; do
    part=$(zenity --entry \
    --title="Zip the file" \
    --text "Choose the size of divided parts:
(0 = no division, *m = *mb, *k = *kb)" \
    --entry-text "0");

    if grep -E "$pattern" "$part" ; then
        zenity --warning --text "Wrong text entry, try again." --no-cancel;
    fi
done

I want it to accept string containing digits ended with 'k' or 'm' (but not both of them) and don't accept string started with '0'.

Is the pattern ok?


Solution

  • $ grep -w '^[1-9][0-9]*[km]$' <<< 45k
    45k
    $ grep -w '^[1-9][0-9]*[km]$' <<< 001023m
    $ grep -w '^[1-9][0-9]*[km]$' <<< 1023m
    1023m
    

    Don't forget the <<< in your expression, you're not grep'ing a file, but a string. To be more POSIX-compliant, you can also use:

    echo 1023m | grep -w '^[1-9][0-9]*[km]$'
    

    But it is kinda ugly.

    Edit:

    Longer example:

    initmessage="Choose the size of divided parts:\n(0 = no division, *m = *mb, *k = *kb)"
    errmessage="Wrong input. Please re-read carefully the following:\n\n$initmessage"
    
    message="$initmessage"
    
    while true ; do
        part=$(zenity --entry \
             --title="Zip the file" \
             --text "$message")
        if grep -qw '^[1-9][0-9]*[km]$' <<< "$part" ; then
             zenity --info --text 'Thank you !'
             break
        else
            message="$errmessage"
        fi
    done
    

    Also, this is not directly related to the question, but you may want to have a look at Yad, which does basically the same things Zenity does, but has more options. I used it a lot when I had to write Bash scripts, and found it much more useful than Zenity.