Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run text file as commands in Bash

If I have a text file with a separate command on each line how would I make terminal run each line as a command? I just don't want to have to copy and paste 1 line at a time. It doesn't HAVE to be a text file... It can be any kind of file that will work.

example.txt:

sudo command 1 sudo command 2 sudo command 3 
like image 552
Blainer Avatar asked Mar 22 '12 15:03

Blainer


People also ask

How do I run a text file in bash?

Using bash We can run our file by using the bash command: $ bash sample. txt Hello World! Note that to run our file with bash, we don't need to use chmod to gain execution permissions.

How do you run a command in a text file?

Right click on the text file, select properties, select permission, mark the "Let this file be executed" text box. Now you can execute it just by double clicking on the file.

How do I run a file in Terminal bash?

This method is quite easy to run a bash script, and all of them are quite simple. We just need to type in “source” before the file/script name with an extension. In a terminal, run the following code by replacing the filename with your bash script filename. The script will simply get executed after “sourcing” the file.

How do I make a bash file executable in Linux?

The first step to creating a new text file is to click the.SH extension. The second thing you need to do is add #!/bin/bash to the top. This is necessary for the “make it executable” command. The third way to do it is to add lines that you normally would type in your command line.

How do I run a script in Linux terminal?

How Do You Run Linux Commands In A File? you will need to open the terminal and navigate to the directory where the script will be created. You need the.sh extension to create a file. Using an editor, create a script. you will need the following command: chmod +x “fileName”.

How to run commands from a text file in Linux?

How To Run Commands From A Text File In Linux? If you right-click the text file, edit properties, select permission, and mark “Let this file be run” the text box will appear. The file can now be executed in two clicks rather than dragging and dropping.

How do I change the permissions of a file in Bash?

add /bin/bash at starting of the file 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


2 Answers

you can make a shell script with those commands, and then chmod +x <scriptname.sh>, and then just run it by

./scriptname.sh 

Its very simple to write a bash script

Mockup sh file:

#!/bin/sh sudo command1 sudo command2  . . . sudo commandn 
like image 87
Chaos Avatar answered Sep 23 '22 10:09

Chaos


you can also just run it with a shell, for example:

bash example.txt  sh example.txt 
like image 42
kclair Avatar answered Sep 24 '22 10:09

kclair