Command Line Fundamentals
eBook - ePub

Command Line Fundamentals

Learn to use the Unix command-line tools and Bash shell scripting

Vivek N

Share book
  1. 314 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Command Line Fundamentals

Learn to use the Unix command-line tools and Bash shell scripting

Vivek N

Book details
Book preview
Table of contents
Citations

About This Book

Master shell basics and Unix tools and discover easy commands to perform complex tasks with speed

Key Features

  • Learn why the Bash shell is widely used on Linux and iOS
  • Explore advanced shell concepts, such as pipes and redirection
  • Understand how to use Unix command-line tools as building blocks for different tasks

Book Description

The most basic interface to a computerā€”the command lineā€”remains the most flexible and powerful way of processing data and performing and automating various day-to-day tasks.

Command Line Fundamentals begins by exploring the basics, and then focuses on the most common tool, the Bash shell (which is standard on all Linux and iOS systems). As you make your way through the book, you'll explore the traditional Unix command-line programs as implemented by the GNU project. You'll also learn to use redirection and pipelines to assemble these programs to solve complex problems.

By the end of this book, you'll have explored the basics of shell scripting, allowing you to easily and quickly automate tasks.

What you will learn

  • Use the Bash shell to run commands
  • Utilize basic Unix utilities such as cat, tr, sort, and uniq
  • Explore shell wildcards to manage groups of files
  • Apply useful keyboard shortcuts in shell
  • Employ redirection and pipes to process data
  • Write both basic and advanced shell scripts to automate tasks

Who this book is for

Command Line Fundamentals is for programmers who use GUIs but want to understand how to use the command line to complete tasks faster.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on ā€œCancel Subscriptionā€ - itā€™s as simple as that. After you cancel, your membership will stay active for the remainder of the time youā€™ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlegoā€™s features. The only differences are the price and subscription period: With the annual plan youā€™ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, weā€™ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Command Line Fundamentals an online PDF/ePUB?
Yes, you can access Command Line Fundamentals by Vivek N in PDF and/or ePUB format, as well as other popular books in Informatik & Programmierung. We have over one million books available in our catalogue for you to explore.

Information

Year
2018
ISBN
9781789803525
Edition
1

Shell Scripting

Learning Objectives

By the end of this chapter, you will be able to:
  • Use looping and conditional constructs in the shell
  • Use shell functions
  • Perform line-oriented processing using the shell language
  • Write shell scripts
  • Automate data processing tasks with scripts
This chapter explains advanced scripting features such as conditionals and loops, and also describes how to write shell scripts.

Introduction

In the previous chapters, we learned most of the shell constructs and syntax that are used with the interactive command line. We also built a vocabulary of useful commands and learned how to combine them. In this final chapter, we will learn a few more shell constructs and commands, all of which when combined, will let us use the shell as a generic programming language.
The programs we write for the shell's language are called shell scripts. Any task we perform once can be converted into a shell script that can be repeated later. Scripts can be executed just like any other program in the system, giving us a way to automate our workflows. Since the shell can use any program that's installed on the system, by installing the right command-line utilities, we can automate practically anything we do manually.
Once we have learned this, for the remainder of the chapter, we will create several complex shell scripts that perform a non-trivial task, to cement our understanding of how to use the shell effectively. By the end of this chapter, you should be capable of formulating scripts to solve real-world problems on your own.

Conditionals and Loops

As with any other general-purpose programming language, the shell provides conditional and looping constructs. They are especially useful within shell scripts. We will learn some of the commonly used conditionals and loops in further sections.

Note

Conditionals and loops (along with functions, which we will cover later) make the shell language Turing complete. This is a computer science notion that means that it is equivalent to any general programming language in terms of its capabilities.

Conditional Expressions

As we saw in the previous chapter, every command returns an exit code that is interpreted as true and false for zero and non-zero values, respectively. A conditional expression is any shell command evaluating to true or false. One mechanism that augments this is the [[ ]] construct, which provides a way to test for a condition and return a Boolean. This syntax is as follows:
[[ EXPRESSION ]]
Here, EXPRESSION has a syntax similar to arithmetic expressions with the following operators (as discussed in the subsequent paragraphs). Note that the whitespace after the [[ and before ]] is mandatory.
File Operators
These operators return true or false, depending on some property of a file or directory:
  • -e FILE: Returns true if FILE exists either as a file or a directory. For instance, let's create a file called test and check whether it exists using this operator:
    robin ~ $ touch test
    robin ~ $ [[ -e test ]] && echo File or directory exists
    File or directory exists
    We can also use this to check for files that do not exist, as follows:
    robin ~ $ [[ -e none ]] || echo File or directory missing
    File or directory missing
  • -f FILE: Returns true if FILE exists. For instance, look at the following snippet:
    robin ~ $ touch test
    robin ~ $ [[ -f test ]] && echo test exists and is a file
    test exists and is a file
  • -d DIR: Returns true if the directory DIR exists. For instance, look at the following snippet:
    robin ~ $ mkdir dir
    robin ~ $ [[ -d dir ]] && echo Dir exists
    Dir exists
    robin ~ $ [[ -d dir1 ]] || echo Dir missing
    Dir missing
String Operators
These operators let us test properties of string literals or strings contained in variables:
  • -z STRING: Returns true if the string is empty. Look at the following example:
    robin ~ $ [[ -z '' ]] && echo EMPTY
    EMPTY
  • -n STRING: Returns true if the string is not empty (its length is not zero), as shown here:
    robin ~ $ [[ -n 'SOMETHING' ]] && echo NOT EMPTY
    NOT EMPTY
  • STRING1 = STRING2 or STRING1 == STRING2: Returns true if the two strings are identical. Let's understand this better with an example. In the following snippet, we have defined three variables with strings, as follows:
    robin ~ $ A=APPLE
    robin ~ $ B=HELLO
    robin ~ $ C=HELLO
    We can use the preceding operator to compare any two strings, as shown here:
    robin ~ $ [[ "$B" = "$C" ]] && echo Same
    Same
  • STRING1 != STRING2: Returns true if the strings are not identical. For instance, for the preceding example, the following will be true:
    robin ~ $ [[ "$A" != "$C" ]] && echo Different
    Different
  • STRING1 < STRING2: Returns true if the first string is lexicographically lower. Look at the following example:
    robin ~ $ [[ "$A" < "$C" ]] && echo Less
    Less
  • STRING1 > STRING2: Returns true if the first string is lexicographically higher, as shown in the following example:
    robin ~ $ [[ "$C" > "$A" ]] && echo Greater
    Greater

    Note

    Remember to always use double quotes for string matching. Whether you use a variable or a literal string as either operand, this is recommended since strings can contain spaces, and other characters. We cannot use single quotes since the variables will not expand to their values.
Glob Operators
These operators allow us to test whether a literal string or a string within a variable matches a shell wildcard expression:
  • STRING = PATTERN or STRING == PATTERN: Returns true if the string matches the glob PATTERN.
  • STRING != PATTERN: Returns true if the string does not match the glob PATTERN.
Notice that the same operator is used for string and glo...

Table of contents