Find the answer to your Linux question:
Results 1 to 7 of 7
Iam having one script which is using one variable as CONFS. CONFS="LATEST_v1.1 LATEST_v1.2 LATEST_V1.3 LATEST_V1.2_V2 LATEST_V1.2_Unmasked APPROVED_V1.1 APPROVED_V1.2 APPROVED_V1.3" Now I need to modify script in such a way that ...
  1. #1
    Just Joined!
    Join Date
    Dec 2011
    Posts
    11

    How to change the value of variable after comparing it with other ?

    Iam having one script which is using one variable as CONFS.

    CONFS="LATEST_v1.1 LATEST_v1.2 LATEST_V1.3 LATEST_V1.2_V2 LATEST_V1.2_Unmasked APPROVED_V1.1 APPROVED_V1.2 APPROVED_V1.3"

    Now I need to modify script in such a way that i will remove few conf files while modifying another variable.for example using another variable exclude_conf.

    EXCLUDE_CONF="LATEST_V1.2_Unmasked APPROVED_V1.3 APPROVED_V1.2"

    Now when i ran the one script which is using CONFS variable I want to update it automatically in such a way that the final value of CONFS=CONFS - EXCLUDE_CONF.I guess iam clear in explaining.

    Please let me know if someone have any idea on this.

    Thanks
    Vikram

  2. #2
    Linux Guru
    Join Date
    May 2011
    Posts
    1,843
    If I understand you right, try this:

    Code:
    #!/bin/sh
    CONF="a.conf b.conf c.conf d.conf"
    EXCL_CONF="b.conf c.conf"
    echo CONF: $CONF
    echo EXCL_CONF: $EXCL_CONF
    
    for conf in $CONF; do
      unset exclude
    
      for excl in $EXCL_CONF; do
        if [ "$excl" == "$conf" ]; then
          exclude=1
          break
        fi
      done
      [ -n "$exclude" ] && continue
      [ -n "$NEW_CONF" ] && NEW_CONF="$NEW_CONF $conf" || NEW_CONF=$conf
    done
    CONF=$NEW_CONF
    
    echo "CONF: $CONF"

  3. #3
    Just Joined!
    Join Date
    Dec 2011
    Posts
    11
    Hi Atreyu..Thanks for your response.One thing i would liike to add in this that we need to update excl_conf manually..........as in my script we are taking CONF Value as...........CONF=$(find /apps/psr/database/conf \( -name APPROVED_V1.2*.conf -o -name LATEST_V1.2*.conf \) -exec basename {} \
    So my question is that will this program work that time too ?

  4. #4
    Linux Guru
    Join Date
    May 2011
    Posts
    1,843
    not sure exactly what you mean. Do you mean CONF value is generated dynamically (via your find command) and it then needs whatever you have defined in EXCL_CONF removed from it? Or do you truly mean that EXCL_CONF is defined dynamically? If so, how?

    If you mean the former, just replace my generic CONF= line (line 2) with your find command.
    Last edited by atreyu; 01-07-2012 at 06:22 PM. Reason: typo

  5. #5
    Just Joined!
    Join Date
    Dec 2011
    Posts
    11
    Yeah Atreyu...CONF value will be generated dynamically with find command...and the second variable EXCL_CONF will be defined manually.and this CONF needs whatever we defined in EXCL_CONF removed from it

    Could you please explain me the usage of last 2 lines if it is not trouble some for you.

    [ -n "$exclude" ] && continue
    [ -n "$NEW_CONF" ] && NEW_CONF="$NEW_CONF $conf" || NEW_CONF=$conf
    Last edited by vikram151; 01-08-2012 at 05:03 AM.

  6. #6
    Linux Guru
    Join Date
    May 2011
    Posts
    1,843
    Quote Originally Posted by vikram151 View Post
    Yeah Atreyu...CONF value will be generated dynamically with find command...and the second variable EXCL_CONF will be defined manually.and this CONF needs whatever we defined in EXCL_CONF removed from it
    okay, then substitute your find command for my CONF= line, like I said before, and you should be good.

    Quote Originally Posted by vikram151 View Post
    Could you please explain me the usage of last 2 lines if it is not trouble some for you.

    [ -n "$exclude" ] && continue
    [ -n "$NEW_CONF" ] && NEW_CONF="$NEW_CONF $conf" || NEW_CONF=$conf
    Code:
    [ -n "$exclude" ] && continue
    Literally, this means, if the variable exclude is not empty, then end processing and jump to the next variable in the loop. Specifically, if the CONF variable matched one of the variables listed in the EXCL_CONF list (i.e., if "exclude=1" was set), then stop processing in the loop and continue on to the next variable (I think next would have been a better function name here, like in Perl, but whatever).
    Code:
    [ -n "$NEW_CONF" ] && NEW_CONF="$NEW_CONF $conf" || NEW_CONF=$conf
    In this line, note the && and the ||. These signify and and or. So it is saying, if the variable NEW_CONF has been defined yet (-n means "not empty"), then append the current conf variable ($conf), otherwise, simply make NEW_CONF equal the current conf, since none are set yet. The whole point of this is to avoid having an extra space in the NEW_CONF variable (when NEW_CONF is empty). It does not matter (you could just use NEW_CONF="$NEW_CONF $conf"), but it is just a neatness thing.

  7. #7
    Just Joined!
    Join Date
    Dec 2011
    Posts
    11
    Thanks Atreyu..I got it...Iam really learning lot from you..Thanks Again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...