InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Running Several Commands in Sequence in bash
Bash

Running Several Commands in Sequence in bash

InfinityCoder February 9, 2017

You need to run several commands, but some take a while and you don’t want to wait for the last one to finish before issuing the next command.

There are three solutions to this problem, although the first is rather trivial: just keep typing.

A Linux or Unix system is advanced enough to be able to let you type while it works on your previous commands, so you can simply keep typing one command after another.
Another rather simple solution is to type those commands into a file and then tell bash to execute the commands in the file—i.e., a simple shell script.
Assume that we want to run three commands: long, medium, and short, each of whose execution time is reflected in its name.

We need to run them in that order, but don’t want to wait around for long to finish before starting the other commands.

We could use a shell script (aka batch file). Here’s a primitive way to do that:

1
2
3
4
5
6
$ cat > simple.script
long
medium
short
^D                    # Ctrl-D, not visible
$ bash ./simple.script

The third, and arguably best, solution is to run each command in sequence. If you want to run each program, regardless if the preceding ones fail, separate them with semicolons:

1
$ long ; medium ; short

If you only want to run the next program if the preceding program worked, and all the programs correctly set exit codes, separate them with double-ampersands:

1
$ long && medium && short

The cat example was just a very primitive way to enter text into a file.

We redirect the output from the command into the file named simple.script (for more on redirecting output, see Chapter 3).

Better you should use a real editor, but such things are harder to show in examples like this.

From now on, when we want to show a script, we’ll just either show the text as disembodied text not on a command line, or we will start the example with a command like cat filename to dump the contents of the file to the screen (rather than redirecting output from our typing into the file), and thus display it in the example.
The main point of this simple solution is to demonstrate that more than one command can be put on the bash command line.

In the first case the second command isn’t run until the first command exits, and the third doesn’t execute until the second exits and so on, for as many commands as you have on the line.

In the second case the second command isn’t run unless the first command succeeds, and the third doesn’t execute until the second succeeds and so on, for as many commands as you have on the line.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Handling Lists of Parameters with Blanks in bash
OK, you have quotes around your variable as the previous …

Handling Lists of Parameters with Blanks in bash

Setting Shell History Options in bash
You’d like more control over your command-line history. Set the …

Setting Shell History Options in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap