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

    Join Date
    Jun 2006
    Posts
    3
    Rep Power
    0

    [Bash] execute string as command


    Hi,

    I am trying to execute a string as command.

    Code:
    txt="echo \"abc\""
    `$txt`
    result
    Code:
    ./test.sh: line 2: "abc": command not found
    What am I doing wrong?

    Thanks.

    Michael
  2. #2
  3. fork while true;
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2005
    Location
    England, UK
    Posts
    5,538
    Rep Power
    1051
    If you just type $txt, it will run the command. What you're doing is making it do this:

    eval "echo \"abc\""

    then eval the return

    eval "abc"

    Suggested usage: $txt

    Comments on this post

    • ishnid agrees
    • Ehlanna agrees
    • codergeek42 agrees
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2006
    Posts
    3
    Rep Power
    0

    Thumbs up


    Originally Posted by LinuxPenguin
    If you just type $txt, it will run the command. What you're doing is making it do this:

    eval "echo \"abc\""

    then eval the return

    eval "abc"

    Suggested usage: $txt
    Thanks for your clear explanation. Problem solved.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2009
    Posts
    9
    Rep Power
    0

    Arrow Taking it to the next level


    Hello all this is my first post on this forum and I have a question closely related to this. Hope I'm posting in the right place.

    While the above solution works for a single command like:

    Code:
    #!/bin/bash
    txt="cat /etc/hostname"
    $txt
    It doesn't seem to work for commands like:

    Code:
    #!/bin/bash
    txt="cat /etc/hostname > tmp_file"
    $txt
    It seems to pass the pipe and everything after it to the base application (cat in this case). The same thing happens with the other pipes <, <<, >>, | . I need to find a solution that will work with all of these possibilities and will work no matter how many of them are used. I'm guessing there is a way to escape these commands to get them to work right but as of yet I can't fig out how.

    The reason I need this to work is because I have made a function which takes any command and displays it to the user before it runs (asking them if they really want to run the command) and then runs it if they just press enter or alternatively skips the command if they press s+enter (also there are some other options for the user but this is the important stuff). Here is a sample of the basic idea:

    Code:
    #!/bin/bash
    function _exec {
      cmd=${1:-"pwd"}
      ${cmd}
    }
    echo "This Works:"
    _exec "cat /etc/hostname"
    
    echo "This Doesn't Work:"
    _exec "cat /etc/hostname > tmp_file"
    If you can get that to work right I'll be able to get my scripts to work right. Any help would be appreciated. BTW, this is for an integral part of as OSS bash library I'm working on. Anyone who helps me will be credited in the release .
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,436
    Rep Power
    1688
    Have you tried passing the contents of the file one line at a time to the exec function?

    Look at using the read function in combination with a while loop and having the call the _exec inside the loop.
    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
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2009
    Posts
    9
    Rep Power
    0
    No I haven't. This function _exec is going to be part of a library of a few hundred bash functions which are intended to make bash scripts safer and easier to write. One of the things I am planning to do is to create a script that will convert your old scripts into scripts which use the new functions. That script will basically work like what you are suggesting except instead of immediately calling _exec it will just be writing out the command to call _exec to a new script file.

    But in the end this function (_exec) will need to work pretty much as it does now (without the aforementioned problems) if any of this is going to work at all because after I parse out a line of the script I'm still going to need to be able to call the _exec function with redirects and pipes as part of the arguments.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2009
    Posts
    9
    Rep Power
    0
    BTW, I forgot to thank you for the suggestion. Also, to verify that this is still a problem I have written a version which reads a file and it still has the same problem it uses these two files:

    test_data:
    Code:
    cat /etc/hostname
    cat /etc/hostname > tmp_file
    test3:
    Code:
    #!/bin/bash
    
    function _exec {
      cmd=${1:-"pwd"}
      ${cmd}
    }
    
    cat test_data |while read line; do _exec "${line}"; done
    And the output looks like this:
    Code:
    krowe@jeos-base $ ./test3
    jeos-base
    jeos-base
    cat: >: No such file or directory
    cat: tmp_file: No such file or directory
    update:
    This also doesn't work:
    Code:
    #!/bin/bash
    cat test_data |while read line; do ${line}; done
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2009
    Posts
    9
    Rep Power
    0
    Right now I think I'm going to need to write a series of functions to handle this. Like this:

    Code:
    #!/bin/bash
    
    function _exec_gt {
      cmd1=${1:-"pwd"}
      cmd2=${2:-""}
      ${cmd1} > ${cmd2}
    }
    
    echo "This works but is not going to be the best solution:"
    _exec_gt "cat /etc/hostname" "tmp_file"
    It would be better if these could all be handled with just one function. This will make the conversion script discussed earlier much more difficult to write.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    You can accomplish what you would like by using eval:
    Code:
    #!/bin/bash
    
    fun="cat t.t > tmp_file"
    eval $fun
    And my results
    Code:
    $ rm tmp_file
    $ ./eval.sh
    $ diff t.t tmp_file
    $
    [edit]
    As an aside, you really should either search the forums (this answer exists already) or create a new thread to announce your issue. Bumping an old thread is not the best etiquette.
    [/edit]
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2009
    Posts
    9
    Rep Power
    0
    Thanks so much. That works great! I hope that anyone reading this post will now have a better understanding of the difference between using ?>${cmdstr} and ?>eval ${cmdstr}
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2011
    Posts
    1
    Rep Power
    0

    opening a string retreived from databse for variable value insertion from user.


    ok so here is the problem, and its getting annoying because my requirement is such :

    lets say ive got the following string stored in mysql database in column 'info' of table 'TBL':

    "ur name is $name"

    and lets say ive got this code :
    =================================================
    #bin/bash
    echo -n "enter your name: ";
    read -e $name; /*read input from user an store it in $name*/

    result=$(mysql -u USER -p -e "use xyz_db; select info from TBL where id=1;

    echo $result;


    ================================================

    I want the string retrieved from database and stored in the variable "result", ("ur name is $name"), to be opened some how in this code so that the user supplied input of "$name" in this code be put in the variable "$name" of the retreived string from data base.

    so when it asks: and a name is entered, like

    enter your name: jhon

    it should put that value in the string retrived from database which contains variable $name. and the out put should come as:

    "ur name is jhon"
    ===============================================

    its something that i need to do...and its getting very annoying . its my requirment that the string stored in the database should contain the variable that will store the input given by the user in the program.

    I would REALLYY apriciate any bright ideas or ways or changes in this code to make this work.

    best regard
    Desperate_scripter
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Location
    the Netherlands
    Posts
    32
    Rep Power
    12
    Originally Posted by krowe.dev
    Thanks so much. That works great! I hope that anyone reading this post will now have a better understanding of the difference between using ?>${cmdstr} and ?>eval ${cmdstr}
    Indeed. Thank you both.

IMN logo majestic logo threadwatch logo seochat tools logo