DEV Community

Cover image for Advance Shell Scripting (1-100)πŸš€
Shubham Srivastava
Shubham Srivastava

Posted on

Advance Shell Scripting (1-100)πŸš€

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:
  df
Enter fullscreen mode Exit fullscreen mode

df command

nproc

  • The nproc command prints the number of processing units available to the current process.
  • Example:
  nproc
Enter fullscreen mode Exit fullscreen mode

nproc command

top

  • The top command displays real-time information about running processes, system load, and CPU usage.
  • Example:
  top
Enter fullscreen mode Exit fullscreen mode

top command

ps

  • The ps command is used to display information about processes.
  • Example:
  ps -ef
Enter fullscreen mode Exit fullscreen mode

ps command

grep

  • The grep command is used to search for specific patterns within files or output.
  • Example:
  grep "amazon" /path/to/file
Enter fullscreen mode Exit fullscreen mode

grep command

curl

  • The curl command is used to transfer data to or from a server.
  • Example:
  curl https://example.com
Enter fullscreen mode Exit fullscreen mode

find

  • The find command is used to search for files or directories in a directory hierarchy.
  • Example:
  find / -name "filename"
Enter fullscreen mode Exit fullscreen mode

Special Commands and Constructs

sudo

  • The sudo command is used to execute commands with elevated privileges.
  • Example:
  sudo find / -name "filename"
Enter fullscreen mode Exit fullscreen mode

set

  • The set command is used to set shell options.
  • Example:
  set -e # exit the script when error
  set -o # debug mode
  set -x # debug mode
Enter fullscreen mode Exit fullscreen mode

shell Script :

set command

set command

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

Example 1: set -e (Exit on Error)

#!/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"
Enter fullscreen mode Exit fullscreen mode

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)

#!/bin/bash

# Enable debug mode
set -x

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

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)

#!/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"
Enter fullscreen mode Exit fullscreen mode

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:
  if [expression]; then
      command1
      command2
  else
      command3
  fi
Enter fullscreen mode Exit fullscreen mode

Advanced Usage

curl and grep

  • Combining curl and grep can be useful to filter specific content from web responses.
  • Example:
  curl https://example.com | grep "Error"
Enter fullscreen mode Exit fullscreen mode

curl and grep command

Pipeline Usage

  • Unix/Linux commands can be chained together using pipes (|) for more complex operations.
  • Example:
  ./tst.sh | grep "1"
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)