简体   繁体   中英

How can I execute list of commands from a file in Bash Shell Script

I have a file "commands.txt" with some commands in it for example:

pwd
wc -l commands.txt

And when I run the following command, its not executing the commands.

export IFS=$'\n' (I did this so that I could avoid breaking up the command line from the file)
for i in `cat commands.txt`; do  $i; done

Any help would be greatly appreciated.

Regards, RSR

Since you are setting IFS to \\n , your second line wc -l commands.txt is not being word-split correctly and is being treated as a single command instead of the command wc followed by parameter commands.txt . Do not set IFS , use a while loop instead

while read -r com; 
do 
    $com; 
done < commands.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM