Advance Shell Scripting (1-100)🚀
Tags: devops, cloud, linux, shell
Published: 2024-02-13 21:09:02



### Introduction
This documentation provides an overview and examples of various commands and constructs commonly used in Unix/Linux environments for managing processes, manipulating data, and handling conditions.

### Commands

#### `df`
- The `df` command is used to display disk space usage on Unix/Linux systems.
- Example:
  ```bash
  df
  ```

![df command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kl985ig4pkf1mc8ftbms.png)



#### `nproc`
- The `nproc` command prints the number of processing units available to the current process.
- Example:
  ```bash
  nproc
  ```

![nproc command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bs1u2w65crk1znzxatey.png)



#### `top`
- The `top` command displays real-time information about running processes, system load, and CPU usage.
- Example:
  ```bash
  top
  ```

![top command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xjk2t2hl91p2kbi32daf.png)



#### `ps`
- The `ps` command is used to display information about processes.
- Example:
  ```bash
  ps -ef
  ```

![ps command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oqjjzmvpfrbe7f3k8bhv.png)



#### `grep`
- The `grep` command is used to search for specific patterns within files or output.
- Example:
  ```bash
  grep "amazon" /path/to/file
  ```

![grep command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k82ooq60abhlaty58scd.png)



#### `curl`
- The `curl` command is used to transfer data to or from a server.
- Example:
  ```bash
  curl https://example.com
  ```

#### `find`
- The `find` command is used to search for files or directories in a directory hierarchy.
- Example:
  ```bash
  find / -name "filename"
  ```

### Special Commands and Constructs

#### `sudo`
- The `sudo` command is used to execute commands with elevated privileges.
- Example:
  ```bash
  sudo find / -name "filename"
  ```

#### `set`
- The `set` command is used to set shell options.
- Example:
  ```bash
  set -e # exit the script when error
  set -o # debug mode
  set -x # debug mode
  ```

shell Script :


![set command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f1tq0vzjmmh7pcgdocwt.png)

![set command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/abtcpru37qmcjrum4r4k.png)

Certainly! Below are examples demonstrating the usage of `set -e`, `set -x`, and `set -o`.

#### Example 1: `set -e` (Exit on Error)
```bash
#!/bin/bash

# Enable exit on error
set -e

# Command that might fail
ls /nonexistent_directory

# This line won't be executed if the previous command fails
echo "This won't be printed"
```

In this example, if the `ls /nonexistent_directory` command fails (since the directory doesn't exist), the script will terminate immediately due to `set -e`, preventing the subsequent echo command from executing.

#### Example 2: `set -x` (Debug Mode)
```bash
#!/bin/bash

# Enable debug mode
set -x

# Commands to be debugged
echo "Debug mode enabled"
ls -l /etc/passwd
echo "Debug mode disabled"
```

When this script runs, each command will be printed to the terminal before it is executed, allowing the user to see the commands being run. This is useful for troubleshooting and understanding the flow of execution.

#### Example 3: `set -o` (Options)
```bash
#!/bin/bash

# Enable shell options
set -o nounset # Treat unset variables as errors
set -o errexit # Equivalent to set -e, exit on error

# Undefined variable, will trigger an error
echo $undefined_variable

# This line won't be executed due to errexit option
echo "This won't be printed"
```

In this example, `set -o nounset` ensures that using undefined variables will result in an error. Additionally, `set -o errexit` is equivalent to `set -e`, causing the script to exit immediately if any command fails.






#### `if else`
- Conditional statements in Unix/Linux are implemented using the `if else` construct.
- Example:
  ```bash
  if [expression]; then
      command1
      command2
  else
      command3
  fi
  ```

### Advanced Usage

#### `curl` and `grep`
- Combining `curl` and `grep` can be useful to filter specific content from web responses.
- Example:
  ```bash
  curl https://example.com | grep "Error"
  ```


![curl and grep command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aktu3srfq7ts0d95kxsp.png)




#### Pipeline Usage
- Unix/Linux commands can be chained together using pipes (`|`) for more complex operations.
- Example:
  ```bash
  ./tst.sh | grep "1"
  ```

### Conclusion
This documentation provides a brief overview and examples of common Unix/Linux commands, special constructs, and their usage in various scenarios. Experimenting with these commands and constructs will help users become proficient in Unix/Linux system administration and scripting.