> For the complete documentation index, see [llms.txt](https://doane-ccla.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://doane-ccla.gitbook.io/docs/learning-linux/bash-scripting.md).

# Bash Scripting

## Considerations

* It is crucial that the first line of the Bash script begins with the header `#!/bin/bash`.
* The extension of a file which represents a script should be `.sh`.
* Comments in Bash begin with `#` and run to the end of the line:

  ```bash
  echo Hello, World. # prints out "Hello, World."
  ```
* To execute a script, you must be sure your file has the permission to be executed `chmod +x your_script.sh`. Click [here](/docs/learning-linux/file-permissions.md) to see more about permissions.
* If you are located in the same PATH were you created the script, you execute your script by running `./your_script.sh`.

## Conditionals (Decision Control Structure)

Use conditionals to specify different courses of action to be taken. In this case, we have three possibilities to check a number in a range of other values:

```bash
#!/bin/bash
#Setting a value to output
output=99
#Determine an action based on the output's value
if [ $output -eq 100 ]   #if the output is equal to 100
then
  echo "The calculation reaches 100%"
else
  if [ $output -gt 100 ]   #if the output is greater than 100
  then
    echo "The calculation is greater than 100%"
  else     #only option is that the output is less than 100
  echo "The calculation is less than 100%"
  fi
```

Read more examples about [if conditionals](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html).

## Loops (Repetitive tasks)

In a loop, commands will continue to run repeatedly until a task is executed for all elements. One useful command for loop calculations is `for`. Here is an example of printing numbers from 1 to 9:

```bash
#!/bin/bash
#Printing numbers from 1 to 9
for i in {1..9}; do echo $i; done
```

If you want to have all the numbers in the same line, add the `-n` option. If you want to add 4 units, use double parentheses in the operation:

```bash
#!/bin/bash
#Numbers from 1 to 9
#Add four units in the same line
for i in {1..9}; do echo -n "$((i+4))  " ;  done
```

* Click [here](https://www.tldp.org/LDP/abs/html/loops1.html) to see more examples.

## Working with files (combining conditionals with Bash commands)

Check the existence of a file to determine the size of the file as well the quantity of words:

```bash
#!/bin/bash
#Clear the terminal
tput clear

#Request the name of the file to be evaluated
printf "Enter the absolute path of the file, e.g. /home/user/your_file\n"
read FILE

#Evaluate the file
if [ -e $FILE ]
then
    printf "The $FILE has a size of $(du -h $FILE | awk '{print $1}') and it contains $(wc -w $FILE | awk '{print $1}') words.\n"
else
    printf "File not found, keep trying..."
fi
```

📝 It is important to indent the code in the `if` block with 4 spaces. Also include 1 space between the *contents* of the brackets (`[` and `]`) and the brackets themselves.

## Working with filesystems (combining conditionals with Bash commands)

Send an email if disk usage in the system has reached 90% or more:

```bash
#!/bin/bash
#Run 'df -H' first to check filesystems and usage
#Then filter with `grep` to not consider Filesystem, tmpfs, nor cdrom using the options `-vE`
#Take only columns 5 and 1 with 'awk' and keep that output using `while read` to do an action
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
do
#Print the output with 'echo', and assign to `usep` only the first column of output, taking out '%'
  echo $output
  usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
#Partition is only going to take the names of your filesystems
  partition=$(echo $output | awk '{ print $2 }' )
#if the value of `usep` is greater than or equal to 90 then you will print a message and send an email to alert
  if [ $usep -ge 90 ]; then
    echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" |
     mail -s "Alert: Almost out of disk space $usep%" username@example.com
  fi
done
```

*You can learn more about Bash scripting by taking a look at this* [*tutorial*](http://www.tldp.org/LDP/abs/html/)*.*


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://doane-ccla.gitbook.io/docs/learning-linux/bash-scripting.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
