Skip to main content

Command Palette

Search for a command to run...

DevOps Journey Week 2 : Shell Scripting

Published
4 min read
DevOps Journey Week 2 : Shell Scripting
M

I'm experienced in designing scalable software architecture on a higher level and lower level implementation. Ensures the smooth functioning of technical operations by monitoring and evaluating staff progress. Involved in the training and recruitment process, works for company goals, and ensures overall client satisfaction.

I’ve been diving into the essentials of Linux and Shell Scripting as part of my DevOps journey. Here’s a summary of what I’ve learned so far.

Operating Systems (OS)

  • Role: The OS is like the bridge between the software and hardware. It manages:

    • Device Management: Connects hardware components with software.

    • Memory Management: Keeps track of memory allocation and usage.

    • Process Management: Handles running processes and multitasking.

    • System Handling: Ensures the overall system stability and efficiency.

Linux

  • Why Linux? It’s a powerful, open-source OS widely used in production.

  • Kernel: The core of Linux that manages devices, memory, processes, and system resources.

  • Popular Distros:

    • Ubuntu: User-friendly and widely used in both dev and production environments.

    • CentOS: Known for its stability and enterprise features.

    • Others: Fedora, Redhat, Debian, Kali Linux.

Shell Scripting

Purpose: Automates tasks and makes system management easier.

Key Commands:

File and Directory Management:

  • pwd - Shows the current working directory.

      pwd
      # Output: /home/user
    
  • ls - Lists files and directories.

      ls
      # Output: file1.txt  file2.txt  directory1
    
  • ls -ltr - Lists files with permissions and timestamps.

      ls -ltr
      # Output:
      # -rw-r--r-- 1 user group 1024 Aug 24 10:00 file1.txt
      # drwxr-xr-x 2 user group 4096 Aug 24 09:45 directory1
    
  • mkdir - Creates a new directory.

      mkdir new_directory
    
  • touch - Creates a new file.

      touch newfile.txt
    

File Editing and Viewing:

  • vim - Opens a file for editing.

      vim filename.txt
    
    • Commands:

      • esc + i (insert mode)

      • esc + :wq! (save and exit)

      • esc + :q! (exit without saving)

  • cat - Displays file content.

      cat filename.txt
    

Permissions and Removal:

  • chmod - Changes file permissions.

      chmod 666 file1.txt
      chmod 444 file1.txt
      chmod 777 file1.txt
    
  • rm - Deletes files or directories.

      rm file1.txt
      rm -rf directory1
    

System Monitoring:

  • nproc - Shows the number of CPUs.

      nproc
      # Output: 4
    
  • free - Displays RAM usage.

      free -h
      # Output: 
      #              total        used        free      shared  buff/cache   available
      # Mem:          7.7G        2.4G        3.2G        310M        2.0G        4.7G
    
  • top - Lists running processes.

      top
      # Displays a dynamic view of running processes
    
  • ps -ef - Shows detailed process information.

      ps -ef
      # Output:
      # UID        PID  PPID  C STIME TTY          TIME CMD
      # root         1     0  0 Aug23 ?        00:00:02 /sbin/init
    

Data Handling and Networking:

  • grep - Filters output by keywords.

      ps -ef | grep ssh
      # Output: Lists processes related to SSH
    
  • curl - Fetches data from the web.

      curl http://example.com
    
  • wget - Downloads files from the web.

      wget http://example.com/file.zip
    
  • find - Locates files or directories.

      find /home/user -name filename.txt
    
  • su - Switches user accounts.

      su - username
    
  • awk - Processes and filters text.

      ps -ef | awk '{print $1, $2}'
      # Output: Displays the user and process ID of each running process
    

Scripting Practices

Debugging and Error Handling:

  • set -x - Turns on debug mode to show command execution.

      set -x
    
  • set -e - Exits the script if any command fails.

      set -e
    
  • set -o pipefail - Exits the script if any command in a pipeline fails.

      set -o pipefail
    

Control Structures:

  • Conditionals: Use if-else statements for decision-making.

      if [ $a -gt $b ]
      then
          echo "a is greater than b"
      else
          echo "b is greater than a"
      fi
    
  • Loops: Use for loops for iteration.

      for i in {1..5}; do
          echo $i
      done
    

Pipes and Redirection:

  • Use | to chain commands, passing output from one as input to another.

      ps -ef | grep mysql | awk '{print $2}'
    

#!/bin/bash vs #!/bin/sh:

  • #!/bin/bash is used for Bash scripts. Bash offers more features and is commonly used for scripting.

  • #!/bin/sh is used for POSIX-compliant scripts. It may be linked to different shells like Dash, which has different syntax and capabilities.

Executing Scripts:

  • Run scripts using bash script_name.sh or ./script_name.sh (make sure the script has execute permissions with chmod +x script_name.sh).

      bash script.sh
      ./script.sh
    

Permissions:

  • Use chmod to set permissions for files and directories:

      chmod 777 file
      chmod 644 file
    

Refer to my github repo for shell scripts https://github.com/manjot-baj/DevOps/tree/main/Shell%20Scripting.