90 Days of journey with trainwithshubham shell scripting command make us master in devops journey

90 Days of journey with trainwithshubham shell scripting command make us master in devops journey

Shell scripting is a powerful way to automate tasks on Unix-like operating systems such as Linux. Here are some common shell script commands:

  1. Shebang: Indicates the interpreter to be used for executing the script.

      #!/bin/bash
    
  2. Comments: Used for adding comments in the script for documentation.

      # This is a comment
    
  3. Variables: Used to store data.

      variable_name="value"
    
  4. Echo: Prints text or variables to the terminal.

      echo "Hello, world!"
      echo "The value of variable is: $variable_name"
    
  5. Read: Reads input from the user.

      read -p "Enter your name: " name
    
  6. If Statement: Conditional execution based on a condition.

    COPY

      if [ condition ]; then
          # code block
      fi
    
  7. For Loop: Executes a block of code a fixed number of times.

    COPY

    COPY

      for i in {1..2}; do
          # code block
      done
    
  8. While Loop: Executes a block of code repeatedly as long as a condition is true.

      while [ condition ]; do
          # code block
      done
    
  9. Functions: Blocks of reusable code.

       codemy_function() {
          # code block
      }
    
  10. Command Substitution: Executes a command and stores the output in a variable.

    result=$(command)
    
  11. Conditional Execution: Execute commands based on the success or failure of previous commands.

    command1 && command2   # command2 runs if command1 succeeds
    command1 || command2   # command2 runs if command1 fails
    
  12. File Operations:

    • Create a file: touch filename

    • Remove a file: rm filename

    • Check if a file exists: if [ -f filename ]; then ...

    • Check if a directory exists: if [ -d directoryname ]; then ...

  13. Directory Navigation:

    • Change directory: cd directoryname

    • List files and directories: ls

    • Make directory: mkdir directoryname

    • Remove directory: rmdir directoryname

  14. String Manipulation:

    • Substring: Extracts a portion of a string.

          substring=${string:position:length}
      
    • Concatenation: Combines two or more strings.

          concatenated="$string1$string2"
      
  15. File Permissions:

    • Change Permissions: chmod command.

          chmod permissions filename
      
    • View Permissions: ls -l command.

  16. Input/Output Redirection:

    • Redirect Output: Sends the output of a command to a file.

          command > output_file
      
    • Redirect Input: Provides input to a command from a file.

          command < input_file
      
  17. Piping:

    • Pipe (|): Sends the output of one command as input to another command.

          command1 | command2
      
  18. Advanced Control Structures:

    • Case Statement: Executes different blocks of code based on matching patterns.

          case expression in
              pattern1)
                  # code block
                  ;;
              pattern2)
                  # code block
                  ;;
              *)
                  # default code block
                  ;;
          esac
      
  19. Debugging:

    • Print Debugging: Inserting echo statements for debugging purposes.

    • Debugging Flags: Using set -x to enable debugging mode.

          #!/bin/bash
          set -x
          # script content
      
  20. Functions:

    • Passing Arguments: Functions can accept arguments.

           codemy_function() {
              arg1=$1
              arg2=$2
              # code block
          }
          my_function value1 value2
      
  21. Exit Codes:

    • Exit Status: Every command returns an exit status.

    • Checking Exit Codes: $? variable holds the exit status of the last command.

          command
          if [ $? -eq 0 ]; then
              echo "Command successful"
          else
              echo "Command failed"
          fi
      
  22. Hshsh Operations:

    • Usingexpr: expr command for basic arithmetic.

          sum=$(expr $num1 + $num2)
      

These command make you expert in Linux as well Devops journey