Linux 从txt文件运行bash命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13567947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:52:30  来源:igfitidea点击:

Run bash commands from txt file

linuxfilebash

提问by Андрей

I have some commands in txt file and I need to execute all them line by line. How could I do it?

我在 txt 文件中有一些命令,我​​需要逐行执行所有这些命令。我怎么能做到?

采纳答案by Chris Seymour

Just do bash file:

只是做bash file

$ cat file 
date
echo '12*12' | bc

$ bash file
Mon Nov 26 15:34:00 GMT 2012
144

In case of aliases just run bash -i file

如果是别名,只需运行 bash -i file

No need to worry about file extensions or execution rights.

无需担心文件扩展名或执行权限。

回答by Rubens Mariuzzo

In a Terminal just type:

在终端中只需输入:

bash path/to/my/file.txt

And you will get each lines executed.

你会得到每一行的执行。

回答by Debobroto Das

just change the extension of the file to .sh

只需将文件的扩展名更改为 .sh

add /bin/bash at starting of the file

在文件开头添加 /bin/bash

change the permission of the file to executable

修改文件权限为可执行

than simply run ./filename.sh command in command line. all the commands in the file will be executed

而不是简单地在命令行中运行 ./filename.sh 命令。文件中的所有命令都会被执行

回答by Cradam

Change the extension to .sh and for the first line #!/usr/bin/env bashand as someone else said chmod +x

将扩展名更改为 .sh 和第一行#!/usr/bin/env bash,正如其他人所说chmod +x