Sponsored Content
Top Forums Shell Programming and Scripting [SHELL: /bin/sh] For loop using variable variable names Post 302651899 by Vryali on Wednesday 6th of June 2012 10:24:56 AM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
GAMES(1)						      General Commands Manual							  GAMES(1)

NAME
4s, 5s, ana, mandel, plumb, smiley, life, fsim, clock, catclock, fireworks, swar, festoon - time wasters SYNOPSIS
games/4s games/5s games/ana [ fixwords ] games/mandel games/plumb [ level ] games/smiley games/life startfile games/fsim games/clock games/catclock [ -c ] games/fireworks games/swar games/festoon [ -p ] [ -e ] [ -t ] [ -sseed ] [ len ] [ pfn ] DESCRIPTION
There are a few games in /bin/games: 4s, 5s Try to fill complete rows using 4-square or 5-square tiles. Move tiles left or right by moving the mouse. Rotate tiles with buttons 1 and 3. Drop tiles for more points with button 2 or the space bar. ana Find anagrams for words typed on standard input. Anagrams can contain several dictionary words. The fixwords argument or num- bers typed on standard input fix the number of words in the output anagrams. mandel Compute and display Mandelbrot sets. Menus on the mouse buttons control various things. plumb Build a plumbing system. Keep ahead of the advancing oil and don't waste pipe. The level argument lets you start at a harder level. smiley A game of historical importance. Type space to shoot, comma to move left, and period to go right. life Play the game of life, given an initial position. There is a library of interesting initial positions; the library is consulted if startfile cannot be found. fsim Pretend you're flying a Cessna. clock, catclock Display analog clocks. Option -c makes catclock crosseyed. fireworks Hoist the fiery blue peter. swar Space war for two players called MCI and SPRINT. One player types a or d to turn left or right, s to shoot, x to enter hyper- space, and w to accelerate. The other player uses ;, l, . and o. AT&T scores whenever either ship shoots itself or otherwise causes mayhem. Hyperspace is occasionally fatal. festoon Traditional radical monoarchate qualitativeness. Now produces pictures (-p), tables (-t), and equations (-e). The seed can be set to recreate a specific document. Len is sentence length, and pfn is percent faked nouns (default 0). FILES
/sys/games/lib/4scores scores of 4s games /sys/games/lib/5scores scores of 5s games /sys/games/lib/plumb/scores scores of plumb games /sys/games/lib/anawords used by ana /sys/games/lib/plumb/* miscellaneous files used by plumb /sys/games/lib/life/* interesting starting positions SOURCE
/sys/src/games GAMES(1)
All times are GMT -4. The time now is 08:06 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy