Linux Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsOperating SystemsLinux Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 10th, 2012, 01:22 PM
sujoydc sujoydc is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 14 sujoydc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 41 m
Reputation Power: 0
Send a message via Yahoo to sujoydc
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:
Original - 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,

Reply With Quote
  #2  
Old May 11th, 2012, 07:44 AM
sujoydc sujoydc is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 14 sujoydc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 41 m
Reputation Power: 0
Send a message via Yahoo to sujoydc
I would appreciate if someone can shed some light on this.
Is this something can't be done? or doable somehow.

Quote:
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:
Original - 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,

Reply With Quote
  #3  
Old May 11th, 2012, 10:02 AM
sujoydc sujoydc is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 14 sujoydc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 41 m
Reputation Power: 0
Send a message via Yahoo to sujoydc
Found the answer:

`eval "$COMMAND"`


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

Reply With Quote
  #4  
Old May 11th, 2012, 10:11 AM
SimonJM SimonJM is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Mar 2006
Posts: 2,108 SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 4 h 41 m
Reputation Power: 1485
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

Reply With Quote
  #5  
Old May 15th, 2012, 10:02 AM
sujoydc sujoydc is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 14 sujoydc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 41 m
Reputation Power: 0
Send a message via Yahoo to sujoydc
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsLinux Help > Executing String value as a command

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap