Skip to main content

Shell Scripts

Shell scripts


$? variable:

In a shell script, we can check the return status immediately after running any command to determine if command is successful or not. like

echo $?

if return status is 0, it indicates success,

 and if the return status is non-zero, typically 1, means failure.


/dev/null

/dev/null is a special device file in Unix-like operating systems (including Linux) that discards all data written to it. It essentially acts as a black hole for data. When data is written to /dev/null, it simply disappears and does not consume any storage space.

Here are some common use cases for /dev/null:

Discarding Output: As mentioned earlier, redirecting output to /dev/null is a common way to discard unwanted output, such as diagnostic messages or verbose output, especially when running scripts or commands in the background where you don't need to see the output.

command >/dev/null  # Redirects stdout to /dev/null

command 2>/dev/null # Redirects stderr to /dev/null

command &>/dev/null # Redirects both stdout and stderr to /dev/null

Silencing Commands: Sometimes you may want to silence a command completely, preventing it from producing any output.

command > /dev/null 2>&1

This redirects both stdout and stderr to /dev/null, effectively silencing the command.

Writing to Nowhere: If a program requires output to be written somewhere but you're not interested in the output, you can redirect it to /dev/null.

echo "Some data" > /dev/null

Dummy Output: /dev/null can also be used as a placeholder when a program expects output but you don't have anything meaningful to provide.

command --input-file=/dev/null  # Provides an empty file as input

Overall, /dev/null is a useful tool for managing output in Unix-like systems, allowing you to effectively discard or redirect data as needed. 


&> 

In shell scripting, stdout refers to the standard output stream, where the normal output of a command is directed. On the other hand, stderr refers to the standard error stream, where error messages and diagnostics are typically sent.

In the redirection syntax &> /dev/null, & before the greater than sign (>) indicates that both stdout and stderr should be redirected. So, &> /dev/null redirects both standard output and standard error to /dev/null, which is a special device file that discards all data written to it.

If you want to redirect only stdout or only stderr, you can use 1> for stdout and 2> for stderr, followed by the desired destination. For example:

command > /dev/null: Redirects only stdout to /dev/null.

command 2> /dev/null: Redirects only stderr to /dev/null.

command 1> output.txt: Redirects only stdout to a file named output.txt.

command 2> error.txt: Redirects only stderr to a file named error.txt.


2>&1

The 2>&1 syntax in shell scripting is used for redirecting the standard error (stderr) to the same destination as the standard output (stdout). Here's what each part means:


  • 2>: Redirects standard error (file descriptor 2).
  • &1: Points to the same destination as file descriptor 1 (stdout).
  • So, 2>&1 essentially means "redirect stderr to the same place as stdout".


Here's an example to illustrate its usage:

command > output.txt 2>&1

In this example:

  • > redirects standard output (stdout) to the file output.txt.
  • 2>&1 redirects standard error (stderr) to the same place as stdout, which in this case is output.txt.
  • Therefore, both stdout and stderr are combined and written to the file output.txt. 

Comments

Popular posts from this blog

Apache Storm vs Apache Flink

 Apache Storm and Apache Flink are both distributed stream processing frameworks, but they have some key differences in terms of architecture, programming models, and features. Here's a comparison between Apache Storm and Apache Flink: 1. **Programming Model:**    - **Apache Storm:** Storm provides a low-level, event-driven programming model using spouts and bolts. Spouts are sources of data, and bolts are the processing units that apply transformations or analyses to the data. It is designed for building complex, directed acyclic graphs (DAGs) of processing stages.        - **Apache Flink:** Flink offers a more high-level and expressive API for stream processing. Flink's API includes a functional programming style using operations like map, flatMap, filter, and windowing operations, making it easier to express complex data transformations. 2. **Event Time Processing:**    - **Apache Storm:** Initially, Storm had challenges in handling event ...

Recover lost files on Windows, free and effective

 Windows File Recovery If necessary, download and launch the app from Microsoft Store. Press the Windows key, enter Windows File Recovery in the search box, and then select Windows File Recovery. When you are prompted to allow the app to make changes to your device, select Yes. In the Command Prompt window, enter the command in the following format:  winfr source-drive: destination-drive: [/mode] [/switches] There are 2 basic modes you can use to recover files: Regular and Extensive.  Regular mode examples Recover your Documents folder from your C: drive to the recovery folder on an E: drive. Don’t forget the backslash (\) at the end of the folder.   winfr C: E: /regular /n \Users\<username>\Documents\  Recover PDF and Word files from your C: drive to the recovery folder on an E: drive.  winfr C: E: /regular /n *.pdf /n *.docx  Extensive mode examples   winfr E: C: /extensive /n *invoice*  Recover jpeg and png photos from your...