# Comprehensive Guide to Shell Scripting (0-1)🚀
Tags: devops, linux, programming, beginners
Published: 2024-01-21 20:10:29



## 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](#creating-and-viewing-files)
2. [Understanding Commands](#understanding-commands)
3. [Executing Shell Scripts](#executing-shell-scripts)
4. [Managing Permissions](#managing-permissions)
5. [Useful Commands](#useful-commands)
6. [Example: Basic Shell Script](#example-basic-shell-script)
7. [Shell Scripting Roles](#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

```bash
#!/bin/bash

# Creating a folder
mkdir user 

# Creating 2 files
cd user 
touch firstfile secondfile
```

## 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:**
   ```bash
   user@hostname:~/path/to/directory$ ls
   ```

2. **Create and open a shell script file named `shellscript.sh`:**
   ```bash
   user@hostname:~/path/to/directory$ vim shellscript.sh
   ```

3. **List files again to confirm the creation of the script:**
   ```bash
   user@hostname:~/path/to/directory$ ls
   shellscript.sh  otherfile
   ```

4. **View the contents of the shell script:**
   ```bash
   user@hostname:~/path/to/directory$ cat shellscript.sh
   #!/bin/bash

   # Create a folder
   mkdir user 

   # Create 2 files
   cd user 
   touch firstfile secondfile
   ```

5. **Attempt to execute the script (permission denied):**
   ```bash
   user@hostname:~/path/to/directory$ ./shellscript.sh
   bash: ./shellscript.sh: Permission denied
   ```

6. **Grant execute permission to the script:**
   ```bash
   user@hostname:~/path/to/directory$ chmod 777 shellscript.sh
   ```

7. **Execute the script successfully:**
   ```bash
   user@hostname:~/path/to/directory$ ./shellscript.sh
   ```

8. **List files to verify changes:**
   ```bash
   user@hostname:~/path/to/directory$ ls
   shellscript.sh  user  otherfile
   ```

9. **Navigate to the 'user' directory:**
   ```bash
   user@hostname:~/path/to/directory$ cd user
   ```

10. **List files in the 'user' directory:**
    ```bash
    user@hostname:~/path/to/directory/user$ ls
    firstfile  secondfile
    ```

11. **Attempt to execute the script inside the 'user' directory (file not found):**
    ```bash
    user@hostname:~/path/to/directory/user$ ./shellscript.sh
    bash: ./shellscript.sh: No such file or directory
    ```

12. **Navigate back to the parent directory:**
    ```bash
    user@hostname:~/path/to/directory/user$ cd ..
    ```

13. **Attempt to execute the script again (folder already exists):**
    ```bash
    user@hostname:~/path/to/directory$ ./shellscript.sh
    mkdir: cannot create directory ‘user’: File exists
    ```

[Advance shell scripting ➡️](https://dev.to/surajvast/advance-shell-script-1-100-5756)