Setting variables in a variable array of null variables

Setting variables in a variable array of null variables

Post by Andi » Thu, 10 May 2001 07:43:38



Hi there,

I'm stumped and thinking that my logic is way off.  I'd appreciate another set
of eyes looking over my shoulder to help my catch my logic error.  

Here's my dilemma, I am setting several variables, I have several variable
arrays, one variable array consists of names of previously set variables, my
script attempts in a "while" loop to check each variable in the variable array
and set it if necessary.

I'm using conditional parameter substitution to set my variables inside my
variable arrays.  Is this just not possible and I'm approaching this wrong?

Here's my example script.  And if there's a better way of doing this, I'm always
open to better practices.  

Thanks for your time and effort,
Andi

------------------------------------------------------------------

#!/usr/bin/ksh
#set -x
#-------------------chk_kernel.ksh--------------------------------
#  This script will be part of an app. installation script that
#  checks the kernel parameters, warns the installer if a reboot
#  would be necessary, and eventually copy the /etc/system file
#  and add the changes in.
#-----------------------------------------------------------------

#--VARIABLES

#----------------
# System settings
#----------------
CHG_SYSTEM_SEMMAP="false"
CHG_SYSTEM_SEMMNI="false"
SYSTEM_SEMMAP=$(grep seminfo_semmap /etc/system | awk -F= '{print $2}')
SYSTEM_SEMMNI=$(grep seminfo_semmni /etc/system | awk -F= '{print $2}')

#--VARIABLE ARRAYS

#-------------------------
# Kernel Module Parameters
#-------------------------
set -A CHGMODPARAM_NAMES -- "CHG_SYSTEM_SEMMAP" "CHG_SYSTEM_SEMMNI"
set -A CHGMODPARAM_VARS -- "$CHG_SYSTEM_SEMMAP" "$CHG_SYSTEM_SEMMAP"
set -A MODPARAM_NAMES -- "SYSTEM_SEMMAP" "SYSTEM_SEMMNI"
set -A MODPARAM_VALUES -- "50" "100"
set -A MODPARAM_VARS -- "$SYSTEM_SEMMAP" "$SYSTEM_SEMMNI"
set -A MODPARAMS -- "semsys:seminfo_semmap" "semsys:seminfo_semmni"

#--FUNCTIONS

kernel_module_parameters()
{

(( i = 0 ))
while (( i < n ))
do

#--------------------------------------------------------------------------
#
# Checking for variable and size of kernel parameter, adjusting if needed.
#
#--------------------------------------------------------------------------

CHK_KERN_PARAM=${MODPARAM_VARS[$i]:-"${MODPARAM_VALUES[$i]}"}
case $? in
0)  # The variable is set and not null, need to check variable.
[ "${MODPARAM_VARS[$i]}" -ge "${MODPARAM_VALUES[$i]}" ]
;;
case $? in
0) # The variable is set correctly to MODPARAM_VALUES or greater.
print "Kernel parameter: ${MODPARAMS[$i]} is >= \
${MODPARAM_VALUES[$i]}."
;;
1)  # The variable is less than MODPARAM_VALUES, change variable
${CHGMODPARAM_NAMES[$i]:+"${MODPARAM_VALUES[$i]}"}  #TROUBLE AREA
print ${CHGMODPARAM_NAMES[$i]} # Temp to see if the var. changed
# CHG_${MODPARAM_NAMES[$i]}="${MODPARAM_VALUES[$i]}" #Test idea
print ${CHGMODPARAM_VARS[$i]} # Temp to see what the var really is
print "Kernel parameter: ${MODPARAMS[$i]} is not >= \
${MODPARAM_VALUES[$i]}, a reboot would be necessary."
;;
esac
1)  # The variable is either null or unset, need to set
${CHGMODPARAM_NAMES[$i]:="${MODPARAM_VALUES[$i]}"}  #TROUBLE AREA
print "Kernel parameter: ${MODPARAMS[$i]} is not >= \
${MODPARAM_VALUES[$i]}, a reboot would be necessary."
;;
esac
((i+=1))
done

