Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terminal (OS X), a queue of commands from file

I want to execute a queue of commands in the Mac terminal. Is it possible to prepare a file which contains a series of commands, and to submit the file through the terminal? For example... commands.txt contains several commands, one per line:

./executable_file -f sub_a 100
./executable_file -f sub_b 100
./executable_file -f sub_c 100
./executable_file -f sub_d 100
...

How can I do that?

like image 915
Stingery Avatar asked Dec 20 '11 12:12

Stingery


People also ask

Is macOS Terminal the same as Linux?

In general, both will have the same core commands and features (especially those defined in the Posix standard), but a lot of extensions will be different. For example, linux systems generally have a useradd command to create new users, but OS X doesn't.


3 Answers

Maybe I am misunderstanding you, but this sounds like a typical case of a shell script file.

Create a file "myscript.sh", add your commands to it. Make the very first line

#!/bin/sh

to tell OS X what to do with the file. Then mark it executable (chmod +x myscript.sh) and run it per ./myscript.sh.

That should do the trick.

like image 179
Andrew J. Brehm Avatar answered Oct 18 '22 04:10

Andrew J. Brehm


You can do this with a shell script, which contains these commands. Typically, the extension would be .sh (so call it commands.sh instead of commands.txt).

You can then run it using bash commands.sh.

In addition, if the first line of your file is #!/bin/bash and if you make it executable (for example, via chmod 755 commands.sh), you should be able to execute this file directly: ./commands.sh.

Other lines starting with # (or anything after # on a line) will be comments, if you need.

Here are some tutorials:

  • BASH Programming - Introduction HOW-TO
  • Advanced Bash-Scripting Guide

There are other shells than bash too, but bash is the default on OSX (same as /bin/sh): it's what you're already running within Terminal. It's also the default on the number of Linux distributions.

like image 20
Bruno Avatar answered Oct 18 '22 03:10

Bruno


Yes. This is called a shell script, and is a function of your shell (probably bash, unless you've changed it), not Terminal.app itself.

There are countless resources on the web about shell scripting, but in the basic form, just start the file with the line

#!/bin/bash

make it executable with the command chmod +x commands.txt and put your commands in the file in the order you want to run them. (Note that using a .txt extension, while perfectly legal, may be a bit confusing).

like image 1
Wooble Avatar answered Oct 18 '22 02:10

Wooble