Chapter 25. Shell Scripts

Secrets in This Chapter

  • What are Shell Scripts?

  • Shell Script Basics

  • Creating Your Own Scripts

This chapter examines the world of Ubuntu shell scripts. Shell scripts allow you to write programs to automate tasks instead of having to manually process them. The chapter starts out by showing how shell scripts work, then it walks you through how to create your own shell scripts to help automate your daily Ubuntu tasks.

Using Multiple Commands

In Chapter 19, "The Ubuntu Command Line," you saw how to use the command-line interface (CLI) prompt of the shell to enter commands and view the command results. The key to shell scripts is the ability to enter multiple commands and process the results from each command, possibly even passing the results of one command to another. The shell allows you to chain commands together into a single step.

If you want to run two commands together, you can enter them on the same prompt line, separated with a semicolon:

$ date ; who
Mon Nov 03 19:44:35 EST 2008
rich :0 2008-11-03 18:23 (console)
rich pts/1 2008-11-03 18:24
rich pts/0 2008-11-03 18:42
barbara pts/2 2008-11-03 19:30
katie pts/3 2008-11-03 19:39
$

Congratulations, you just wrote a shell script! This simple script uses just two shell commands. The date command runs first, displaying the current date and time, followed by the output of the who command, showing who is currently logged on to the system. Using this technique you can string together as many commands as you wish, up ...

Get Ubuntu® Linux® Secrets now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.