質問

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