[SHELL: /bin/sh] For loop using variable variable names


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [SHELL: /bin/sh] For loop using variable variable names
# 1  
Old 06-06-2012
[SOLVED] [SHELL: /bin/sh] For loop using variable variable names

Simple enough problem I think, I just can't seem to get it right.

The below doesn't work as intended, it's just a function defined in a much larger script:
Code:
CheckValues() {
    for field in \
        Group_ID \
        Group_Title \
        Rule_ID \
        Rule_Severity \
        Rule_Version \
        Rule_Title \
        Rule_Fix_Text; do
        if [ ! -n \$${field} ]; then
            echo "Error, ${field} undefined."
        else
            echo "\$${field}"
        fi
    done
}

The else statement won't exist once I've successfully tested it. The output I see when I run the actual script is:

Code:
# ./GEN000290-1.sh
$Group_ID
$Group_Title
$Rule_ID
$Rule_Severity
$Rule_Version
$Rule_Title
$Rule_Fix_Text
GEN000290-1: Running CMD: grep -c '^games' /etc/passwd
0
Not a finding.
GEN000290-1: Not a finding.

What I expect to see in the above is the value of those variables (Which is just a string of text assuming non-zero length).

Code:
Group_ID='V-4269-1'
Group_Title='Unnecessary Accounts, Games.'
Rule_ID='SV-4269-lr4_rule'
Rule_Severity='CAT II'
Rule_Version='GEN000290-1'
Rule_Title='The system must not have the unnecessary "games" account.'
Rule_Fix_Text='Remove the "games" account from the /etc/password file before connecting a system to the network.'

Kindly note that I need it to be /bin/sh compliant, as it'll be going across multiple Linux/Unix platforms.

Last edited by Vryali; 06-06-2012 at 11:47 AM.. Reason: Clarity
# 2  
Old 06-06-2012
This works. Note Rule_Severity is null to test:
Code:
$ cat x
#!/bin/sh

Group_ID='V-4269-1'
Group_Title='Unnecessary Accounts, Games.'
Rule_ID='SV-4269-lr4_rule'
Rule_Severity=""
Rule_Version='GEN000290-1'
Rule_Title='The system must not have the unnecessary "games" account.'
Rule_Fix_Text='Remove the "games" account from the /etc/password file before connecting a system to the network.'


CheckValues() {
    for field in \
        Group_ID \
        Group_Title \
        Rule_ID \
        Rule_Severity \
        Rule_Version \
        Rule_Title \
        Rule_Fix_Text; do
        eval var="\$${field}"
        if [ -z "$var" ]; then
            echo "Error, field [${field}] undefined."
        else
            echo "$var"
        fi
    done
}

CheckValues

exit 0
$ x
V-4269-1
Unnecessary Accounts, Games.
SV-4269-lr4_rule
Error, field [Rule_Severity] undefined.
GEN000290-1
The system must not have the unnecessary "games" account.
Remove the "games" account from the /etc/password file before connecting a system to the network.
$

This User Gave Thanks to gary_w For This Post:
# 3  
Old 06-06-2012
Thanks so much, that did the trick.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script use two variable in for loop

I want to create a shell script to add a user and modify its comment field mentioned in a file. 1. File value:- username comment field value xyz123 xyztesting abc123 abctesting def123 deftesting 2. i am using below loop to create user... (2 Replies)
Discussion started by: Anil
2 Replies

2. Programming

FORTRAN: Loop over variable file names

Hi guys I'm a beginner in fortran. So excuse me for my naivety, let me briefly describe what I was trying to do. I have let's say 2 files named reac-1 and reac-2. After opening these files I've to do some calculations, close these files and open the same files again in a loop. So my faulty code... (6 Replies)
Discussion started by: saleheen
6 Replies

3. Shell Programming and Scripting

Does a variable lose its value outside the loop in shell script?

hi, when we assign a variable inside a for loop or while loop in a shell script, does it loses its value after comming out of the loop. i am facing this issue. can anyone help me?? (8 Replies)
Discussion started by: Little
8 Replies

4. Shell Programming and Scripting

How to make dynamic variable names for use in while loop?

i=0 while do sizesfor0=`cat 16 | grep 'pickSize' -A 1 | grep '_sz' | cut -d'_' -f1` sizesfor0=${sizesfor0//id=\"lll/:} IFS=: array0=( $sizesfor0 ) echo ${array0} i=$(( $i + 1 )) done So, right now I have two variables in the while statement above sizesfor0 and array0 The... (1 Reply)
Discussion started by: phpchick
1 Replies

5. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

6. Shell Programming and Scripting

Korn Shell help - Using parameter to create variable names

I'm using korn shell and I am wondering if it's possible to use a parameter passed into a function to build a variable name in a configuration file. I have the function in one source file, I'd like to have a global configuration file instead of hardcoding logins to each script. So I have a... (7 Replies)
Discussion started by: mrevello
7 Replies

7. Shell Programming and Scripting

Shell script variable names created dynamically

Hi, I'm trying to use a config file to define frequencies for checking log files. If the config file contains a frequency it will be used else a default value. The format of the config file (and hence the environment variable) is FREQ_log_logname=value A test shell script as below:... (2 Replies)
Discussion started by: u671296
2 Replies

8. Shell Programming and Scripting

Evaluate string containing shell variable names

Hello, I have this: #!/usr/bin/ksh V1=ABC str="hello 123;${V1}" eval "echo $str" i get hello 123 /script.sh ABC not found However eval works if $str variable doesn't contain a semicolumn (eg if str="hello 123~${v1}" running the eval statement above would produce (2 Replies)
Discussion started by: endorphin
2 Replies

9. Shell Programming and Scripting

File Names in a Variable in a loop

Hi All , I am having confusion in a shell script. Please guide me. I need to get multiple files (number of files vary time to time, file names are separated by '|') using FTP get from the remote server. Actually, i call the FTP function in a loop. At the last step, i need to move all the get... (3 Replies)
Discussion started by: spkandy
3 Replies

10. Shell Programming and Scripting

Passing a variable to awk while in a shell for loop

I am a newbie to awk and c programming, however am not a unix newbie. However, I do need help with a kshell script I am writing. It is almost complete, the last step is killing me. Any help would be greatly appreciated. What I am trying to do is cat a text file that has usernames. Then, using... (2 Replies)
Discussion started by: synergy_texas
2 Replies
Login or Register to Ask a Question