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?
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.
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.
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:
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With