User Tools

Site Tools


wiki:2016chaining

CHAINING AND SCRIPTING

Chaining with ;

So far, we've typically executed one command per line. However, we learned to pipe commands together, which is a way of combining two commands into one.

We can also tell the shell to execute two commands in succession. This is called chaining and it uses the ;, semi-colon.

chaining usage:
command1; command2

:!: Exercise: Try it:

$mkdir testdir; touch testfile.txt

The shell waits for the first command to finish before the second command completes. You can command multiple commands together.


Basic scripting

We can also write very basic scripts by adding lists of commands into a script.

:!: Exercise: Copy and paste the following text into a script and name it testscript.sh:

#!/bin/bash

#make a directory
mkdir scriptdir

#make a readmefile
touch readmefile.txt

Execute the command by returning to the command prompt and executing:

$ bash testscript.sh

Alternatively, you can make your script executable with the chmod command (change mode). Once it is executable, you can execute it with ./testscript.sh

$ chmod 744 testscript.sh
$ ./testscript.sh

:!: Learn more on your own: We'll cover more about permissions later in the class. You can learn more about permissions and chmod on your own here Permissions

:!: Common pitfall: Comment your code!!! This makes your code readable to other users. Remember other user is you, 2 months from now.

:!: Common pitfall: Test your scripts frequently for proper behavior. Even the best programmers test their code every few lines.

Supercomputing

wiki/2016chaining.txt ยท Last modified: 2017/08/29 09:00 by erin