Meaningless Drivel is fun!*
The moose likes Linux / UNIX and the fly likes Execute String as a Unix Command Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Execute String as a Unix Command" Watch "Execute String as a Unix Command" New topic
Author

Execute String as a Unix Command

Sujoy Choudhury
Ranch Hand

Joined: Sep 17, 2008
Posts: 136

Hello Linux/Unix Gurus,

I have a typical requirement:
1. I have a file where I kept all the commands in each line like key=value
eg.



2. Then I loop through this file like below:




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.

Thanks,

Thanks and Regards,
~Sujoy
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5811
    
    7

Why not simply this:

$COMMAND

In other words, this should work:

$CMD="ls -al"
$CMD


JBoss In Action
Sujoy Choudhury
Ranch Hand

Joined: Sep 17, 2008
Posts: 136

Hi Peter,
Thanks for your reply.

If you see the statement
echo "Command="$COMMAND
This should have executed the command right?
But it is not.

I am really confused.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15949
    
  19

The "echo" command takes whatever text follows it on the command line and displays it on stdout. It does not execute that text.

Please remember that permitting an arbitrary string to be executed as a command is not something to do casually. When done in the wrong place at the wrong time, it can be used as a security exploit.


Customer surveys are for companies who didn't pay proper attention to begin with.
Sujoy Choudhury
Ranch Hand

Joined: Sep 17, 2008
Posts: 136

Hi Tim,
Yes, I got your security point. And I have seen that too in my code if I load the file and get the values using $KEY sign, the command gets executed at that point of time which I don't want to.
That's why I changed my code and 'grep'ing line by line to read the values.
But in this approach I can't execute the value as a Unix command.
Is this doable or I am doing something very wrong?

Thanks,
Sujoy Choudhury
Ranch Hand

Joined: Sep 17, 2008
Posts: 136

Found the answer here
http://www.unix.com/shell-programming-scripting/139327-execute-command-stored-string.html

anyway thanks a lot guys for reading this post...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Execute String as a Unix Command
 
Similar Threads
How to capture shell scripts ouput to a log file .
Remote Debugging using Jbuilder X and Jboss 3.2.x
to check and start a process
Creating Platform independent scripts(Solaris and Linux)
Multiple instances of Tomcat 6, class loading experiencing problems for each instance