0

I want to write a shell script that takes commands given in a .txt file and execute them.

i have a .txt file with mail commands for multiple users. i want to write a script to invoke and execute the commands given in file. please help

1 Answers1

1

If your input file contains a list of commands you can just pipe it to bash or your favorite shell in order to execute them. You can also (better way of doing) redirect the file as stdin of bash or your favorite shell.

INPUT:

$ more commands.txt 
echo abc
echo 123

OUTPUT:

$ cat commands.txt | bash
abc
123

or even better

$ bash < commands.txt
abc
123

or simply

$ bash commands.txt
abc
123

or the best (add a shebang at the first line of your commands.txt to point to your favorite shell)

$ more commands.txt
#!/bin/bash
echo abc
echo 123

run it after giving execution permissions (chmod u+x commands.txt)

./commands.txt
abc
123

Some shebangs that might interest you:

#!/bin/sh -x
#!/bin/bash
#!/usr/bin/env bash
#!/usr/bin/perl
#!/usr/bin/env perl
#!/usr/bin/tcl
#!/bin/sed -f
#!/usr/awk -f
#!/usr/bin/python 
Allan
  • 12,117
  • 3
  • 27
  • 51