문제

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