#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    14
    Rep Power
    0

    Executing String value as a command


    Hello Linux Gurus,
    I have a typical requirement:
    1. I have a file where I kept all the commands in each line like key=value
    eg.
    Properties file entry Code:
     
    check_java_command=`ls -ld /app/java/jdk160_20-linux64bit | awk '{print $8}'`
    check_java_value="/app/java/jdk160_20-linux64bit"
     
    check_jrockit_command="ls -ld /app/java/jrockit28jdk160_20-64b | awk '{print $8}'"
    check_jrockit_value="/app/java/jrockit28jdk160_20-64b"


    2. Then I loop through this file like below:

    Code:
    grep '_command' command.properties | awk -F= '{print $1}' | while read command; do
    echo "Command:"$command
                    if [[ "$command" != \#* ]] ; then
                            KEY=${command%_command}
                            echo "Key is:"$KEY
                            COMMAND=`grep '^'"$KEY"'_command' command.properties | awk -F= '{print $2}'`
                            VALUE=`grep '^'"$KEY"'_value' command.properties | awk -F= '{print $2}'`
                            echo "Command="$COMMAND
                            echo "Value="$VALUE
                            COMMAND=eval ${COMMAND}
                            echo "Executed Command  :"$COMMAND
                            if [[ "$COMMAND" == "$VALUE" ]]; then
                                    echo $KEY executed successfully...
                                    
                            else
                                    echo $KEY execution failed...
                                    
                            fi
                    else
                            echo "This is a comment!"
                    fi
                 done
    3. Now the command is getting read from the file nicely but I can't execute that command. I tried using eval/$/another ``/" " sign and what not.

    I don't have fundamental knowledge so please guide me what's wrong here and how can I fix it?

    Thanks,
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    14
    Rep Power
    0
    I would appreciate if someone can shed some light on this.
    Is this something can't be done? or doable somehow.

    Originally Posted by sujoydc
    Hello Linux Gurus,
    I have a typical requirement:
    1. I have a file where I kept all the commands in each line like key=value
    eg.
    Properties file entry Code:
     
    check_java_command=`ls -ld /app/java/jdk160_20-linux64bit | awk '{print $8}'`
    check_java_value="/app/java/jdk160_20-linux64bit"
     
    check_jrockit_command="ls -ld /app/java/jrockit28jdk160_20-64b | awk '{print $8}'"
    check_jrockit_value="/app/java/jrockit28jdk160_20-64b"


    2. Then I loop through this file like below:

    Code:
    grep '_command' command.properties | awk -F= '{print $1}' | while read command; do
    echo "Command:"$command
                    if [[ "$command" != \#* ]] ; then
                            KEY=${command%_command}
                            echo "Key is:"$KEY
                            COMMAND=`grep '^'"$KEY"'_command' command.properties | awk -F= '{print $2}'`
                            VALUE=`grep '^'"$KEY"'_value' command.properties | awk -F= '{print $2}'`
                            echo "Command="$COMMAND
                            echo "Value="$VALUE
                            COMMAND=eval ${COMMAND}
                            echo "Executed Command  :"$COMMAND
                            if [[ "$COMMAND" == "$VALUE" ]]; then
                                    echo $KEY executed successfully...
                                    
                            else
                                    echo $KEY execution failed...
                                    
                            fi
                    else
                            echo "This is a comment!"
                    fi
                 done
    3. Now the command is getting read from the file nicely but I can't execute that command. I tried using eval/$/another ``/" " sign and what not.

    I don't have fundamental knowledge so please guide me what's wrong here and how can I fix it?

    Thanks,
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    14
    Rep Power
    0
    Found the answer:

    `eval "$COMMAND"`


    Originally Posted by sujoydc
    I would appreciate if someone can shed some light on this.
    Is this something can't be done? or doable somehow.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,446
    Rep Power
    1751
    It's a horrible way of doing it, are all the things in that file the same format? If so there's are far nicer ways of doing this. You seem to have two related lines for each item, and use those to determine if that item (a file name) exists. If all you had was the filename then all you'd need to do woudl be to loop over the contents and have the actual commands explicitly in your script:

    Code:
    for file in $(grep -v "^#" /path/to/config.file)
    do
      if [ -f $file ]
      then
        echo "exists"
      else
        echo "not exists"
      fi
    done
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    14
    Rep Power
    0
    Hi Simon,
    Thanks for your reply.
    I not only have folders to check, I can have any type of unix commands there. So, that's why i can't always check -f.

    Thanks,
    ~Sujoy

IMN logo majestic logo threadwatch logo seochat tools logo