[NTLUG:Discuss] bash question: executing a list of commands
Victor Brilon
victor at victorland.com
Sun Feb 22 22:05:15 CST 2004
The reason this doesn't work (at least in this example) will become
evident if you echo $i in your loop. It's being read like this:
ls
-l
ls
-lt
ls
-ltr
And so on.
It's possible to work around this just not easy ;)
The thing I'd recommend to do is something like (assuming "command" is
the program that generates other commands):
command > tmp_file
bash tmp_file
rm tmp_file
Victor
Lance Simmons wrote:
> Suppose I have a file named foo with a series of commands I want
> execute. E.g.,
>
> ls -l
> ls -lt
> ls -ltr
>
> How do I execute the lines of foo in order? I've tried
>
> for i in `cat foo`; do "$i"; done
> for i in "`cat foo`"; do "$i"; done
>
> and several others, but haven't been able to make it work.
>
> I've tried using xargs, by editing foo to contain only the lines
>
> -l
> -lt
> -ltr
>
> and then doing
>
> xargs -p ls < foo
>
> but the resulting command line is
>
> ls -l -lt -ltr
>
> not the desired
>
> ls -l
> ls -lt
> ls -ltr
>
> What am I missing? How do I take an arbitrary list of commands and
> execute them seriatim?
>
> (As to why I don't just put the commands in a script and execute them
> directly, the answer is that I want to build the list of commands as
> output from another command. In other words, I want the equivalent of
> the find command's -exec option.)
>
More information about the Discuss
mailing list