Linux Essentials (010-160): A Time Compressed Resource to Passing the LPI® Linux Essentials Exam on Your First Attempt by Dion Jason

Linux Essentials (010-160): A Time Compressed Resource to Passing the LPI® Linux Essentials Exam on Your First Attempt by Dion Jason

Author:Dion, Jason [Dion, Jason]
Language: eng
Format: epub
Publisher: Dion Training Solutions, LLC
Published: 2020-07-19T16:00:00+00:00


The first line begins with two characters (#!). These two characters are a special code that tells the Linux kernel that this is a script and that the rest of the line should be treated as a pathname to the program that will interpret this script. In this script, it is the path /bin/bash, which is the path to the bash shell program.

Within a shell script, the pound symbol or hash mark (#) is considered a comment character. This causes the script utility to ignore this line, but the kernel still reads it. On most Linux systems, /bin/sh is a symbolic link that points to /bin/bash, but it can also point to another shell. If a user specifies the script as using /bin/sh instead of /bin/bash, this almost certainly guarantees that any Linux system will be able to execute the script since they all have a shell program to run the script. If it is a simple bash script, the script can be run in most other shells. Unfortunately, if the script is more complex, then it will need a specific shell or else it could fail.

After writing the shell script, we must modify the text file to ensure it is now an executable file within the operating system by using the chmod command. The use of chmod will be covered in Chapter 12 (Ownership and Permissions).

As you begin to create shell scripts that are lengthier and more complex, you will want to have a good text editor program. If using a graphical desktop, KWrite and gnome’s gedit editors are excellent for creating text shell scripts. However, if working from a command line, then vi, emacs, pico, or nano can be used instead.

Using Commands in Scripts

The simplest use of a script is to run a series of commands without user intervention. Commands built into the shell and external commands can both be used in scripts. This means that other programs can also be run from within a script.

Most of the commands that a user enters in the shell prompt are external commands. All these programs are located in /bin, /usr/bin, and other directories within the filesystem. These programs, as well as internal commands, can be run by simply using their names within the script. If needed, we can also specify parameters for each of these programs in a script. For instance, consider the following script for copying a file from Downloads to Documents, launching an xterm window, and then starting up the KMail mail reader program inside the GUI:

#!/bin/bash

cp ~/Downloads/file.txt ~/Documents/file.txt

/usr/bin/xterm &

/usr/bin/kmail &



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.