How to grep a string in until loop in bash? -
i work on script compressing files. want 'until loop' til' content of variable matches pattern. script using zenity. major part:
part="0" pattern="^([0-9]{1}[0-9]*([km])$" until `grep -e "$pattern" "$part"` ; part=$(zenity --entry \ --title="zip file" \ --text "choose size of divided parts: (0 = no division, *m = *mb, *k = *kb)" \ --entry-text "0"); if grep -e "$pattern" "$part" ; zenity --warning --text "wrong text entry, try again." --no-cancel; fi done
i want accept string containing digits ended 'k' or 'm' (but not both of them) , don't accept string started '0'.
is pattern ok?
$ 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 <<<
in expression, you're not grep'ing file, string. more posix-compliant, can use:
echo 1023m | grep -w '^[1-9][0-9]*[km]$'
but kinda ugly.
edit:
longer example:
initmessage="choose size of divided parts:\n(0 = no division, *m = *mb, *k = *kb)" errmessage="wrong input. please re-read following:\n\n$initmessage" message="$initmessage" while true ; part=$(zenity --entry \ --title="zip file" \ --text "$message") if grep -qw '^[1-9][0-9]*[km]$' <<< "$part" ; zenity --info --text 'thank !' break else message="$errmessage" fi done
also, not directly related question, may want have @ yad, same things zenity does, has more options. used lot when had write bash scripts, , found more useful zenity.
Comments
Post a Comment