]> git.ipfire.org Git - thirdparty/dracut.git/commitdiff
add function getargnum
authordyoung@redhat.com <dyoung@redhat.com>
Thu, 17 Jan 2013 08:55:50 +0000 (16:55 +0800)
committerHarald Hoyer <harald@redhat.com>
Wed, 23 Jan 2013 14:24:27 +0000 (15:24 +0100)
For cmdline argument with numeric value, add a new function getargnum
It will get proper value with default value as $1, min value as $2,
max value as $3, and param name as $4. valid result will be echo to stdout.
for nul or value not valid it will just echo the default value.
Note: The values should be >=0

[v1->v2]: add arg <minval>
[v2->v3]: do not use bash string match =~

Signed-off-by: Dave Young <dyoung@redhat.com>
modules.d/99base/dracut-lib.sh

index 15f13c3e4ae87763baf33ced068df4887d06186c..da37ac4ad3693a70d0250da3dbbe749d1e79f1e1 100755 (executable)
@@ -181,6 +181,35 @@ getargbool() {
     return 0
 }
 
+isdigit() {
+    case "$1" in
+        *[!0-9]*|"") return 1;;
+    esac
+
+    return 0
+}
+
+# getargnum <defaultval> <minval> <maxval> <arg>
+# Will echo the arg if it's in range [minval - maxval].
+# If it's not set or it's not valid, will set it <defaultval>.
+# Note all values are required to be >= 0 here.
+# <defaultval> should be with [minval -maxval].
+getargnum() {
+    local _b
+    unset _b
+    local _default _min _max
+    _default=$1; shift
+    _min=$1; shift
+    _max=$1; shift
+    _b=$(getarg "$1")
+    [ $? -ne 0 -a -z "$_b" ] && _b=$_default
+    if [ -n "$_b" ]; then
+        isdigit "$_b" && _b=$(($_b)) && \
+        [ $_b -ge $_min ] && [ $_b -le $_max ] && echo $_b && return
+    fi
+    echo $_default
+}
+
 _dogetargs() {
     debug_off
     local _o _found _key