|
| |||
| running a command stored in a script variable Consider the following script ... #!/bin/bash i="one two three.wav" ls -l "${i}" out_linetext="ls -l \"${i}\"" echo "generated stmt ... [$out_linetext]" $out_linetext when this is run, the results are ... /home/user> xls -rw-r--r-- 1 user user 5365804 2008-01-26 09:40 one two three.wav generated stmt ... [ls -l "one two three.wav"] ls: "one: No such file or directory ls: two: No such file or directory ls: three.wav": No such file or directory /home/user> note that "ls" is run twice. The first time ... ls -l "${i}" it works with no problem. The second time, I simply constructed the entire statement and stored it into a variable and then attempted to "run" what was in that variable ... $out_linetext and that's when the errors occurred. you can see from the echo of the value of $out_linetext that the statement was constructed with the correct syntax. So why did it work the first time but not the second time? |
| |||
| Re: running a command stored in a script variable wylbur37 wrote: > Consider the following script ... > > #!/bin/bash > i="one two three.wav" > ls -l "${i}" > out_linetext="ls -l \"${i}\"" > echo "generated stmt ... [$out_linetext]" > $out_linetext i="one two three.wav" ls -l "${i}" The above command looks for a file named 'one two three.wav' (which is a legal file name). If you have such a file in the current directory (which I suppose is the case since you get no errors), the command succeeds. out_linetext="ls -l \"${i}\"" After this command, the variable out_linetext contains exactly the following: ls -l "one two three.wav" If you try to run this as a command, the shell does not re-evaluate the command and treats '"one', 'two' and 'three.wav"' as three different file names (all legal) and does not find them. What you really want is probably eval "$out_linetext" |
| Bookmarks |
| Thread Tools | |
| |
| | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Command prompt random variable | jimshoe2112 | Windows XP | 1 | 09-20-2007 11:20 PM |
| How select a Sheet if the Name is stored in a variable | TeddyBear | Microsoft Office | 1 | 07-29-2007 05:01 AM |
| Specify environmental variable when starting command prompt? | Don Culp | Windows XP | 1 | 07-19-2007 04:40 AM |
| How do you pass a vb script variable to a batch file? | Mike | Windows XP | 13 | 01-04-2007 03:03 AM |
| How do you pass a vb script variable to a batch file? | Mike | Windows XP | 2 | 01-04-2007 03:02 AM |
| New To Technology Questions? | Do You Need Help with Your Computer or Device? | Do You Need Help with this site? |