{"id":22206264,"url":"https://github.com/miusarname2/bash_scriptformanagementandoptimisation","last_synced_at":"2026-04-29T08:02:25.672Z","repository":{"id":265121180,"uuid":"895142601","full_name":"miusarname2/Bash_scriptForManagementAndOptimisation","owner":"miusarname2","description":"🚀 Welcome to my Linux adventure! 🐧   This repo is my Bash lab: from monitoring CPU and memory to automating temporary file cleanups, all part of my vacation learning path to master system administration. All part of my holiday learning path to mastering system administration - join and explore with me! 🌟","archived":false,"fork":false,"pushed_at":"2024-11-27T18:38:31.000Z","size":5,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-25T03:36:00.793Z","etag":null,"topics":["bash","bash-script","learning","shell","shell-scripting"],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/miusarname2.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-27T16:27:02.000Z","updated_at":"2024-11-27T18:38:35.000Z","dependencies_parsed_at":"2024-11-27T22:35:48.879Z","dependency_job_id":null,"html_url":"https://github.com/miusarname2/Bash_scriptForManagementAndOptimisation","commit_stats":null,"previous_names":["miusarname2/bash_scriptformanagementandoptimisation"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/miusarname2/Bash_scriptForManagementAndOptimisation","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miusarname2%2FBash_scriptForManagementAndOptimisation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miusarname2%2FBash_scriptForManagementAndOptimisation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miusarname2%2FBash_scriptForManagementAndOptimisation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miusarname2%2FBash_scriptForManagementAndOptimisation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/miusarname2","download_url":"https://codeload.github.com/miusarname2/Bash_scriptForManagementAndOptimisation/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miusarname2%2FBash_scriptForManagementAndOptimisation/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32416146,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-29T06:29:02.080Z","status":"ssl_error","status_checked_at":"2026-04-29T06:29:00.631Z","response_time":110,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bash","bash-script","learning","shell","shell-scripting"],"created_at":"2024-12-02T18:10:26.614Z","updated_at":"2026-04-29T08:02:25.640Z","avatar_url":"https://github.com/miusarname2.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"## How to get started with Bash scripting\n\n## Running Bash commands from the command line\n\nAs mentioned above, the shell prompt looks like this:\n\n```\n[username@host ~]$\n```\n\nYou can enter any command after the `$` sign and see the result in the terminal.\n\nGenerally, commands follow this syntax:\n\n```\ncommand [OPTIONS] arguments\n```\n\nLet's look at some basic bash commands and see their results. Don't forget to follow the instructions! :)\n\n### Basic commands:\n\n- **`date`**: Displays the current date.\n\n    ````bash\n    zaira@Zaira:~/shell-tutorial$ date\n    Tue Mar 14 13:08:57 PKT 2023\n    ```\n\n- **`pwd`**: Show current working directory\n\n    ````bash\n    zaira@Zaira:~/shell-tutorial$ pwd\n    /home/zaira/shell-tutorial\n    ```\n\n- **``ls`**: List the contents of the current directory\n\n    ````bash\n    zaira@Zaira:~/shell-tutorial$ ls\n    check_plaindrome.sh count_odd.sh env log temp\n    ```\n\n- **`echo`**: Print a string or the value of a variable to the terminal\n\n    ````bash\n    zaira@Zaira@Zaira:~/shell-tutorial$ echo ‘Hello bash’\n    Hello bash\n    ```\n\nYou can always consult a command manual with the `man` command.\n\nFor example, the `ls` manual looks like this:\n\n\u003e Image\n\nYou can see the options for a command in detail using `man`.\n## How to Create and Run Bash Scripts\n\n### Script Naming Conventions\n\nBy naming convention, Bash scripts typically end with `.sh`. However, Bash scripts can run perfectly fine without the `.sh` extension.\n\n### Adding the Shebang\n\nBash scripts begin with a shebang. The shebang is a combination of the `#` symbol and the `!` symbol, followed by the path to the Bash shell. This is the first line of the script. The shebang instructs the shell to execute the script using the Bash shell. It is simply an absolute path to the Bash interpreter.\n\nBelow is an example of a shebang declaration:\n\n```bash\n#!/bin/bash\n```\n\nYou can find the path to your Bash shell (which may differ from the above) using the following command:\n\n```bash\nwhich bash\n```\n\n### How to Execute a Bash Script\n# Using `chmod` and Running Scripts\n\n- **`chmod`**: Modifies the file's permissions for the current user: `u`.\n- **`+x`**: Grants execute permissions to the current user. This means the file owner can now run the script.\n\n### Example:\n`run_all.sh` is the file we want to execute.\n\nYou can execute the script using any of the following methods:\n\n```bash\nsh run_all.sh\n```\n\n```bash\nbash run_all.sh\n```\n\n```bash\n./run_all.sh\n```\n## Variables and Data Types in Bash\n\nVariables allow you to store data. You can use variables to read, access, and manipulate data throughout a script.\n\nIn Bash, there are no strict data types. A variable can store numeric values, single characters, or strings.\n\n### Using Variables in Bash\n\nYou can assign and use variable values in the following ways:\n\n- **Assigning a value directly**:\n\n    ```bash\n    country=Pakistan\n    ```\n\n- **Assigning a value based on the output of a program or command** using command substitution. Note that `$` is required to access the value of an existing variable:\n\n    ```bash\n    same_country=$country\n    ```\n\n### Accessing the Value of a Variable\n\nTo access the value of a variable, prepend `$` to the variable name:\n\n```bash\nzaira@Zaira:~$ country=Pakistan\nzaira@Zaira:~$ echo $country\nPakistan\nzaira@Zaira:~$ new_country=$country\nzaira@Zaira:~$ echo $new_country\nPakistan\n```\n\n# Input and Output in Bash Scripts\n\n## Gathering Information\n\nThis section discusses methods to provide input to your scripts.\n\n### Reading User Input and Storing it in a Variable\n\nYou can read user input using the `read` command.\n\n```bash\n#!/bin/bash\n\necho \"What's your name?\"\n\nread entered_name\n\necho -e \"\\nWelcome to the Bash tutorial, $entered_name\"\n```\n\n\u003e Image\n\n### Reading from a File\n\nThis code reads each line from a file called `input.txt` and prints it to the terminal. We'll explore `while` loops in more detail later in this article.\n\n```bash\nwhile read line\ndo\n  echo $line\ndone \u003c input.txt\n```\n\n### Command-Line Arguments\n\nIn a Bash script or function, `$1` denotes the first argument passed, `$2` denotes the second argument, and so on.\n\nThis script takes a name as a command-line argument and prints a personalised greeting:\n\n```bash\necho \"Hello, $1!\"\n```\n\nFor example, we supply `Zaira` as an argument to the script:\n\n```bash\n#!/bin/bash\necho \"Hello, $1!\"\n```\n\n# Displaying Output\n\nHere are some methods for receiving output from scripts.\n\n### Printing to the Terminal\n\n```bash\necho \"Hello, World!\"\n```\n\nThis prints the text \"Hello, World!\" to the terminal.\n\n### Writing to a File\n\n```bash\necho \"This is some text.\" \u003e output.txt\n```\n\nThis writes the text \"This is some text.\" to a file named `output.txt`. Note that the `\u003e` operator overwrites a file if it already has content.\n\n### Appending to a File\n\n```bash\necho \"More text.\" \u003e\u003e output.txt\n```\n\nThis appends the text \"More text.\" to the end of the `output.txt` file.\n\n### Redirecting Output\n\n```bash\nls \u003e files.txt\n```\n\nThis lists files in the current directory and writes the output to a file called `files.txt`. You can redirect the output of any command to a file this way.\n\n## Basic Bash Commands (`echo`, `read`, etc.)\n\nBelow is a list of commonly used Bash commands:\n\n- **`cd`**: Changes the directory.\n- **`ls`**: Lists the contents of the current directory.\n- **`mkdir`**: Creates a new directory.\n- **`touch`**: Creates a new file.\n- **`rm`**: Deletes a file or directory.\n- **`cp`**: Copies a file or directory.\n- **`mv`**: Moves or renames a file or directory.\n- **`echo`**: Prints text to the terminal.\n- **`cat`**: Concatenates and displays the contents of a file.\n- **`grep`**: Searches for a pattern in a file.\n- **`chmod`**: Changes file or directory permissions.\n- **`sudo`**: Runs a command with administrative privileges.\n- **`df`**: Shows available disk space.\n- **`history`**: Displays a list of previously executed commands.\n- **`ps`**: Shows information about running processes.\n\n# Conditional Statements (`if/else`)\n\nExpressions that produce a boolean result, either true or false, are called conditions. Various forms of conditionals include `if`, `if-else`, `if-elif-else`, and nested conditionals.\n\n### Syntax\n\n```bash\nif [[ condition ]];\nthen\n    statement\nelif [[ condition ]]; then\n    statement\nelse\n    do this by default\nfi\n```\n\nLogical operators such as `-a` (AND) and `-o` (OR) can be used for more complex comparisons.\n\n```bash\nif [ $a -gt 60 -a $b -lt 100 ]\n```\n\n### Example: Using `if`, `if-else`, and `if-elif-else`\n\nHere's a Bash script that determines if a number entered by the user is positive, negative, or zero:\n\n```bash\n#!/bin/bash\n\necho \"Please enter a number: \"\nread num\n\nif [ $num -gt 0 ]; then\n  echo \"$num is positive\"\nelif [ $num -lt 0 ]; then\n  echo \"$num is negative\"\nelse\n  echo \"$num is zero\"\nfi\n```\n\n### See it in Action 🚀\n\n# Loops and Branching in Bash\n\n## While Loop\n\n`while` loops check if a condition exists and repeat the loop as long as the condition is `true`. A counter statement is required to control the loop's execution.\n\nIn the following example, `(( i += 1 ))` increments the value of `i`. The loop will execute exactly 10 times.\n\n```bash\n#!/bin/bash\ni=1\nwhile [[ $i -le 10 ]] ; do\n   echo \"$i\"\n   (( i += 1 ))\ndone\n```\n\n## For Loop\n\nThe `for` loop, like the `while` loop, allows statements to execute a specific number of times. The syntax and usage differ.\n\nIn this example, the loop iterates 5 times.\n\n```bash\n#!/bin/bash\nfor i in {1..5}\ndo\n    echo $i\ndone\n```\n\n## Case Statements\n\nIn Bash, `case` statements are used to compare a given value against a list of patterns and execute a block of code based on the first matching pattern. The syntax for a `case` statement in Bash is as follows:\n\n```bash\ncase expression in\n    pattern1)\n        # code to execute if expression matches pattern1\n        ;;\n    pattern2)\n        # code to execute if expression matches pattern2\n        ;;\n    pattern3)\n        # code to execute if expression matches pattern3\n        ;;\n    *)\n        # code to execute if none of the above patterns match expression\n        ;;\nesac\n```\n\n### Example\n\n```bash\nfruit=\"apple\"\n\ncase $fruit in\n    \"apple\")\n        echo \"This is a red fruit.\"\n        ;;\n    \"banana\")\n        echo \"This is a yellow fruit.\"\n        ;;\n    \"orange\")\n        echo \"This is an orange fruit.\"\n        ;;\n    *)\n        echo \"Unknown fruit.\"\n        ;;\nesac\n```\n\nIn this example, since the value of `fruit` is `\"apple\"`, the first pattern matches, and the block of code prints \"This is a red fruit.\" If the value of `fruit` were `\"banana\"`, the second pattern would match, printing \"This is a yellow fruit,\" and so on. If `fruit` does not match any specified pattern, the default case executes, printing \"Unknown fruit.\"\n\n# Activity 1\n\nCreate a script to monitor CPU and memory usage, sending alerts if they exceed a defined threshold.\n\n# Activity 2\n\nAutomate the cleanup of temporary files older than seven days.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiusarname2%2Fbash_scriptformanagementandoptimisation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmiusarname2%2Fbash_scriptformanagementandoptimisation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiusarname2%2Fbash_scriptformanagementandoptimisation/lists"}