I have a file like this (text.txt):

ls -al
ps -au
export COP=5
clear

Each line corresponds at a command. In my script, I need to read each line and launch each command.

ps: I tried all these options and with all of them I have the same problem with the command "export". In the file there is "export COP=5", but after running the script, if I do echo $COP in the same terminal, no value is displayed

有帮助吗?

解决方案

while IFS= read line; do eval $line; done < text.txt

Be careful about it, it's generally not advised to use eval as it's quite powerful and as easy to be abused.
However, if there is no risk of influence from unprivileged users on text.txt it should be ok.

其他提示

cat test.txt | xargs -l1 bash -c '"$@"' echo

In order to avoid confusion I would simply rename the file from text.txt to text and add a shebang (e.g. #!/bin/bash) as the first line of the file. Make sure it is executable by calling chmod +x text. Afterwards you can execute it as expected.

$ cat text
#!/bin/bash
ls -al
ps -au
clear

$ chmod +x text
$ ./text
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top