Unit-2
## BASH SCRIPTING
## Definition
- **Bash (Bourne Again Shell)**: A command processor that typically runs in a text window, allowing users to type commands that trigger actions.
## Purpose
- **Automation**: Bash scripting automates various system tasks, minimizing repetitive efforts for developers.
- **Programming Features**: Supports variables, conditional statements, and loops similar to programming languages, enhancing functionality.
## Key Features of Bash Shell Scripting
- **Interactivity**: Users can interactively command the system via a command-line environment.
- **Scripting Capabilities**: Allows writing scripts that combine multiple commands for task automation.
- **Variables**: Facilitate data storage and manipulation.
- **Control Structures**: Enable implementation of conditional statements and loops.
- **Functions**: Provide code encapsulation for reusability.
## Advantages and Disadvantages of Bash Shell
### Advantages
- **Simplicity**: User-friendly for script development.
- **Task Automation**: Reduces manual repetition significantly.
- **Single Command Execution**: Run a sequence of commands as a single command.
### Disadvantages
- **Error Sensitivity**: Mistakes in script writing can lead to costly errors.
- **Performance**: Slower execution speed compared to compiled languages.
- **Compatibility Issues**: Potential problems across different platforms.
## How to Write Bash Scripts
1. **File Creation**: Create a file with a `.sh` extension.
2. **Write Scripts**: Input the required Bash scripts into the file.
3. **Execution Permission**: Provide execution permission to the script file.
4. **Execution**: Execute the file to run the script.
## General Purpose Commands
### Table of Commands
| S. No | Command | Description | Example |
|-------|---------|-------------|---------|
| 1 | `ls` | List files and directories | `ls` |
| 2 | `cd` | Change working directory | `cd /path/to/directory` |
| 3 | `pwd` | Print current directory | `pwd` |
| 4 | `cp` | Copy files or directories | `cp file1.txt /path/to/destination` |
| 5 | `mv` | Move or rename files | `mv file1.txt newfile.txt` |
| 6 | `rm` | Remove files | `rm file.txt` |
| 7 | `mkdir` | Create a new directory | `mkdir new_directory` |
| 8 | `rmdir` | Remove an empty directory | `rmdir empty_directory` |
| 9 | `touch` | Create/update an empty file | `touch new_file.txt` |
| 10 | `cat` | Display file contents | `cat file.txt` |
| 11 | `echo` | Display a message | `echo "Hello, World!"` |
| 12 | `grep` | Search for a pattern in a file | `grep "pattern" file.txt` |
| 13 | `find` | Search for files and directories | `find /path/to/search -name "*.txt"` |
| 14 | `chmod` | Change file/directory permissions | `chmod +x script.sh` |
| 15 | `chown` | Change file/directory ownership | `chown user:group file.txt` |
| 16 | `ps` | Display running processes | `ps aux` |
| 17 | `kill` | Terminate a process | `kill PID` |
| 18 | `df` | Disk space usage information | `df -h` |
| 19 | `du` | Display disk usage | `du -h` |
| 20 | `man` | Display command manual | `man ls` |
| 21 | `who` | List current users | `who` |
| 22 | `cal` | Display calendar | `cal` |
| 23 | `clear` | Clear terminal screen | `clear` |
| 24 | `history` | List previous commands | `history` |
## Quiz (General Purpose Commands)
1. Which command is used to display the current directory in Linux?
- a) `ls`
- b) `cd`
- c) `pwd`
- d) `dir`
2. What command is used to list the contents of a directory in Linux?
- a) `ls`
- b) `dir`
- c) `list`
- d) `show`
3. To create a new directory in Linux, you use:
- a) `mkdir`
- b) `newdir`
- c) `create`
- d) `touch`
4. What command is used to copy files or directories in Linux?
- a) `mv`
- b) `copy`
- c) `cp`
- d) `xcopy`
5. To remove a file in Linux, you use:
- a) `delete`
- b) `rm`
- c) `del`
- d) `erase`
## Redirection Operators
### Overview of Redirection Operators
| S. No | Command | Description | Example |
|-------|---------|-------------|---------|
| 1 | `>` | Output Redirection | `command > output.txt` |
| 2 | `>>` | Append Output | `command >> output.txt` |
| 3 | `