Executing commands containing space in Bash
Try changing the one line to eval $cmds
rather than just $cmds
You can replace your script with the command
sh cmd
The shell’s job is to read commands and run them! If you want output/progress indicators, run the shell in verbose mode
sh -v cmd
Edit: Turns out this fails on pipes and redirection. Thanks, Andomar.
You need to change IFS
back inside the loop so that bash knows where to split the arguments:
IFS=$'\n'
clear
for cmds in `cat cmd`
do
if [ $cmds ] ; then
IFS=$' \t\n' # the default
$cmds;
echo "****************************";
IFS=$'\n'
fi
done
I personally like this approach better - I don't want to munge the IFS if I don't have to do so. You do need to use an eval if you are going to use pipes in your commands. The pipe needs to be processed by the shell not the command. I believe the shell parses out pipes before the expanding strings.
Note that if your cmd file contains commands that take input there will be an issue. (But you can always create a new fd for the read command to read from.)
clear
while read cmds
do
if [ -n "$cmds" ] ; then
eval $cmds
echo "****************************";
fi
done < cmd