Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Linux Shell Scripting

You're reading from   Mastering Linux Shell Scripting Master the complexities of Bash shell scripting and unlock the power of shell for your enterprise

Arrow left icon
Product type Paperback
Published in Dec 2015
Publisher
ISBN-13 9781784396978
Length 198 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Andrew Mallett Andrew Mallett
Author Profile Icon Andrew Mallett
Andrew Mallett
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. What and Why of Scripting with Bash FREE CHAPTER 2. Creating Interactive Scripts 3. Conditions Attached 4. Creating Code Snippets 5. Alternative Syntax 6. Iterating with Loops 7. Creating Building Blocks with Functions 8. Introducing sed 9. Automating Apache Virtual Hosts 10. Awk Fundamentals 11. Summarizing Logs with Awk 12. A Better lastlog with Awk 13. Using Perl as a Bash Scripting Alternative 14. Using Python as a Bash Scripting Alternative Index

The bash command hierarchy

When working on at the bash shell and when you are sitting comfortably at your prompt eagerly waiting to type a command, you will most likely feel that it is a simple matter of typing and hitting the Enter key. You should know better than to think that things are never quite as simple as we imagine.

Command type

For example, if we type and enter ls to list files, it will be reasonable to think that we were running the command. It is possible, but we will be running an alias often. Aliases exist in memory as a shortcut to commands or commands with options; these aliases are used before we even check for the file. The bash shell built-in command type can come to our aid here. The type command will display the type of command for a given word entered at the command line. The types of command is listed as follows:

  • Alias
  • Function
  • Shell built in
  • Keyword
  • File

This list is also a representative of the order in which they are searched. As we can see, it is not until the very end where we search for the executable file ls.

The following command demonstrates the simple use type:

$ type ls
ls is aliased to `ls --color=auto'

We can extend this further to display all the matches for the given command:

$ type -a ls
ls is aliased to `ls --color=auto'
ls is /bin/ls

If we need to just type in the output, we can use the -t option. This is useful when we need to test the command type from within a script and only need the type to be returned. This excludes the superfluous information; thus, making it easier for us humans to read. Consider the following command and output:

$ type -t ls
alias

The output is clear and simple and just what a computer or script requires.

The built-in type can also be used to identify shell keywords such as if, case, function, and so on. The following command shows type being used against multiple arguments and types:

$ type ls quote pwd do id

The output of the command is shown in the following screenshot:

Command type

You can also see that the function definition is printed when we stumble across a function when using type.

Command PATH

Linux will check for executables in the PATH environment only when the full or relative path to the program is supplied. In general, the current directory is not searched unless it is in the PATH. It is possible to include our current directory within the PATH by adding the directory to the PATH variable. This is shown in the following code example:

$ export PATH=$PATH:.

This appends the current directory to the value of the PATH variable each item the PATH is separated using the colon. Now, your PATH is updated to include the current working directory and each time you change directories, the scripts can be executed easily. In general, organizing scripts into a structured directory hierarchy is probably a great idea. Consider creating a subdirectory called bin within your home directory and add the scripts into that folder. Adding $HOME/bin to your PATH variable will enable you to find the scripts by name and without the file path.

The following command-line list will only create the directory, if it does not already exist:

$ test -d $HOME/bin || mkdir $HOME/bin

Although the above command-line list is not strictly necessary, it does show that scripting in bash is not limited to the actual script and we can use conditional statements and other syntax directly at the command line. From our viewpoint, we know that the preceding command will work whether you have the bin directory or not. The use of the $HOME variable ensures that the command will work without considering your current file system context.

As we work through the book, we will add scripts into the $HOME/bin directory so that they can be executed regardless of our working directory.

You have been reading a chapter from
Mastering Linux Shell Scripting
Published in: Dec 2015
Publisher:
ISBN-13: 9781784396978
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime