[NTLUG:Discuss] bash question: executing a list of commands

Lance Simmons lance at lsimmons.net
Sun Feb 22 21:45:56 CST 2004


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.)

-- 
Lance Simmons



More information about the Discuss mailing list