Skip to contentSkip to conversation

add function getargnum

PATCH Superseded Dave Young 3 participants 6 messages Nov 26, 2012 – Jan 14, 2013 3 messages not in the archive

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Dave Young

author
#0 lore raw

PATCH 1/2 add function getargnum

For cmdline argument with numeric value, add a new function getargnum It will get proper value with default value as $1, max value as $2. valid result will be echo to stdout, for nul or value not valid it will just echo the default value.

---
 modules.d/99base/dracut-lib.sh |   19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
modules.d/99base/dracut-lib.sh +19 -0
@@ -167,6 +167,25 @@ getargbool() {
     return 0
 }
 
+# getargnum <defaultval> <maxval> <args...>
+# Will echo the arg if it's in range [0 - maxval].
+# If it's not set or it's not valid, will set it <defaultval>.
+getargnum() {
+    local _b
+    unset _b
+    local _default
+    local _max
+    _default=$1; shift
+    _max=$1; shift
+    _b=$(getarg "$@")
+    [ $? -ne 0 -a -z "$_b" ] && _b=$_default
+    if [ -n "$_b" ]; then
+        [[ "$_b" =~ ^[0-9]+$ ]] && _b=$(($_b)) && \
+        [[ $_b -ge 0 && $_b -le $_max ]] && echo $_b && return
+    fi
+    echo $_default
+}
+
 _dogetargs() {
     debug_off
     local _o _found _key

A message that is not in the archive (sent off-list or not yet received): 20121126023847.GA6884-bi+AKbBUZKY6gyzm1THtWbp2dZbC/[email protected]

#2 lore raw
Quoted 15 lines · Dave Young

For cmdline argument with numeric value, add a new function getargnum It will get proper value with default value as $1, max value as $2. valid result will be echo to stdout, for nul or value not valid it will just echo the default value.

---
 modules.d/99base/dracut-lib.sh |   19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

modules.d/99base/dracut-lib.sh getargbool() { · line 170

Quoted 4 lines
modules.d/99base/dracut-lib.sh +1 -0
@@ -167,6 +167,25 @@ getargbool() {
     return 0
 }
 
+# getargnum <defaultval> <maxval> <args...>
				      ^^^^^^

Is above one arg or multiple args. I thought in one call we will resolve the value of one parameter?

modules.d/99base/dracut-lib.sh getargbool() { · line 171

Quoted 1 line
modules.d/99base/dracut-lib.sh +1 -0
+# Will echo the arg if it's in range [0 - maxval].

You might want to consider passing in minval instead of assuming it is always 0. It just makes function more generic.

Thanks
Vivek

Quoted 20 lines
modules.d/99base/dracut-lib.sh +17 -0
+# If it's not set or it's not valid, will set it <defaultval>.
+getargnum() {
+    local _b
+    unset _b
+    local _default
+    local _max
+    _default=$1; shift
+    _max=$1; shift
+    _b=$(getarg "$@")
+    [ $? -ne 0 -a -z "$_b" ] && _b=$_default
+    if [ -n "$_b" ]; then
+        [[ "$_b" =~ ^[0-9]+$ ]] && _b=$(($_b)) && \
+        [[ $_b -ge 0 && $_b -le $_max ]] && echo $_b && return
+    fi
+    echo $_default
+}
+
 _dogetargs() {
     debug_off
     local _o _found _key

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#4 lore raw
Quoted 19 lines · Vivek Goyal
Quoted 18 lines · Dave Young

For cmdline argument with numeric value, add a new function getargnum It will get proper value with default value as $1, max value as $2. valid result will be echo to stdout, for nul or value not valid it will just echo the default value.

---
 modules.d/99base/dracut-lib.sh |   19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
modules.d/99base/dracut-lib.sh +0 -0
@@ -167,6 +167,25 @@ getargbool() {
     return 0
 }
 
Quoted 4 lines

modules.d/99base/dracut-lib.sh getargbool() { · line 170

Quoted 1 line
modules.d/99base/dracut-lib.sh +1 -0
+# getargnum <defaultval> <maxval> <args...>
				      ^^^^^^

Is above one arg or multiple args. I thought in one call we will resolve the value of one parameter?

It is one arg. will s/args.../arg

Quoted 5 lines

modules.d/99base/dracut-lib.sh getargbool() { · line 171

Quoted 2 lines
modules.d/99base/dracut-lib.sh +1 -0
+# Will echo the arg if it's in range [0 - maxval].

You might want to consider passing in minval instead of assuming it is always 0. It just makes function more generic.

Make sense. I would like to assume the arg as a number >=0 even with the minval added which means minval should be >=0.

Quoted 24 lines

Thanks
Vivek

Quoted 20 lines
modules.d/99base/dracut-lib.sh +17 -0
+# If it's not set or it's not valid, will set it <defaultval>.
+getargnum() {
+    local _b
+    unset _b
+    local _default
+    local _max
+    _default=$1; shift
+    _max=$1; shift
+    _b=$(getarg "$@")
+    [ $? -ne 0 -a -z "$_b" ] && _b=$_default
+    if [ -n "$_b" ]; then
+        [[ "$_b" =~ ^[0-9]+$ ]] && _b=$(($_b)) && \
+        [[ $_b -ge 0 && $_b -le $_max ]] && echo $_b && return
+    fi
+    echo $_default
+}
+
 _dogetargs() {
     debug_off
     local _o _found _key
Signature · 2 lines
Thanks
Dave
author
#5 lore raw

PATCH v2 1/2 add function getargnum

In reply to a message not in the archive

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>

---
 modules.d/99base/dracut-lib.sh |   21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
modules.d/99base/dracut-lib.sh +21 -0
@@ -167,6 +167,27 @@ getargbool() {
     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
+        [[ "$_b" =~ ^[0-9]+$ ]] && _b=$(($_b)) && \
+        [[ $_b -ge $_min && $_b -le $_max ]] && echo $_b && return
+    fi
+    echo $_default
+}
+
 _dogetargs() {
     debug_off
     local _o _found _key
#6 lore raw
In reply to a message not in the archive

Am 26.11.2012 03:38, schrieb Dave Young:

Quoted 22 lines

For cmdline argument with numeric value, add a new function getargnum It will get proper value with default value as $1, max value as $2. valid result will be echo to stdout, for nul or value not valid it will just echo the default value.

---
 modules.d/99base/dracut-lib.sh |   19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
modules.d/99base/dracut-lib.sh +4 -0
@@ -167,6 +167,25 @@ getargbool() {
     return 0
 }
 
+# getargnum <defaultval> <maxval> <args...>
+# Will echo the arg if it's in range [0 - maxval].
+# If it's not set or it's not valid, will set it <defaultval>.
+getargnum() {

modules.d/99base/dracut-lib.sh getargbool() { · line 183

Quoted 10 lines
modules.d/99base/dracut-lib.sh +10 -0
+    local _b
+    unset _b
+    local _default
+    local _max
+    _default=$1; shift
+    _max=$1; shift
+    _b=$(getarg "$@")
+    [ $? -ne 0 -a -z "$_b" ] && _b=$_default
+    if [ -n "$_b" ]; then
+        [[ "$_b" =~ ^[0-9]+$ ]] && _b=$(($_b)) && \

we can't use "=~" because it's not posix shell. We restricted ourselves to dash syntax in the initramfs. sorry.

Quoted 9 lines
modules.d/99base/dracut-lib.sh +5 -0
+        [[ $_b -ge 0 && $_b -le $_max ]] && echo $_b && return
+    fi
+    echo $_default
+}
+
 _dogetargs() {
     debug_off
     local _o _found _key

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#8 lore raw
Quoted 28 lines · Harald Hoyer

Am 26.11.2012 03:38, schrieb Dave Young:

Quoted 27 lines

For cmdline argument with numeric value, add a new function getargnum It will get proper value with default value as $1, max value as $2. valid result will be echo to stdout, for nul or value not valid it will just echo the default value.

---
 modules.d/99base/dracut-lib.sh |   19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
modules.d/99base/dracut-lib.sh +9 -0
@@ -167,6 +167,25 @@ getargbool() {
     return 0
 }
 
+# getargnum <defaultval> <maxval> <args...>
+# Will echo the arg if it's in range [0 - maxval].
+# If it's not set or it's not valid, will set it <defaultval>.
+getargnum() {
+    local _b
+    unset _b
+    local _default
+    local _max
+    _default=$1; shift
Quoted 8 lines

modules.d/99base/dracut-lib.sh getargbool() { · line 183

Quoted 5 lines
modules.d/99base/dracut-lib.sh +5 -0
+    _max=$1; shift
+    _b=$(getarg "$@")
+    [ $? -ne 0 -a -z "$_b" ] && _b=$_default
+    if [ -n "$_b" ]; then
+        [[ "$_b" =~ ^[0-9]+$ ]] && _b=$(($_b)) && \

we can't use "=~" because it's not posix shell. We restricted ourselves to dash syntax in the initramfs. sorry.

Will fix, thanks for review.

Quoted 12 lines
Quoted 11 lines
modules.d/99base/dracut-lib.sh +5 -0
+        [[ $_b -ge 0 && $_b -le $_max ]] && echo $_b && return
+    fi
+    echo $_default
+}
+
 _dogetargs() {
     debug_off
     local _o _found _key
Signature · 2 lines
Thanks
Dave