Quote:}

#--MAIN
kernel_module_parameters

--------------------------------------------------------------------------

 
 
 

Setting variables in a variable array of null variables

Post by Chris F.A. Johnso » Thu, 10 May 2001 14:28:59


[snip]

Quote:> ${CHGMODPARAM_NAMES[$i]:="${MODPARAM_VALUES[$i]}"}  #TROUBLE AREA

CHGMODPARAM_NAMES[$i]=${CHGMODPARAM_NAMES[$i]:="${MODPARAM_VALUES[$i]}"}

--

        =================================================================



 
 
 

Setting variables in a variable array of null variables

Post by Benjamin.Altma » Thu, 10 May 2001 22:34:24


Why not:
    : ${CHGMODPARAM_NAMES[$i]:="${MODPARAM_VALUES[$i]}"}


> [snip]
> > ${CHGMODPARAM_NAMES[$i]:="${MODPARAM_VALUES[$i]}"}  #TROUBLE AREA

> CHGMODPARAM_NAMES[$i]=${CHGMODPARAM_NAMES[$i]:="${MODPARAM_VALUES[$i]}"}

> --

>         =================================================================



 
 
 

Setting variables in a variable array of null variables

Post by Cyrille Lefevr » Sun, 13 May 2001 09:49:20



[unsnip]

SYSTEM_SEMMAP=$(grep seminfo_semmap /etc/system | awk -F= '{print $2}')

no need to grep before awk, do it in one pass :

SYSTEM_SEMMAP=$(awk -F= '/seminfo_semmap/{print $2}' /etc/system)

Quote:> [snip]
> > ${CHGMODPARAM_NAMES[$i]:="${MODPARAM_VALUES[$i]}"}  #TROUBLE AREA

> CHGMODPARAM_NAMES[$i]=${CHGMODPARAM_NAMES[$i]:="${MODPARAM_VALUES[$i]}"}

" aren't needed and := would be :- to avoid changing
CHGMODPARAM_NAMES[$i] twince or do it the Benjamin's way.

Cyrille.
--


Supprimer "%no-spam" et ".invalid" pour me repondre. | about who it chooses
Remove "%no-spam" and ".invalid" to answer me back.  | to be friends with.

 
 
 

1. detecting difference between unset variable and variable whose value is null in Korn Shell

I'm writing a Korn Shell.

 I want to assign default values to environment variables only if they
 are
 not already set.  A null string value is valid.

 ex.

 Before invoking script.

 export str1="hello"
 export str2=
 unset str3

 Now inside my script I want the following logic, (pseudo code)

 if ( $str1 doesn't exist )
   export str1="str1 set to default"
 fi
 if ( $str2 doesn't exist )
   export str3="str2 set to default"
 fi
 if ( $str3 doesn't exist )
   export str3="str3 set to default"
 fi

 print "str1= $str1"
 print "str2= $str2"
 print "str3= $str3"

 I should see the following on the screen:

 str1= hello
str2=
str3= str3 set to default

 My problem is that I don't know how to detect the difference between an
 unset variable and a variable whose value is null.
 ( i.e. str2 and str3 ).

 Does anybody know how?

 Thanks in advance.

 Jeff

2. problem with tin

3. Using SetEnv to Set a Variable with a Variable

4. AST Fourport problems

5. how to set a variable that modifies itself according to another variable?

6. FreeBSD on 50MB?

7. Setting variables with variables (sh/bash) $it=$bit (in functions

8. Linux API to create logical interface on a physical interface?

9. Setting variables with variables (sh/bash) [Summary]

10. Expanding variables within variables

11. Reading HTTP POST Variables in Shell Variables - HOW?

12. Q: Variable name within a variable

13. Variable number of variables in structures