Tushar / Lakshman | Linux Shell Scripting Cookbook, Second Edition | E-Book | www.sack.de
E-Book

E-Book, Englisch, 384 Seiten

Tushar / Lakshman Linux Shell Scripting Cookbook, Second Edition

Don't neglect the shell – this book will empower you to use simple commands to perform complex tasks. Whether you're a casual or advanced Linux user, the cookbook approach makes it all so brilliantly accessible and, above all, useful.
2. Auflage 2025
ISBN: 978-1-78216-275-9
Verlag: De Gruyter
Format: PDF
Kopierschutz: Adobe DRM (»Systemvoraussetzungen)

Don't neglect the shell – this book will empower you to use simple commands to perform complex tasks. Whether you're a casual or advanced Linux user, the cookbook approach makes it all so brilliantly accessible and, above all, useful.

E-Book, Englisch, 384 Seiten

ISBN: 978-1-78216-275-9
Verlag: De Gruyter
Format: PDF
Kopierschutz: Adobe DRM (»Systemvoraussetzungen)



The shell remains one of the most powerful tools on a computer system 'Äî yet a large number of users are unaware of how much one can accomplish with it. Using a combination of simple commands, we will see how to solve complex problems in day to day computer usage.Linux Shell Scripting Cookbook, Second Edition will take you through useful real-world recipes designed to make your daily life easy when working with the shell. The book shows the reader how to effectively use the shell to accomplish complex tasks with ease.The book discusses basics of using the shell, general commands and proceeds to show the reader how to use them to perform complex tasks with ease.Starting with the basics of the shell, we will learn simple commands with their usages allowing us to perform operations on files of different kind. The book then proceeds to explain text processing, web interaction and concludes with backups, monitoring and other sysadmin tasks.Linux Shell Scripting Cookbook, Second Edition serves as an excellent guide to solving day to day problems using the shell and few powerful commands together to create solutions.

Tushar / Lakshman Linux Shell Scripting Cookbook, Second Edition jetzt bestellen!

Weitere Infos & Material


Playing with file descriptors and redirection


File descriptors are integers that are associated with file input and output. They keep track of opened files. The best-known file descriptors are , , and . We even can redirect the contents of one file descriptor to another. This recipe shows examples on how to manipulate and redirect with file descriptors.

Getting ready


While writing scripts we use standard input (), standard output (), and standard error () frequently. Redirection of an output to a file by filtering the contents is one of the essential things we need to perform. While a command outputs some text, it can be either an error or an output (nonerror) message. We cannot distinguish whether it is output text or an error text by just looking at it. However, we can handle them with file descriptors. We can extract text that is attached to a specific descriptor.

File descriptors are integers associated with an opened file or data stream. File descriptors 0, 1, and 2 are reserved as follows:

  • 0: (standard input)
  • 1: (standard output)
  • 2: (standard error)

How to do it...


  1. Redirecting or saving output text to a file can be done as follows:
    $ echo "This is a sample text 1" > temp.txt

    This would store the echoed text in by truncating the file, the contents will be emptied before writing.

  2. To append text to a file, consider the following example:
    $ echo "This is sample text 2" >> temp.txt
  3. You can view the contents of the file as follows:
    $ cat temp.txt This is sample text 1 This is sample text 2
  4. Let us see what a standard error is and how you can redirect it. messages are printed when commands output an error message. Consider the following example:
    $ ls + ls: cannot access +: No such file or directory

    Here is an invalid argument and hence an error is returned.

    Tip


    Successful and unsuccessful commands

    When a command returns after an error, it returns a nonzero exit status. The command returns zero when it terminates after successful completion. The return status can be read from special variable (run immediately after the command execution statement to print the exit status).

    The following command prints the text to the screen rather than to a file (and because there is no output, will be empty):

    $ ls + > out.txt ls: cannot access +: No such file or directory

    In the following command, we redirect to :

    $ ls + 2> out.txt # works

    You can redirect exclusively to a file and to another file as follows:

    $ cmd 2>stderr.txt 1>stdout.txt

    It is also possible to redirect and to a single file by converting to using this preferred method:

    $ cmd 2>&1 output.txt

    Or the alternate approach:

    $ cmd &> output.txt
  5. Sometimes, the output may contain unnecessary information (such as debug messages). If you don't want the output terminal burdened with the details then you should redirect the output to , which removes it completely. For example, consider that we have three files , , and . However, does not have the read-write-execute permission for the user. When you need to print the contents of files starting with , we use the command. Set up the test files as follows:
    $ echo a1 > a1 $ cp a1 a2 ; cp a2 a3; $ chmod 000 a1 #Deny all permissions

    While displaying contents of the files using wildcards (), it will show an error message for file as it does not have the proper read permission:

    $ cat a* cat: a1: Permission denied a1 a1

    Here, belongs to the data. We can redirect the data into a file, whereas remains printed in the terminal. Consider the following code:

    $ cat a* 2> err.txt #stderr is redirected to err.txt a1 a1 $ cat err.txt cat: a1: Permission denied

    Take a look at the following code:

    $ cmd 2>/dev/null

    When redirection is performed for or , the redirected text flows into a file. As the text has already been redirected and has gone into the file, no text remains to flow to the next command through pipe (), and it appears to the next set of command sequences through .

  6. However, there is a way to redirect data to a file, as well as provide a copy of redirected data as for the next set of commands. This can be done using the command. For example, to print in the terminal as well as redirect into a file, the syntax for is as follows:
    command | tee FILE1 FILE2

    In the following code, the data is received by the command. It writes a copy of to the file and sends another copy as for the next command. The command puts a line number for each line received from and writes it into :

    $ cat a* | tee out.txt | cat -n cat: a1: Permission denied 1a1 2a1

    Examine the contents of as follows:

    $ cat out.txt a1 a1

    Note that does not appear because it belongs to . The command can read from only.

    By default, the command overwrites the file, but it can be used with appended options by providing the option, for example, .

    Commands appear with arguments in the format: or simply .

  7. We can use as a command argument. It can be done by using as the filename argument for the command as follows:
    $ cmd1 | cmd2 | cmd -

    For example:

    $ echo who is this | tee - who is this who is this

    Alternately, we can use as the output filename to use .

    Similarly, use for standard error and for standard output. These are special device files that correspond to , , and .

How it works...


For output redirection, and operators are different. Both of them redirect text to a file, but the first one empties the file and then writes to it, whereas the later one adds the output to the end of the existing file.

When we use a redirection operator, the output won't print in the terminal but it is directed to a file. When redirection operators are used, by default, they operate on standard output. To explicitly take a specific file descriptor, you must prefix the descriptor number to the operator.

is equivalent to and similarly it applies for (equivalent to ).

When working with errors, the output is dumped to the file. . is a special device file where any data received by the file is discarded. The null device is often known as a black hole as all the data that goes into it is lost forever.

There's more...


A command that reads for input can receive data in multiple ways. Also, it is possible to specify file...



Ihre Fragen, Wünsche oder Anmerkungen
Vorname*
Nachname*
Ihre E-Mail-Adresse*
Kundennr.
Ihre Nachricht*
Lediglich mit * gekennzeichnete Felder sind Pflichtfelder.
Wenn Sie die im Kontaktformular eingegebenen Daten durch Klick auf den nachfolgenden Button übersenden, erklären Sie sich damit einverstanden, dass wir Ihr Angaben für die Beantwortung Ihrer Anfrage verwenden. Selbstverständlich werden Ihre Daten vertraulich behandelt und nicht an Dritte weitergegeben. Sie können der Verwendung Ihrer Daten jederzeit widersprechen. Das Datenhandling bei Sack Fachmedien erklären wir Ihnen in unserer Datenschutzerklärung.