
Linux Shell Scripting Cookbook, Second Edition
Description
Alles über E-Books | Antworten auf Fragen rund um E-Books, Kopierschutz und Dateiformate finden Sie in unserem Info- & Hilfebereich.
Key Features
Master the art of crafting one-liner command sequence to perform text processing, digging data from files, backups to sysadmin tools, and a lot more
And if powerful text processing isn't enough, see how to make your scripts interact with the web-services like Twitter, Gmail
Explores the possibilities with the shell in a simple and elegant way - you will see how to effectively solve problems in your day to day life
Book DescriptionThe shell remains one of the most powerful tools on a computer system 'AEi 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.What you will learn
Explore a variety of regular usage tasks and how it can be made faster using shell command
Write shell scripts that can dig data from web and process it with few lines of code
Use different kinds of tools together to create solutions
Interact with simple web API from scripts
Perform and automate tasks such as automating backups and restore with archiving tools
Create and maintain file/folder archives, compression formats and encrypting techniques with shell
Set up Ethernet and Wireless LAN with the shell script
Monitor different activities on the network using logging techniques
Who this book is forThis book is both for the casual GNU/Linux users who want to do amazing things with the shell, and for advanced users looking for ways to make their lives with the shell more productive.You can start writing scripts and one-liners by simply looking at the similar recipe and its descriptions without any working knowledge of shell scripting or Linux. Intermediate/advanced users as well as system administrators/ developers and programmers can use this book as a reference when they face problems while coding.
All prices
More details
Other editions
Additional editions

Persons
Content
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 stdin, stdout, and stderr. 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 (stdin), standard output (stdout), and standard error (stderr) 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:
stdin(standard input) - 1:
stdout(standard output) - 2:
stderr(standard error)
How to do it...
- 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
temp.txtby truncating the file, the contents will be emptied before writing. - To append text to a file, consider the following example: $ echo "This is sample text 2" >> temp.txt
- You can view the contents of the file as follows: $ cat temp.txt This is sample text 1 This is sample text 2
- Let us see what a standard error is and how you can redirect it.
stderrmessages are printed when commands output an error message. Consider the following example: $ ls + ls: cannot access +: No such file or directoryHere
+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
$?(runecho $?immediately after the command execution statement to print the exit status).The following command prints the
$ ls + > out.txt ls: cannot access +: No such file or directorystderrtext to the screen rather than to a file (and because there is nostdoutoutput,out.txtwill be empty):In the following command, we redirect
$ ls + 2> out.txt # worksstderrtoout.txt:You can redirect
$ cmd 2>stderr.txt 1>stdout.txtstderrexclusively to a file andstdoutto another file as follows:It is also possible to redirect
$ cmd 2>&1 output.txtstderrandstdoutto a single file by convertingstderrtostdoutusing this preferred method:Or the alternate approach:
$ cmd &> output.txt - Sometimes, the output may contain unnecessary information (such as debug messages). If you don't want the output terminal burdened with the
stderrdetails then you should redirect thestderroutput to/dev/null, which removes it completely. For example, consider that we have three filesa1,a2, anda3. However,a1does not have the read-write-execute permission for the user. When you need to print the contents of files starting witha, we use thecatcommand. Set up the test files as follows: $ echo a1 > a1 $ cp a1 a2 ; cp a2 a3; $ chmod 000 a1 #Deny all permissionsWhile displaying contents of the files using wildcards (
$ cat a* cat: a1: Permission denied a1 a1a*), it will show an error message for filea1as it does not have the proper read permission:Here,
$ cat a* 2> err.txt #stderr is redirected to err.txt a1 a1 $ cat err.txt cat: a1: Permission deniedcat: a1: Permission deniedbelongs to thestderrdata. We can redirect thestderrdata into a file, whereasstdoutremains printed in the terminal. Consider the following code:Take a look at the following code:
$ cmd 2>/dev/nullWhen redirection is performed for
stderrorstdout, 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 throughstdin. - However, there is a way to redirect data to a file, as well as provide a copy of redirected data as
stdinfor the next set of commands. This can be done using theteecommand. For example, to printstdoutin the terminal as well as redirectstdoutinto a file, the syntax forteeis as follows: command | tee FILE1 FILE2In the following code, the
$ cat a* | tee out.txt | cat -n cat: a1: Permission denied 1a1 2a1stdindata is received by theteecommand. It writes a copy ofstdoutto theout.txtfile and sends another copy asstdinfor the next command. Thecat -ncommand puts a line number for each line received fromstdinand writes it intostdout:Examine the contents of
$ cat out.txt a1 a1out.txtas follows:Note that
cat: a1: Permission denieddoes not appear because it belongs tostderr. Theteecommand can read fromstdinonly.By default, the
teecommand overwrites the file, but it can be used with appended options by providing the-aoption, for example,$ cat a* | tee -a out.txt | cat -n.Commands appear with arguments in the format:
command FILE1 FILE2 .or simplycommand FILE. - We can use
stdinas 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 thisAlternately, we can use
/dev/stdinas the output filename to usestdin.Similarly, use
/dev/stderrfor standard error and/dev/stdoutfor standard output. These are special device files that correspond tostdin,stderr, andstdout.
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 1> and similarly it applies for >> (equivalent to 1>>).
When working with errors, the stderr output is dumped to the /dev/null file. ./dev/null 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 stdin for input can receive data in multiple ways. Also, it is possible to specify file...
System requirements
File format: ePUB
Copy protection: Adobe-DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Install the free reader Adobe Digital Editions prior to download (see eBook Help).
- Tablet/smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook before downloading (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (not Kindle).
The file format ePub works well for novels and non-fiction books – i.e., „flowing” text without complex layout. On an e-reader or smartphone, line and page breaks automatically adjust to fit the small displays.
This eBook uses Adobe-DRM, a „hard” copy protection. If the necessary requirements are not met, unfortunately you will not be able to open the eBook. You will therefore need to prepare your reading hardware before downloading.
Please note: We strongly recommend that you authorise using your personal Adobe ID after installation of any reading software.
For more information, see our ebook Help page.
File format: PDF
Copy-Protection: Adobe-DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Install the free reader Adobe Digital Editions prior to download (see eBook Help).
- Tablet/smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook before downloading (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (only limited: Kindle).
The file format PDF always displays a book page identically on any hardware. This makes PDF suitable for complex layouts such as those used in textbooks and reference books (images, tables, columns, footnotes). Unfortunately, on the small screens of e-readers or smartphones, PDFs are rather annoying, requiring too much scrolling.
This eBook uses Adobe-DRM, a „hard” copy protection. If the necessary requirements are not met, unfortunately you will not be able to open the eBook. You will therefore need to prepare your reading hardware before downloading.
Please note: We strongly recommend that you authorise using your personal Adobe ID after installation of any reading software.
For more information, see our eBook Help page.