DEV Community

Cover image for # Comprehensive Guide to Shell Scripting (0-1)πŸš€
Shubham Srivastava
Shubham Srivastava

Posted on • Edited on

# Comprehensive Guide to Shell Scripting (0-1)πŸš€

Introduction

Welcome to the exciting world of shell scripting! This comprehensive guide will take you from the fundamentals to advanced concepts, helping you automate tasks on Linux environments using shell scripts. 🐚

Table of Contents

  1. Creating and Viewing Files
  2. Understanding Commands
  3. Executing Shell Scripts
  4. Managing Permissions
  5. Useful Commands
  6. Example: Basic Shell Script
  7. Shell Scripting Roles

Creating and Viewing Files

  • To create a file, use the touch command: touch filename.
  • Shell scripts typically have the extension .sh.
  • Use ls to list files and directories.
  • ls -ltr sorts files by modification time.

Understanding Commands

  • Use man <command> to get a manual about a command.
  • Editing files: Use vi to open a file, i to insert, and :wq! to save and exit.
  • The first line in a shell script should contain the shebang (#!/bin/bash).

Executing Shell Scripts

  • To execute a shell script: sh filename.sh or ./filename.sh.
  • Permissions: Use chmod to grant permissions. Example: chmod 777 filename.sh.
  • Permissions include read (4), write (2), and execute (1).

Managing Permissions

  • chmod 444 filename gives read access only.
  • Security: Use chmod with root, group, and user access.

Useful Commands

  • history: View executed commands.
  • pwd: Know the current directory.
  • mkdir: Create a directory.
  • cd: Change directory.

Example: Basic Shell Script

#!/bin/bash

# Creating a folder
mkdir user 

# Creating 2 files
cd user 
touch firstfile secondfile
Enter fullscreen mode Exit fullscreen mode

Shell Scripting Roles

Shell scripting serves three main roles:

  1. Infrastructure Management
  2. Code Management
  3. Configuration Management

User-Friendly Example

Now, let's walk through an example using the username "user":

  1. List files in the current directory:
   user@hostname:~/path/to/directory$ ls
Enter fullscreen mode Exit fullscreen mode
  1. Create and open a shell script file named shellscript.sh:
   user@hostname:~/path/to/directory$ vim shellscript.sh
Enter fullscreen mode Exit fullscreen mode
  1. List files again to confirm the creation of the script:
   user@hostname:~/path/to/directory$ ls
   shellscript.sh  otherfile
Enter fullscreen mode Exit fullscreen mode
  1. View the contents of the shell script:
   user@hostname:~/path/to/directory$ cat shellscript.sh
   #!/bin/bash

   # Create a folder
   mkdir user 

   # Create 2 files
   cd user 
   touch firstfile secondfile
Enter fullscreen mode Exit fullscreen mode
  1. Attempt to execute the script (permission denied):
   user@hostname:~/path/to/directory$ ./shellscript.sh
   bash: ./shellscript.sh: Permission denied
Enter fullscreen mode Exit fullscreen mode
  1. Grant execute permission to the script:
   user@hostname:~/path/to/directory$ chmod 777 shellscript.sh
Enter fullscreen mode Exit fullscreen mode
  1. Execute the script successfully:
   user@hostname:~/path/to/directory$ ./shellscript.sh
Enter fullscreen mode Exit fullscreen mode
  1. List files to verify changes:
   user@hostname:~/path/to/directory$ ls
   shellscript.sh  user  otherfile
Enter fullscreen mode Exit fullscreen mode
  1. Navigate to the 'user' directory:
   user@hostname:~/path/to/directory$ cd user
Enter fullscreen mode Exit fullscreen mode
  1. List files in the 'user' directory:

    user@hostname:~/path/to/directory/user$ ls
    firstfile  secondfile
    
  2. Attempt to execute the script inside the 'user' directory (file not found):

    user@hostname:~/path/to/directory/user$ ./shellscript.sh
    bash: ./shellscript.sh: No such file or directory
    
  3. Navigate back to the parent directory:

    user@hostname:~/path/to/directory/user$ cd ..
    
  4. Attempt to execute the script again (folder already exists):

    user@hostname:~/path/to/directory$ ./shellscript.sh
    mkdir: cannot create directory β€˜user’: File exists
    

Advance shell scripting ➑️

Top comments (0)