Sponsored Content
Top Forums Shell Programming and Scripting dynamic variables - eval - expand etc. Post 302508071 by Chubler_XL on Friday 25th of March 2011 03:11:57 PM
Old 03-25-2011
Just a logic problem - you were assigning VAR2 on 3rd loop:

Code:
example=1
for counter in 0 1 2; do
      if [ ! "$(eval echo "\$testing$example")" ]; then
            echo "FIRST LOOP, CREATING DYN VAR1"
            eval testing$example="test"
      else
         if [ ! "$(eval echo "\$testing2$example")" ]; then
              echo "SECOND LOOP, CREATING DYN VAR2"
              eval testing2$example="test"
         fi
            echo "COMPARING DYN VAR1 to DYN VAR2"
            if [ "$(eval echo \$testing$example)" == "$(eval echo \$testing2$example)" ]; then
                echo "MATCH"
                echo "CHANGING SECOND DYN VAR TO GET NO MATCH NEXT LOOP"
                eval testing2$example="test2"
            else
                echo "NO MATCH"
            fi
      fi
done

If your doing a lot of this sort of stuff why not write notdefined and dynval functions:

Code:
notdefined() {
[ ${!1-X} != ${!1-Y} ]
}
dynval() {
  printf ${!1}
}
example=1
for counter in 0 1 2; do
      if notdefined testing$example ; then
            echo "FIRST LOOP, CREATING DYN VAR1"
            eval testing$example="test"
      else
            if notdefined testing2$example ; then
              echo "SECOND LOOP, CREATING DYN VAR2"
              eval testing2$example="test"
            fi
            echo "COMPARING DYN VAR1 to DYN VAR2"
            if [ "$(dynval testing$example)" == "$(dynval testing2$example)" ]; then
                echo "MATCH"
                echo "CHANGING SECOND DYN VAR TO GET NO MATCH NEXT LOOP"
                eval testing2$example="test2"
            else
                echo "NO MATCH"
            fi
      fi
done


Last edited by Chubler_XL; 03-25-2011 at 04:28 PM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Dynamic variables within shell script

Hi Gurus, I have a requirement of writting the shell script where it should ask me two values FND_TOP=/d02/app/oracle/xxx/fnd/11.5.0 CDCRM_TOP=/d02/app/oracle/xxx/cdcrm/11.5.0 and then keep these values stored as variables for the execution of rest of the script. Because, I have to... (2 Replies)
Discussion started by: isingh786
2 Replies

2. UNIX for Dummies Questions & Answers

dynamic variables

I am new to unix and the following problem is bugging me.:confused: var1="hello1" var2="hello2" var3="hello3" counter=1 while do echo $var$counter done the idea here is to display the value of "var" based on the counter. I am using the korn shell. I used array here but the... (4 Replies)
Discussion started by: max_payne1234
4 Replies

3. Programming

global variables and dynamic allocation

Hi, is it possible in C to allocate dynamically a global variable?? (3 Replies)
Discussion started by: littleboyblu
3 Replies

4. UNIX for Dummies Questions & Answers

how to expand environment variables in a file?

I am new to unix and would appreciate if someone could help. I have an environment variable SourceFilePath=/db1/Src/test set on the unix server. I want to expand this SHELL variable in a file using any command sed, awk etc File contents is as follows: var=$SourceFilePath/file.txt ... (2 Replies)
Discussion started by: debbie15
2 Replies

5. Shell Programming and Scripting

Problem with dynamic variables outside the loop

Hi All, Shell is ksh I've given portion of the script here to explain the problem. It will accept 2 input parameters . in_file1=$1 in_file2=$2 outbound_dir=/home/outbound for i in 1 2 do eval file$i=$outbound_dir/\$in_file$i eval echo "filename is \$file$i" ... (4 Replies)
Discussion started by: kk17
4 Replies

6. Shell Programming and Scripting

Dynamic variables and eval

Hi, I need some direction with the following. The below code is semi-psuedo code which will hopefully make it easier to understand what I am trying to achieve: for i in `echo ${testarray }` do let c=c+1 eval "first$c=$i" while... (4 Replies)
Discussion started by: yonderboy
4 Replies

7. Shell Programming and Scripting

For loop using input file doesn't expand variables

Hi, I'm using a for loop reading from an input file that contains files, whose path includes a variable name. But the for loop doesn't expand the variable and therefore can't find the file. Here's an example: File BACKUPFILES /home/John/alpha /home/Sue/beta... (8 Replies)
Discussion started by: Hesiod
8 Replies

8. Shell Programming and Scripting

Generating dynamic variables

Hi , i am unable to generate dynamic variables can any one please help me on the below issue j=1 {record_count_"$j"}=`db2 -xselect substr\(job_name,24\) rec_count from $libname.audit_table_nrt where job_name like \'DATAMART_DEL_RUN%\' and STS_FLAG=\'E\' and seq_no=$i` echo " record... (3 Replies)
Discussion started by: bhaskar v
3 Replies

9. UNIX for Beginners Questions & Answers

Expand Variables and Wildcards into another variable.

Dear Forum members, I am having trouble getting the complete filename (and directory path) in a variable. Output directory mentioned in the code have three files: DISPLAY_CITY_DETAILS_15-05-2019-08-29-26_MIGRATE_london.out DISPLAY_CITY_DETAILS_15-05-2019-08-29-26_MIGRATE_paris.out... (4 Replies)
Discussion started by: chetanojha
4 Replies

10. UNIX for Beginners Questions & Answers

[BASH] eval command not expanding variables as expected.

Hi Guys, I wrote a collection of bash functions years ago and now need to use them again but I'm getting some error messages when eval tries to expand the variables names. I recollect that I used the shopt command to set one of the options but I can't quite remember the command that I... (8 Replies)
Discussion started by: ASGR
8 Replies
DH-EXEC-SUBST(1)						      dh-exec							  DH-EXEC-SUBST(1)

NAME
dh-exec-subst - Debhelper executable file substition scripts SYNOPSIS
#! /usr/bin/dh-exec src/libfoo-*.so.* debian/foo-plugins/usr/lib/foo/${DEB_HOST_MULTIARCH}/ DESCRIPTION
Being a sub-command of dh-exec(1), this program must not be ran directly, but through dh-exec, which automatically runs all available sub-commands if run bare; or explicitly with dh-exec --with=subst. It is a wrapper around the various other substitution helpers, and will pipe the input file through all the available substitution helpers. It is up to these scripts to do the actual work. When a helper fails to expand a variable within its input, it will leave it as-is, so that later in the pipeline it can perhaps be expanded by another program. SCRIPTS
dh-exec-subst-env Substitutes any of the available, exported environment variables into its input. It does not do any kind of filtering: whatever is available in the environment, will be available for substitution, however unsafe that may be. dh-exec-subst-multiarch Attempts to expand any of the variables known to dpkg-architecture(1), mostly useful for multi-arch support. The command will query dpkg-architecture(1) directly, and does not rely on environment variables set (as the called program will prefer already set environment variables anyway). ENVIRONMENT
DH_EXEC_SCRIPTDIR Indicates which directory the command-specific scripts should be sought for. If not specified, scripts will be searched for in /usr/share/dh-exec/. FILES
$DH_EXEC_SCRIPTDIR/dh-exec-subst-* The various scripts for the higher-level program. SEE ALSO
debhelper(1), dh-exec(1) AUTHOR
dh-exec-subst is copyright (C) 2011-2012 by Gergely Nagy <algernon@madhouse-project.org>. 2012-05-03 DH-EXEC-SUBST(1)
All times are GMT -4. The time now is 07:21 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy