What you did is okay, but I think when you want to "run" a variable, you have to eval it:

Code:

eval $cc

. . . but I cannot recall at the moment. It is worth a try.

07-28-2009

MK27

Quote:

Originally Posted by lehe

Hi,
I am trying to redirect my output to a file and need to first put a command into a string.

You will have to redirect each command before &&. However, you cannot effectively run a string with && in it anyway.

One option is to put this into an array, which maybe is what you were trying to do with the brackets before, since that is what they'll do:

Code:

cc=('date' 'bash -c "time ls -l"' 'date')

The reason I used bash -c "time ls -l" is because time is a bash builtin, and if you try to run it from a variable this way you will get bash: time: No such file or directory
Now here's a weird thing about bash. If you do this:

Code:

for e in ${cc[@]}; do echo $e; done

You will get this:date
bash
-c
"time
ls
-l"
date
It seems an element here is not really what one might hope. However, this works:

Code:

for e in 0 1 2; do echo ${cc[$e]}; done

date
bash -c "time ls -l"
date
So, it looks like all we need now is:

Code:

for e in 0 1 2; do ${cc[$e]}>>tmp.txt; done

Which will work (...almost) and amounts to (almost) same thing as using &&. I say almost because in fact you get a strange parsing related error, again from the bash/time/ls construct:ls: -c: line 0: unexpected EOF while looking for matching `"'
ls: -c: line 1: syntax error: unexpected end of file
Since the next "e" does execute dispite the failure of ${cc[1]}, this is not really equivalent to an && -- for that you need to get more complicated.

But I cannot get 'bash -c "time ls"' to run this way. Non builtins with parameters are fine.

I'll go ask some other bunch of monkeys tho and see if they have anything wise to add.

07-28-2009

tabstop

Quote:

Originally Posted by MK27

((stuff))

Or you can use eval. (Thanks Kennedy for getting my one new thing for today out of the way early so I could relax!)