Basic Script Building
IN THIS CHAPTER
Using multiple commands
Creating a script file
Displaying messages
Using variables
Redirecting input and output
Pipes
Performing math
Exiting the script
Now that we've covered the basics of the Linux system and the command line, it's time to start coding. This chapter discusses the basics of writing shell scripts. You need to know these basic concepts before you can start writing your own shell script masterpieces.
Using Multiple Commands
So far you've seen 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, even possibly 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 Feb 21 15:36:09 EST 2014 Christine tty2 2014-02-21 15:26 Samantha tty3 2014-02-21 15:26 Timothy tty1 2014-02-21 15:26 user tty7 2014-02-19 14:03 (:0) user pts/0 2014-02-21 15:21 (:0.0) $
Congratulations, you just wrote a shell script! This simple script uses just two bash 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 ...
Get Linux Command Line and Shell Scripting Bible, 3rd Edition 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.