reworking content
All checks were successful
learn org at code.softwareshinobi.com/linux.softwareshinobi.com/pipeline/head This commit looks good

This commit is contained in:
2025-06-19 10:03:08 -04:00
parent 611d0816cc
commit 7d9171c854
192 changed files with 2234 additions and 2362 deletions

View File

@@ -1,32 +0,0 @@
# Bash Structure
Let's start by creating a new file with a `.sh` extension. As an example, we could create a file called `devdojo.sh`.
To create that file, you can use the `touch` command:
```bash
touch devdojo.sh
```
Or you can use your text editor instead:
```bash
nano devdojo.sh
```
In order to execute/run a bash script file with the bash shell interpreter, the first line of a script file must indicate the absolute path to the bash executable:
```bash
#!/bin/bash
```
This is also called a [Shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)).
All that the shebang does is to instruct the operating system to run the script with the `/bin/bash` executable.
However, bash is not always in `/bin/bash` directory, particularly on non-Linux systems or due to installation as an optional package. Thus, you may want to use:
```bash
#!/usr/bin/env bash
```
It searches for bash executable in directories, listed in PATH environmental variable.

View File

@@ -1,41 +0,0 @@
# Bash Hello World
Once we have our `devdojo.sh` file created and we've specified the bash shebang on the very first line, we are ready to create our first `Hello World` bash script.
To do that, open the `devdojo.sh` file again and add the following after the `#!/bin/bash` line:
```bash
#!/bin/bash
echo "Hello World!"
```
Save the file and exit.
After that make the script executable by running:
```bash
chmod +x devdojo.sh
```
After that execute the file:
```bash
./devdojo.sh
```
You will see a "Hello World" message on the screen.
Another way to run the script would be:
```bash
bash devdojo.sh
```
As bash can be used interactively, you could run the following command directly in your terminal and you would get the same result:
```bash
echo "Hello DevDojo!"
```
Putting a script together is useful once you have to combine multiple commands together.

View File

@@ -1,131 +0,0 @@
# Bash Variables
As in any other programming language, you can use variables in Bash Scripting as well. However, there are no data types, and a variable in Bash can contain numbers as well as characters.
To assign a value to a variable, all you need to do is use the `=` sign:
```bash
name="DevDojo"
```
>{notice} as an important note, you can not have spaces before and after the `=` sign.
After that, to access the variable, you have to use the `$` and reference it as shown below:
```bash
echo $name
```
Wrapping the variable name between curly brackets is not required, but is considered a good practice, and I would advise you to use them whenever you can:
```bash
echo ${name}
```
The above code would output: `DevDojo` as this is the value of our `name` variable.
Next, let's update our `devdojo.sh` script and include a variable in it.
Again, you can open the file `devdojo.sh` with your favorite text editor, I'm using nano here to open the file:
```bash
nano devdojo.sh
```
Adding our `name` variable here in the file, with a welcome message. Our file now looks like this:
```bash
#!/bin/bash
name="DevDojo"
echo "Hi there $name"
```
Save it and run the file using the command below:
```bash
./devdojo.sh
```
You would see the following output on your screen:
```bash
Hi there DevDojo
```
Here is a rundown of the script written in the file:
* `#!/bin/bash` - At first, we specified our shebang.
* `name=DevDojo` - Then, we defined a variable called `name` and assigned a value to it.
* `echo "Hi there $name"` - Finally, we output the content of the variable on the screen as a welcome message by using `echo`
You can also add multiple variables in the file as shown below:
```bash
#!/bin/bash
name="DevDojo"
greeting="Hello"
echo "$greeting $name"
```
Save the file and run it again:
```bash
./devdojo.sh
```
You would see the following output on your screen:
```bash
Hello DevDojo
```
Note that you don't necessarily need to add semicolon `;` at the end of each line. It works both ways, a bit like other programming language such as JavaScript!
You can also add variables in the Command Line outside the Bash script and they can be read as parameters:
```bash
./devdojo.sh Bobby buddy!
```
This script takes in two parameters `Bobby`and `buddy!` separated by space. In the `devdojo.sh` file we have the following:
```bash
#!/bin/bash
echo "Hello there" $1
```
`$1` is the first input (`Bobby`) in the Command Line. Similarly, there could be more inputs and they are all referenced to by the `$` sign and their respective order of input. This means that `buddy!` is referenced to using `$2`. Another useful method for reading variables is the `$@` which reads all inputs.
So now let's change the `devdojo.sh` file to better understand:
```bash
#!/bin/bash
echo "Hello there" $1
# $1 : first parameter
echo "Hello there" $2
# $2 : second parameter
echo "Hello there" $@
# $@ : all
```
The output for:
```bash
./devdojo.sh Bobby buddy!
```
Would be the following:
```bash
Hello there Bobby
Hello there buddy!
Hello there Bobby buddy!
```

View File

@@ -1,54 +0,0 @@
# Bash User Input
With the previous script, we defined a variable, and we output the value of the variable on the screen with the `echo $name`.
Now let's go ahead and ask the user for input instead. To do that again, open the file with your favorite text editor and update the script as follows:
```bash
#!/bin/bash
echo "What is your name?"
read name
echo "Hi there $name"
echo "Welcome to DevDojo!"
```
The above will prompt the user for input and then store that input as a string/text in a variable.
We can then use the variable and print a message back to them.
The output of the above script would be:
* First run the script:
```bash
./devdojo.sh
```
* Then, you would be prompted to enter your name:
```
What is your name?
Bobby
```
* Once you've typed your name, just hit enter, and you will get the following output:
```
Hi there Bobby
Welcome to DevDojo!
```
To reduce the code, we could change the first `echo` statement with the `read -p`, the `read` command used with `-p` flag will print a message before prompting the user for their input:
```bash
#!/bin/bash
read -p "What is your name? " name
echo "Hi there $name"
echo "Welcome to DevDojo!"
```
Make sure to test this out yourself as well!

View File

@@ -1,27 +0,0 @@
# Bash Comments
As with any other programming language, you can add comments to your script. Comments are used to leave yourself notes through your code.
To do that in Bash, you need to add the `#` symbol at the beginning of the line. Comments will never be rendered on the screen.
Here is an example of a comment:
```bash
# This is a comment and will not be rendered on the screen
```
Let's go ahead and add some comments to our script:
```bash
#!/bin/bash
# Ask the user for their name
read -p "What is your name? " name
# Greet the user
echo "Hi there $name"
echo "Welcome to DevDojo!"
```
Comments are a great way to describe some of the more complex functionality directly in your scripts so that other people could find their way around your code with ease.

View File

@@ -1,81 +0,0 @@
# Bash Arguments
You can pass arguments to your shell script when you execute it. To pass an argument, you just need to write it right after the name of your script. For example:
```bash
./devdojo.com your_argument
```
In the script, we can then use `$1` in order to reference the first argument that we specified.
If we pass a second argument, it would be available as `$2` and so on.
Let's create a short script called `arguments.sh` as an example:
```bash
#!/bin/bash
echo "Argument one is $1"
echo "Argument two is $2"
echo "Argument three is $3"
```
Save the file and make it executable:
```bash
chmod +x arguments.sh
```
Then run the file and pass **3** arguments:
```bash
./arguments.sh dog cat bird
```
The output that you would get would be:
```bash
Argument one is dog
Argument two is cat
Argument three is bird
```
To reference all arguments, you can use `$@`:
```bash
#!/bin/bash
echo "All arguments: $@"
```
If you run the script again:
```bash
./arguments.sh dog cat bird
```
You will get the following output:
```
All arguments: dog cat bird
```
Another thing that you need to keep in mind is that `$0` is used to reference the script itself.
This is an excellent way to create self destruct the file if you need to or just get the name of the script.
For example, let's create a script that prints out the name of the file and deletes the file after that:
```bash
#!/bin/bash
echo "The name of the file is: $0 and it is going to be self-deleted."
rm -f $0
```
You need to be careful with the self deletion and ensure that you have your script backed up before you self-delete it.

View File

@@ -1,112 +0,0 @@
# Bash Arrays
If you have ever done any programming, you are probably already familiar with arrays.
But just in case you are not a developer, the main thing that you need to know is that unlike variables, arrays can hold several values under one name.
You can initialize an array by assigning values divided by space and enclosed in `()`. Example:
```bash
my_array=("value 1" "value 2" "value 3" "value 4")
```
To access the elements in the array, you need to reference them by their numeric index.
>{notice} keep in mind that you need to use curly brackets.
* Access a single element, this would output: `value 2`
```bash
echo ${my_array[1]}
```
* This would return the last element: `value 4`
```bash
echo ${my_array[-1]}
```
* As with command line arguments using `@` will return all arguments in the array, as follows: `value 1 value 2 value 3 value 4`
```bash
echo ${my_array[@]}
```
* Prepending the array with a hash sign (`#`) would output the total number of elements in the array, in our case it is `4`:
```bash
echo ${#my_array[@]}
```
Make sure to test this and practice it at your end with different values.
## Substring in Bash :: Slicing
Let's review the following example of slicing in a string in Bash:
```bash
#!/bin/bash
letters=( "A""B""C""D""E" )
echo ${letters[@]}
```
This command will print all the elements of an array.
Output:
```bash
$ ABCDE
```
Let's see a few more examples:
- Example 1
```bash
#!/bin/bash
letters=( "A""B""C""D""E" )
b=${letters:0:2}
echo "${b}"
```
This command will print array from starting index 0 to 2 where 2 is exclusive.
```bash
$ AB
```
- Example 2
```bash
#!/bin/bash
letters=( "A""B""C""D""E" )
b=${letters::5}
echo "${b}"
```
This command will print from base index 0 to 5, where 5 is exclusive and starting index is default set to 0 .
```bash
$ ABCDE
```
- Example 3
```bash
#!/bin/bash
letters=( "A""B""C""D""E" )
b=${letters:3}
echo "${b}"
```
This command will print from starting index
3 to end of array inclusive .
```bash
$ DE
```

View File

@@ -1,186 +0,0 @@
# Bash Conditional Expressions
In computer science, conditional statements, conditional expressions, and conditional constructs are features of a programming language, which perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false.
In Bash, conditional expressions are used by the `[[` compound command and the `[`built-in commands to test file attributes and perform string and arithmetic comparisons.
Here is a list of the most popular Bash conditional expressions. You do not have to memorize them by heart. You can simply refer back to this list whenever you need it!
## File expressions
* True if file exists.
```bash
[[ -a ${file} ]]
```
* True if file exists and is a block special file.
```bash
[[ -b ${file} ]]
```
* True if file exists and is a character special file.
```bash
[[ -c ${file} ]]
```
* True if file exists and is a directory.
```bash
[[ -d ${file} ]]
```
* True if file exists.
```bash
[[ -e ${file} ]]
```
* True if file exists and is a regular file.
```bash
[[ -f ${file} ]]
```
* True if file exists and is a symbolic link.
```bash
[[ -h ${file} ]]
```
* True if file exists and is readable.
```bash
[[ -r ${file} ]]
```
* True if file exists and has a size greater than zero.
```bash
[[ -s ${file} ]]
```
* True if file exists and is writable.
```bash
[[ -w ${file} ]]
```
* True if file exists and is executable.
```bash
[[ -x ${file} ]]
```
* True if file exists and is a symbolic link.
```bash
[[ -L ${file} ]]
```
## String expressions
* True if the shell variable varname is set (has been assigned a value).
```bash
[[ -v ${varname} ]]
```
True if the length of the string is zero.
```bash
[[ -z ${string} ]]
```
True if the length of the string is non-zero.
```bash
[[ -n ${string} ]]
```
* True if the strings are equal. `=` should be used with the test command for POSIX conformance. When used with the `[[` command, this performs pattern matching as described above (Compound Commands).
```bash
[[ ${string1} == ${string2} ]]
```
* True if the strings are not equal.
```bash
[[ ${string1} != ${string2} ]]
```
* True if string1 sorts before string2 lexicographically.
```bash
[[ ${string1} < ${string2} ]]
```
* True if string1 sorts after string2 lexicographically.
```bash
[[ ${string1} > ${string2} ]]
```
## Arithmetic operators
* Returns true if the numbers are **equal**
```bash
[[ ${arg1} -eq ${arg2} ]]
```
* Returns true if the numbers are **not equal**
```bash
[[ ${arg1} -ne ${arg2} ]]
```
* Returns true if arg1 is **less than** arg2
```bash
[[ ${arg1} -lt ${arg2} ]]
```
* Returns true if arg1 is **less than or equal** arg2
```bash
[[ ${arg1} -le ${arg2} ]]
```
* Returns true if arg1 is **greater than** arg2
```bash
[[ ${arg1} -gt ${arg2} ]]
```
* Returns true if arg1 is **greater than or equal** arg2
```bash
[[ ${arg1} -ge ${arg2} ]]
```
As a side note, arg1 and arg2 may be positive or negative integers.
As with other programming languages you can use `AND` & `OR` conditions:
```bash
[[ test_case_1 ]] && [[ test_case_2 ]] # And
[[ test_case_1 ]] || [[ test_case_2 ]] # Or
```
## Exit status operators
* returns true if the command was successful without any errors
```bash
[[ $? -eq 0 ]]
```
* returns true if the command was not successful or had errors
```bash
[[ $? -gt 0 ]]
```

View File

@@ -1,187 +0,0 @@
# Bash Conditionals
In the last section, we covered some of the most popular conditional expressions. We can now use them with standard conditional statements like `if`, `if-else` and `switch case` statements.
## If statement
The format of an `if` statement in Bash is as follows:
```bash
if [[ some_test ]]
then
<commands>
fi
```
Here is a quick example which would ask you to enter your name in case that you've left it empty:
```bash
#!/bin/bash
# Bash if statement example
read -p "What is your name? " name
if [[ -z ${name} ]]
then
echo "Please enter your name!"
fi
```
## If Else statement
With an `if-else` statement, you can specify an action in case that the condition in the `if` statement does not match. We can combine this with the conditional expressions from the previous section as follows:
```bash
#!/bin/bash
# Bash if statement example
read -p "What is your name? " name
if [[ -z ${name} ]]
then
echo "Please enter your name!"
else
echo "Hi there ${name}"
fi
```
You can use the above if statement with all of the conditional expressions from the previous chapters:
```bash
#!/bin/bash
admin="devdojo"
read -p "Enter your username? " username
# Check if the username provided is the admin
if [[ "${username}" == "${admin}" ]] ; then
echo "You are the admin user!"
else
echo "You are NOT the admin user!"
fi
```
Here is another example of an `if` statement which would check your current `User ID` and would not allow you to run the script as the `root` user:
```bash
#!/bin/bash
if (( $EUID == 0 )); then
echo "Please do not run as root"
exit
fi
```
If you put this on top of your script it would exit in case that the EUID is 0 and would not execute the rest of the script. This was discussed on [the DigitalOcean community forum](https://www.digitalocean.com/community/questions/how-to-check-if-running-as-root-in-a-bash-script).
You can also test multiple conditions with an `if` statement. In this example we want to make sure that the user is neither the admin user nor the root user to ensure the script is incapable of causing too much damage. We'll use the `or` operator in this example, noted by `||`. This means that either of the conditions needs to be true. If we used the `and` operator of `&&` then both conditions would need to be true.
```bash
#!/bin/bash
admin="devdojo"
read -p "Enter your username? " username
# Check if the username provided is the admin
if [[ "${username}" != "${admin}" ]] || [[ $EUID != 0 ]] ; then
echo "You are not the admin or root user, but please be safe!"
else
echo "You are the admin user! This could be very destructive!"
fi
```
If you have multiple conditions and scenarios, then can use `elif` statement with `if` and `else` statements.
```bash
#!/bin/bash
read -p "Enter a number: " num
if [[ $num -gt 0 ]] ; then
echo "The number is positive"
elif [[ $num -lt 0 ]] ; then
echo "The number is negative"
else
echo "The number is 0"
fi
```
## Switch case statements
As in other programming languages, you can use a `case` statement to simplify complex conditionals when there are multiple different choices. So rather than using a few `if`, and `if-else` statements, you could use a single `case` statement.
The Bash `case` statement syntax looks like this:
```bash
case $some_variable in
pattern_1)
commands
;;
pattern_2| pattern_3)
commands
;;
*)
default commands
;;
esac
```
A quick rundown of the structure:
* All `case` statements start with the `case` keyword.
* On the same line as the `case` keyword, you need to specify a variable or an expression followed by the `in` keyword.
* After that, you have your `case` patterns, where you need to use `)` to identify the end of the pattern.
* You can specify multiple patterns divided by a pipe: `|`.
* After the pattern, you specify the commands that you would like to be executed in case that the pattern matches the variable or the expression that you've specified.
* All clauses have to be terminated by adding `;;` at the end.
* You can have a default statement by adding a `*` as the pattern.
* To close the `case` statement, use the `esac` (case typed backwards) keyword.
Here is an example of a Bash `case` statement:
```bash
#!/bin/bash
read -p "Enter the name of your car brand: " car
case $car in
Tesla)
echo -n "${car}'s car factory is in the USA."
;;
BMW | Mercedes | Audi | Porsche)
echo -n "${car}'s car factory is in Germany."
;;
Toyota | Mazda | Mitsubishi | Subaru)
echo -n "${car}'s car factory is in Japan."
;;
*)
echo -n "${car} is an unknown car brand"
;;
esac
```
With this script, we are asking the user to input a name of a car brand like Telsa, BMW, Mercedes and etc.
Then with a `case` statement, we check the brand name and if it matches any of our patterns, and if so, we print out the factory's location.
If the brand name does not match any of our `case` statements, we print out a default message: `an unknown car brand`.
## Conclusion
I would advise you to try and modify the script and play with it a bit so that you could practice what you've just learned in the last two chapters!
For more examples of Bash `case` statements, make sure to check chapter 16, where we would create an interactive menu in Bash using a `cases` statement to process the user input.

View File

@@ -1,197 +0,0 @@
# Bash Loops
As with any other language, loops are very convenient. With Bash you can use `for` loops, `while` loops, and `until` loops.
## For loops
Here is the structure of a for loop:
```bash
for var in ${list}
do
your_commands
done
```
Example:
```bash
#!/bin/bash
users="devdojo bobby tony"
for user in ${users}
do
echo "${user}"
done
```
A quick rundown of the example:
* First, we specify a list of users and store the value in a variable called `$users`.
* After that, we start our `for` loop with the `for` keyword.
* Then we define a new variable which would represent each item from the list that we give. In our case, we define a variable called `user`, which would represent each user from the `$users` variable.
* Then we specify the `in` keyword followed by our list that we will loop through.
* On the next line, we use the `do` keyword, which indicates what we will do for each iteration of the loop.
* Then we specify the commands that we want to run.
* Finally, we close the loop with the `done` keyword.
You can also use `for` to process a series of numbers. For example here is one way to loop through from 1 to 10:
```bash
#!/bin/bash
for num in {1..10}
do
echo ${num}
done
```
## While loops
The structure of a while loop is quite similar to the `for` loop:
```bash
while [ your_condition ]
do
your_commands
done
```
Here is an example of a `while` loop:
```bash
#!/bin/bash
counter=1
while [[ $counter -le 10 ]]
do
echo $counter
((counter++))
done
```
First, we specified a counter variable and set it to `1`, then inside the loop, we added counter by using this statement here: `((counter++))`. That way, we make sure that the loop will run 10 times only and would not run forever. The loop will complete as soon as the counter becomes 10, as this is what we've set as the condition: `while [[ $counter -le 10 ]]`.
Let's create a script that asks the user for their name and not allow an empty input:
```bash
#!/bin/bash
read -p "What is your name? " name
while [[ -z ${name} ]]
do
echo "Your name can not be blank. Please enter a valid name!"
read -p "Enter your name again? " name
done
echo "Hi there ${name}"
```
Now, if you run the above and just press enter without providing input, the loop would run again and ask you for your name again and again until you actually provide some input.
## Until Loops
The difference between `until` and `while` loops is that the `until` loop will run the commands within the loop until the condition becomes true.
Structure:
```bash
until [[ your_condition ]]
do
your_commands
done
```
Example:
```bash
#!/bin/bash
count=1
until [[ $count -gt 10 ]]
do
echo $count
((count++))
done
```
## Continue and Break
As with other languages, you can use `continue` and `break` with your bash scripts as well:
* `continue` tells your bash script to stop the current iteration of the loop and start the next iteration.
The syntax of the continue statement is as follows:
```bash
continue [n]
```
The [n] argument is optional and can be greater than or equal to 1. When [n] is given, the n-th enclosing loop is resumed. continue 1 is equivalent to continue.
```bash
#!/bin/bash
for i in 1 2 3 4 5
do
if [[ $i eq 2 ]]
then
echo "skipping number 2"
continue
fi
echo "i is equal to $i"
done
```
We can also use continue command in similar way to break command for controlling multiple loops.
* `break` tells your bash script to end the loop straight away.
The syntax of the break statement takes the following form:
```bash
break [n]
```
[n] is an optional argument and must be greater than or equal to 1. When [n] is provided, the n-th enclosing loop is exited. break 1 is equivalent to break.
Example:
```bash
#!/bin/bash
num=1
while [[ $num lt 10 ]]
do
if [[ $num eq 5 ]]
then
break
fi
((num++))
done
echo "Loop completed"
```
We can also use break command with multiple loops. If we want to exit out of current working loop whether inner or outer loop, we simply use break but if we are in inner loop & want to exit out of outer loop, we use break 2.
Example:
```bash
#!/bin/bash
for (( a = 1; a < 10; a++ ))
do
echo "outer loop: $a"
for (( b = 1; b < 100; b++ ))
do
if [[ $b gt 5 ]]
then
break 2
fi
echo "Inner loop: $b "
done
done
```
The bash script will begin with a=1 & will move to inner loop and when it reaches b=5, it will break the outer loop.
We can use break only instead of break 2, to break inner loop & see how it affects the output.

View File

@@ -1,66 +0,0 @@
# Bash Functions
Functions are a great way to reuse code. The structure of a function in bash is quite similar to most languages:
```bash
function function_name() {
your_commands
}
```
You can also omit the `function` keyword at the beginning, which would also work:
```bash
function_name() {
your_commands
}
```
I prefer putting it there for better readability. But it is a matter of personal preference.
Example of a "Hello World!" function:
```bash
#!/bin/bash
function hello() {
echo "Hello World Function!"
}
hello
```
>{notice} One thing to keep in mind is that you should not add the parenthesis when you call the function.
Passing arguments to a function work in the same way as passing arguments to a script:
```bash
#!/bin/bash
function hello() {
echo "Hello $1!"
}
hello DevDojo
```
Functions should have comments mentioning description, global variables, arguments, outputs, and returned values, if applicable
```bash
#######################################
# Description: Hello function
# Globals:
# None
# Arguments:
# Single input argument
# Outputs:
# Value of input argument
# Returns:
# 0 if successful, non-zero on error.
#######################################
function hello() {
echo "Hello $1!"
}
```
In the next few chapters we will be using functions a lot!

View File

@@ -1,83 +0,0 @@
# Debugging, testing and shortcuts
In order to debug your bash scripts, you can use `-x` when executing your scripts:
```bash
bash -x ./your_script.sh
```
Or you can add `set -x` before the specific line that you want to debug, `set -x` enables a mode of the shell where all executed commands are printed to the terminal.
Another way to test your scripts is to use this fantastic tool here:
[https://www.shellcheck.net/](https://www.shellcheck.net/)
Just copy and paste your code into the textbox, and the tool will give you some suggestions on how you can improve your script.
You can also run the tool directly in your terminal:
[https://github.com/koalaman/shellcheck](https://github.com/koalaman/shellcheck)
If you like the tool, make sure to star it on GitHub and contribute!
As a SysAdmin/DevOps, I spend a lot of my day in the terminal. Here are my favorite shortcuts that help me do tasks quicker while writing Bash scripts or just while working in the terminal.
The below two are particularly useful if you have a very long command.
* Delete everything from the cursor to the end of the line:
```
Ctrl + k
```
* Delete everything from the cursor to the start of the line:
```
Ctrl + u
```
* Delete one word backward from cursor:
```
Ctrl + w
```
* Search your history backward. This is probably the one that I use the most. It is really handy and speeds up my work-flow a lot:
```
Ctrl + r
```
* Clear the screen, I use this instead of typing the `clear` command:
```
Ctrl + l
```
* Stops the output to the screen:
```
Ctrl + s
```
* Enable the output to the screen in case that previously stopped by `Ctrl + s`:
```
Ctrl + q
```
* Terminate the current command
```
Ctrl + c
```
* Throw the current command to background:
```
Ctrl + z
```
I use those regularly every day, and it saves me a lot of time.
If you think that I've missed any feel free to join the discussion on [the DigitalOcean community forum](https://www.digitalocean.com/community/questions/what-are-your-favorite-bash-shortcuts)!

View File

@@ -1,83 +0,0 @@
# Creating custom bash commands
As a developer or system administrator, you might have to spend a lot of time in your terminal. I always try to look for ways to optimize any repetitive tasks.
One way to do that is to either write short bash scripts or create custom commands also known as aliases. For example, rather than typing a really long command every time you could just create a shortcut for it.
## Example
Let's start with the following scenario, as a system admin, you might have to check the connections to your web server quite often, so I will use the `netstat` command as an example.
What I would usually do when I access a server that is having issues with the connections to port 80 or 443 is to check if there are any services listening on those ports and the number of connections to the ports.
The following `netstat` command would show us how many TCP connections on port 80 and 443 we currently have:
```bash
netstat -plant | grep '80\|443' | grep -v LISTEN | wc -l
```
This is quite a lengthy command so typing it every time might be time-consuming in the long run especially when you want to get that information quickly.
To avoid that, we can create an alias, so rather than typing the whole command, we could just type a short command instead. For example, lets say that we wanted to be able to type `conn` (short for connections) and get the same information. All we need to do in this case is to run the following command:
```bash
alias conn="netstat -plant | grep '80\|443' | grep -v LISTEN | wc -l"
```
That way we are creating an alias called `conn` which would essentially be a 'shortcut' for our long `netstat` command. Now if you run just `conn`:
```bash
conn
```
You would get the same output as the long `netstat` command.
You can get even more creative and add some info messages like this one here:
```bash
alias conn="echo 'Total connections on port 80 and 443:' ; netstat -plant | grep '80\|443' | grep -v LISTEN | wc -l"
```
Now if you run `conn` you would get the following output:
```bash
Total connections on port 80 and 443:
12
```
Now if you log out and log back in, your alias would be lost. In the next step you will see how to make this persistent.
## Making the change persistent
In order to make the change persistent, we need to add the `alias` command in our shell profile file.
By default on Ubuntu this would be the `~/.bashrc` file, for other operating systems this might be the `~/.bash_profle`. With your favorite text editor open the file:
```bash
nano ~/.bashrc
```
Go to the bottom and add the following:
```bash
alias conn="echo 'Total connections on port 80 and 443:' ; netstat -plant | grep '80\|443' | grep -v LISTEN | wc -l"
```
Save and then exit.
That way now even if you log out and log back in again your change would be persisted and you would be able to run your custom bash command.
## Listing all of the available aliases
To list all of the available aliases for your current shell, you have to just run the following command:
```bash
alias
```
This would be handy in case that you are seeing some weird behavior with some commands.
## Conclusion
This is one way of creating custom bash commands or bash aliases.
Of course, you could actually write a bash script and add the script inside your `/usr/bin` folder, but this would not work if you don't have root or sudo access, whereas with aliases you can do it without the need of root access.
>{notice} This was initially posted on [DevDojo.com](https://devdojo.com/bobbyiliev/how-to-create-custom-bash-commands)

View File

@@ -1,180 +0,0 @@
# Write your first Bash script
Let's try to put together what we've learned so far and create our first Bash script!
## Planning the script
As an example, we will write a script that would gather some useful information about our server like:
* Current Disk usage
* Current CPU usage
* Current RAM usage
* Check the exact Kernel version
Feel free to adjust the script by adding or removing functionality so that it matches your needs.
## Writing the script
The first thing that you need to do is to create a new file with a `.sh` extension. I will create a file called `status.sh` as the script that we will create would give us the status of our server.
Once you've created the file, open it with your favorite text editor.
As we've learned in chapter 1, on the very first line of our Bash script we need to specify the so-called [Shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)):
```bash
#!/bin/bash
```
All that the shebang does is to instruct the operating system to run the script with the /bin/bash executable.
## Adding comments
Next, as discussed in chapter 6, let's start by adding some comments so that people could easily figure out what the script is used for. To do that right after the shebang you can just add the following:
```bash
#!/bin/bash
# Script that returns the current server status
```
## Adding your first variable
Then let's go ahead and apply what we've learned in chapter 4 and add some variables which we might want to use throughout the script.
To assign a value to a variable in bash, you just have to use the `=` sign. For example, let's store the hostname of our server in a variable so that we could use it later:
```bash
server_name=$(hostname)
```
By using `$()` we tell bash to actually interpret the command and then assign the value to our variable.
Now if we were to echo out the variable we would see the current hostname:
```bash
echo $server_name
```
## Adding your first function
As you already know after reading chapter 12, in order to create a function in bash you need to use the following structure:
```bash
function function_name() {
your_commands
}
```
Let's create a function that returns the current memory usage on our server:
```bash
function memory_check() {
echo ""
echo "The current memory usage on ${server_name} is: "
free -h
echo ""
}
```
Quick run down of the function:
* `function memory_check() {` - this is how we define the function
* `echo ""` - here we just print a new line
* `echo "The current memory usage on ${server_name} is: "` - here we print a small message and the `$server_name` variable
* `}` - finally this is how we close the function
Then once the function has been defined, in order to call it, just use the name of the function:
```bash
# Define the function
function memory_check() {
echo ""
echo "The current memory usage on ${server_name} is: "
free -h
echo ""
}
# Call the function
memory_check
```
## Adding more functions challenge
Before checking out the solution, I would challenge you to use the function from above and write a few functions by yourself.
The functions should do the following:
* Current Disk usage
* Current CPU usage
* Current RAM usage
* Check the exact Kernel version
Feel free to use google if you are not sure what commands you need to use in order to get that information.
Once you are ready, feel free to scroll down and check how we've done it and compare the results!
Note that there are multiple correct ways of doing it!
## The sample script
Here's what the end result would look like:
```bash
#!/bin/bash
##
# BASH script that checks:
# - Memory usage
# - CPU load
# - Number of TCP connections
# - Kernel version
##
server_name=$(hostname)
function memory_check() {
echo ""
echo "Memory usage on ${server_name} is: "
free -h
echo ""
}
function cpu_check() {
echo ""
echo "CPU load on ${server_name} is: "
echo ""
uptime
echo ""
}
function tcp_check() {
echo ""
echo "TCP connections on ${server_name}: "
echo ""
cat /proc/net/tcp | wc -l
echo ""
}
function kernel_check() {
echo ""
echo "Kernel version on ${server_name} is: "
echo ""
uname -r
echo ""
}
function all_checks() {
memory_check
cpu_check
tcp_check
kernel_check
}
all_checks
```
## Conclusion
Bash scripting is awesome! No matter if you are a DevOps/SysOps engineer, developer, or just a Linux enthusiast, you can use Bash scripts to combine different Linux commands and automate boring and repetitive daily tasks, so that you can focus on more productive and fun things!
>{notice} This was initially posted on [DevDojo.com](https://devdojo.com/bobbyiliev/introduction-to-bash-scripting)

View File

@@ -1,305 +0,0 @@
# Creating an interactive menu in Bash
In this tutorial, I will show you how to create a multiple-choice menu in Bash so that your users could choose between what action should be executed!
We would reuse some of the code from the previous chapter, so if you have not read it yet make sure to do so.
## Planning the functionality
Let's start again by going over the main functionality of the script:
* Checks the current Disk usage
* Checks the current CPU usage
* Checks the current RAM usage
* Checks the check the exact Kernel version
In case that you don't have it on hand, here is the script itself:
```bash
#!/bin/bash
##
# BASH menu script that checks:
# - Memory usage
# - CPU load
# - Number of TCP connections
# - Kernel version
##
server_name=$(hostname)
function memory_check() {
echo ""
echo "Memory usage on ${server_name} is: "
free -h
echo ""
}
function cpu_check() {
echo ""
echo "CPU load on ${server_name} is: "
echo ""
uptime
echo ""
}
function tcp_check() {
echo ""
echo "TCP connections on ${server_name}: "
echo ""
cat /proc/net/tcp | wc -l
echo ""
}
function kernel_check() {
echo ""
echo "Kernel version on ${server_name} is: "
echo ""
uname -r
echo ""
}
function all_checks() {
memory_check
cpu_check
tcp_check
kernel_check
}
```
We will then build a menu that allows the user to choose which function to be executed.
Of course, you can adjust the function or add new ones depending on your needs.
## Adding some colors
In order to make the menu a bit more 'readable' and easy to grasp at first glance, we will add some color functions.
At the beginning of your script add the following color functions:
```bash
##
# Color Variables
##
green='\e[32m'
blue='\e[34m'
clear='\e[0m'
##
# Color Functions
##
ColorGreen(){
echo -ne $green$1$clear
}
ColorBlue(){
echo -ne $blue$1$clear
}
```
You can use the color functions as follows:
```bash
echo -ne $(ColorBlue 'Some text here')
```
The above would output the `Some text here` string and it would be blue!
# Adding the menu
Finally, to add our menu, we will create a separate function with a case switch for our menu options:
```bash
menu(){
echo -ne "
My First Menu
$(ColorGreen '1)') Memory usage
$(ColorGreen '2)') CPU load
$(ColorGreen '3)') Number of TCP connections
$(ColorGreen '4)') Kernel version
$(ColorGreen '5)') Check All
$(ColorGreen '0)') Exit
$(ColorBlue 'Choose an option:') "
read a
case $a in
1) memory_check ; menu ;;
2) cpu_check ; menu ;;
3) tcp_check ; menu ;;
4) kernel_check ; menu ;;
5) all_checks ; menu ;;
0) exit 0 ;;
*) echo -e $red"Wrong option."$clear; WrongCommand;;
esac
}
```
### A quick rundown of the code
First we just echo out the menu options with some color:
```
echo -ne "
My First Menu
$(ColorGreen '1)') Memory usage
$(ColorGreen '2)') CPU load
$(ColorGreen '3)') Number of TCP connections
$(ColorGreen '4)') Kernel version
$(ColorGreen '5)') Check All
$(ColorGreen '0)') Exit
$(ColorBlue 'Choose an option:') "
```
Then we read the answer of the user and store it in a variable called `$a`:
```bash
read a
```
Finally, we have a switch case which triggers a different function depending on the value of `$a`:
```bash
case $a in
1) memory_check ; menu ;;
2) cpu_check ; menu ;;
3) tcp_check ; menu ;;
4) kernel_check ; menu ;;
5) all_checks ; menu ;;
0) exit 0 ;;
*) echo -e $red"Wrong option."$clear; WrongCommand;;
esac
```
At the end we need to call the menu function to actually print out the menu:
```bash
# Call the menu function
menu
```
## Testing the script
In the end, your script will look like this:
```bash
#!/bin/bash
##
# BASH menu script that checks:
# - Memory usage
# - CPU load
# - Number of TCP connections
# - Kernel version
##
server_name=$(hostname)
function memory_check() {
echo ""
echo "Memory usage on ${server_name} is: "
free -h
echo ""
}
function cpu_check() {
echo ""
echo "CPU load on ${server_name} is: "
echo ""
uptime
echo ""
}
function tcp_check() {
echo ""
echo "TCP connections on ${server_name}: "
echo ""
cat /proc/net/tcp | wc -l
echo ""
}
function kernel_check() {
echo ""
echo "Kernel version on ${server_name} is: "
echo ""
uname -r
echo ""
}
function all_checks() {
memory_check
cpu_check
tcp_check
kernel_check
}
##
# Color Variables
##
green='\e[32m'
blue='\e[34m'
clear='\e[0m'
##
# Color Functions
##
ColorGreen(){
echo -ne $green$1$clear
}
ColorBlue(){
echo -ne $blue$1$clear
}
menu(){
echo -ne "
My First Menu
$(ColorGreen '1)') Memory usage
$(ColorGreen '2)') CPU load
$(ColorGreen '3)') Number of TCP connections
$(ColorGreen '4)') Kernel version
$(ColorGreen '5)') Check All
$(ColorGreen '0)') Exit
$(ColorBlue 'Choose an option:') "
read a
case $a in
1) memory_check ; menu ;;
2) cpu_check ; menu ;;
3) tcp_check ; menu ;;
4) kernel_check ; menu ;;
5) all_checks ; menu ;;
0) exit 0 ;;
*) echo -e $red"Wrong option."$clear; WrongCommand;;
esac
}
# Call the menu function
menu
```
To test the script, create a new filed with a `.sh` extension, for example: `menu.sh` and then run it:
```bash
bash menu.sh
```
The output that you would get will look like this:
```bash
My First Menu
1) Memory usage
2) CPU load
3) Number of TCP connections
4) Kernel version
5) Check All
0) Exit
Choose an option:
```
You will be able to choose a different option from the list and each number will call a different function from the script:
![Nice Bash interactive menu](https://imgur.com/8EgxX4m.png)
## Conclusion
You now know how to create a Bash menu and implement it in your scripts so that users could select different values!
>{notice} This content was initially posted on [DevDojo.com](https://devdojo.com/bobbyiliev/how-to-work-with-json-in-bash-using-jq)

View File

@@ -1,129 +0,0 @@
# Executing BASH scripts on Multiple Remote Servers
Any command that you can run from the command line can be used in a bash script. Scripts are used to run a series of commands. Bash is available by default on Linux and macOS operating systems.
Let's have a hypothetical scenario where you need to execute a BASH script on multiple remote servers, but you don't want to manually copy the script to each server, then again login to each server individually and only then execute the script.
Of course you could use a tool like Ansible but let's learn how to do that with Bash!
## Prerequisites
For this example I will use 3 remote Ubuntu servers deployed on DigitalOcean. If you don't have a Digital Ocean account yet, you can sign up for DigitalOcean and get $100 free credit via this referral link here:
[https://m.do.co/c/2a9bba940f39](https://m.do.co/c/2a9bba940f39)
Once you have your Digital Ocean account ready go ahead and deploy 3 droplets.
I've gone ahead and created 3 Ubuntu servers:
![DigitalOcean Ubuntu servers](https://imgur.com/09xmq41.png)
I'll put a those servers IP's in a `servers.txt` file which I would use to loop though with our Bash script.
If you are new to DigitalOcean you can follow the steps on how to create a Droplet here:
* [How to Create a Droplet from the DigitalOcean Control Panel](https://www.digitalocean.com/docs/droplets/how-to/create/)
You can also follow the steps from this video here on how to do your initial server setup:
* [How to do your Initial Server Setup with Ubuntu](https://youtu.be/7NL2_4HIgKU)
Or even better, you can follow this article here on how to automate your initial server setup with Bash:
[Automating Initial Server Setup with Ubuntu 18.04 with Bash](https://www.digitalocean.com/community/tutorials/automating-initial-server-setup-with-ubuntu-18-04)
With the 3 new servers in place, we can go ahead and focus on running our Bash script on all of them with a single command!
## The BASH Script
I will reuse the demo script from the previous chapter with some slight changes. It simply executes a few checks like the current memory usage, the current CPU usage, the number of TCP connections and the version of the kernel.
```bash
#!/bin/bash
##
# BASH script that checks the following:
# - Memory usage
# - CPU load
# - Number of TCP connections
# - Kernel version
##
##
# Memory check
##
server_name=$(hostname)
function memory_check() {
echo "#######"
echo "The current memory usage on ${server_name} is: "
free -h
echo "#######"
}
function cpu_check() {
echo "#######"
echo "The current CPU load on ${server_name} is: "
echo ""
uptime
echo "#######"
}
function tcp_check() {
echo "#######"
echo "Total TCP connections on ${server_name}: "
echo ""
cat /proc/net/tcp | wc -l
echo "#######"
}
function kernel_check() {
echo "#######"
echo "The exact Kernel version on ${server_name} is: "
echo ""
uname -r
echo "#######"
}
function all_checks() {
memory_check
cpu_check
tcp_check
kernel_check
}
all_checks
```
Copy the code bellow and add this in a file called `remote_check.sh`. You can also get the script from [here](https://devdojo.com/bobbyiliev/executing-bash-script-on-multiple-remote-server).
## Running the Script on all Servers
Now that we have the script and the servers ready and that we've added those servers in our servers.txt file we can run the following command to loop though all servers and execute the script remotely without having to copy the script to each server and individually connect to each server.
```bash
for server in $(cat servers.txt) ; do ssh your_user@${server} 'bash -s' < ./remote_check.sh ; done
```
What this for loop does is, it goes through each server in the servers.txt file and then it runs the following command for each item in the list:
```bash
ssh your_user@the_server_ip 'bash -s' < ./remote_check.sh
```
You would get the following output:
![Running bash script on multiple remote servers](https://imgur.com/B1AmhUP.png)
## Conclusion
This is just a really simple example on how to execute a simple script on multiple servers without having to copy the script to each server and without having to access the servers individually.
Of course you could run a much more complex script and on many more servers.
If you are interested in automation, I would recommend checking out the Ansible resources page on the DigitalOcean website:
[Ansible Resources](https://www.digitalocean.com/community/tags/ansible)
>{notice} This content was initially posted on [DevDojo](https://devdojo.com/bobbyiliev/bash-script-to-summarize-your-nginx-and-apache-access-logs)

View File

@@ -1,225 +0,0 @@
# Work with JSON in BASH using jq
The `jq` command-line tool is a lightweight and flexible command-line **JSON** processor. It is great for parsing JSON output in BASH.
One of the great things about `jq` is that it is written in portable C, and it has zero runtime dependencies. All you need to do is to download a single binary or use a package manager like apt and install it with a single command.
## Planning the script
For the demo in this tutorial, I would use an external REST API that returns a simple JSON output called the [QuizAPI](https://quizapi.io/):
> [https://quizapi.io/](https://quizapi.io/)
If you want to follow along make sure to get a free API key here:
> [https://quizapi.io/clientarea/settings/token](https://quizapi.io/clientarea/settings/token)
The QuizAPI is free for developers.
## Installing jq
There are many ways to install `jq` on your system. One of the most straight forward ways to do so is to use the package manager depending on your OS.
Here is a list of the commands that you would need to use depending on your OS:
* Install jq on Ubuntu/Debian:
```bash
sudo apt-get install jq
```
* Install jq on Fedora:
```bash
sudo dnf install jq
```
* Install jq on openSUSE:
```bash
sudo zypper install jq
```
- Install jq on Arch:
```bash
sudo pacman -S jq
```
* Installing on Mac with Homebrew:
```bash
brew install jq
```
* Install on Mac with MacPort:
```bash
port install jq
```
If you are using other OS, I would recommend taking a look at the official documentation here for more information:
> [https://stedolan.github.io/jq/download/](https://stedolan.github.io/jq/download/)
Once you have jq installed you can check your current version by running this command:
```bash
jq --version
```
## Parsing JSON with jq
Once you have `jq` installed and your QuizAPI API Key, you can parse the JSON output of the QuizAPI directly in your terminal.
First, create a variable that stores your API Key:
```bash
API_KEY=YOUR_API_KEY_HERE
```
In order to get some output from one of the endpoints of the QuizAPI you can use the curl command:
```bash
curl "https://quizapi.io/api/v1/questions?apiKey=${API_KEY}&limit=10"
```
For a more specific output, you can use the QuizAPI URL Generator here:
> [https://quizapi.io/api-config](https://quizapi.io/api-config)
After running the curl command, the output which you would get would look like this:
![Raw Json output](https://imgur.com/KghOfzj.png)
This could be quite hard to read, but thanks to the jq command-line tool, all we need to do is pipe the curl command to jq and we would see a nice formatted JSON output:
```bash
curl "https://quizapi.io/api/v1/questions?apiKey=${API_KEY}&limit=10" | jq
```
> Note the `| jq` at the end.
In this case the output that you would get would look something like this:
![bash jq formatting](https://imgur.com/ebdTtVf.png)
Now, this looks much nicer! The jq command-line tool formatted the output for us and added some nice coloring!
## Getting the first element with jq
Let's say that we only wanted to get the first element from the JSON output, in order to do that we have to just specify the index that we want to see with the following syntax:
```bash
jq .[0]
```
Now, if we run the curl command again and pipe the output to jq .[0] like this:
```bash
curl "https://quizapi.io/api/v1/questions?apiKey=${API_KEY}&limit=10" | jq.[0]
```
You will only get the first element and the output will look like this:
![jq get first element only](https://imgur.com/h9bFMAL.png)
## Getting a value only for specific key
Sometimes you might want to get only the value of a specific key only, let's say in our example the QuizAPI returns a list of questions along with the answers, description and etc. but what if you wanted to get the Questions only without the additional information?
This is going to be quite straight forward with `jq`, all you need to do is add the key after jq command, so it would look something like this:
```bash
jq .[].question
```
We have to add the `.[]` as the QuizAPI returns an array and by specifying `.[]` we tell jq that we want to get the .question value for all of the elements in the array.
The output that you would get would look like this:
![jq get a value only for specific key](https://imgur.com/0701wHD.png)
As you can see we now only get the questions without the rest of the values.
## Using jq in a BASH script
Let's go ahead and create a small bash script which should output the following information for us:
* Get only the first question from the output
* Get all of the answers for that question
* Assign the answers to variables
* Print the question and the answers
* To do that I've put together the following script:
>{notice} make sure to change the API_KEY part with your actual QuizAPI key:
```bash
#!/bin/bash
##
# Make an API call to QuizAPI and store the output in a variable
##
output=$(curl 'https://quizapi.io/api/v1/questions?apiKey=API_KEY&limit=10' 2>/dev/null)
##
# Get only the first question
##
output=$(echo $output | jq .[0])
##
# Get the question
##
question=$(echo $output | jq .question)
##
# Get the answers
##
answer_a=$(echo $output | jq .answers.answer_a)
answer_b=$(echo $output | jq .answers.answer_b)
answer_c=$(echo $output | jq .answers.answer_c)
answer_d=$(echo $output | jq .answers.answer_d)
##
# Output the question
##
echo "
Question: ${question}
A) ${answer_a}
B) ${answer_b}
C) ${answer_c}
D) ${answer_d}
"
```
If you run the script you would get the following output:
![Using jq in a bash script](https://imgur.com/LKEsrbq.png)
We can even go further by making this interactive so that we could actually choose the answer directly in our terminal.
There is already a bash script that does this by using the QuizAPI and `jq`:
You can take a look at that script here:
* [https://github.com/QuizApi/QuizAPI-BASH/blob/master/quiz.sh](https://github.com/QuizApi/QuizAPI-BASH/blob/master/quiz.sh)
## Conclusion
The `jq` command-line tool is an amazing tool that gives you the power to work with JSON directly in your BASH terminal.
That way you can easily interact with all kinds of different REST APIs with BASH.
For more information, you could take a look at the official documentation here:
* [https://stedolan.github.io/jq/manual/](https://stedolan.github.io/jq/manual/)
And for more information on the **QuizAPI**, you could take a look at the official documentation here:
* [https://quizapi.io/docs/1.0/overview](https://quizapi.io/docs/1.0/overview)
>{notice} This content was initially posted on [DevDojo.com](https://devdojo.com/bobbyiliev/how-to-work-with-json-in-bash-using-jq)

View File

@@ -1,228 +0,0 @@
# Redirection in Bash
A Linux superuser must have a good knowledge of pipes and redirection in Bash. It is an essential component of the system and is often helpful in the field of Linux System Administration.
When you run a command like ``ls``, ``cat``, etc, you get some output on the terminal. If you write a wrong command, pass a wrong flag or a wrong command-line argument, you get error output on the terminal.
In both the cases, you are given some text. It may seem like "just text" to you, but the system treats this text differently. This identifier is known as a File Descriptor (fd).
In Linux, there are 3 File Descriptors, **STDIN** (0); **STDOUT** (1) and **STDERR** (2).
* **STDIN** (fd: 0): Manages the input in the terminal.
* **STDOUT** (fd: 1): Manages the output text in the terminal.
* **STDERR** (fd: 2): Manages the error text in the terminal.
# Difference between Pipes and Redirections
Both *pipes* and *redidertions* redirect streams `(file descriptor)` of process being executed. The main difference is that *redirections* deal with `files stream`, sending the output stream to a file or sending the content of a given file to the input stream of the process.
On the other hand a pipe connects two commands by sending the output stream of the first one to the input stream of the second one. without any redidertions specified.
# Redirection in Bash
## STDIN (Standard Input)
When you enter some input text for a command that asks for it, you are actually entering the text to the **STDIN** file descriptor. Run the ``cat`` command without any command-line arguments.
It may seem that the process has paused but in fact it's ``cat`` asking for **STDIN**. ``cat`` is a simple program and will print the text passed to **STDIN**. However, you can extend the use case by redirecting the input to the commands that take **STDIN**.
Example with ``cat``:
```
cat << EOF
Hello World!
How are you?
EOF
```
This will simply print the provided text on the terminal screen:
```
Hello World!
How are you?
```
The same can be done with other commands that take input via STDIN. Like, ``wc``:
```
wc -l << EOF
Hello World!
How are you?
EOF
```
The ``-l`` flag with ``wc`` counts the number of lines.
This block of bash code will print the number of lines to the terminal screen:
```
2
```
## STDOUT (Standard Output)
The normal non-error text on your terminal screen is printed via the **STDOUT** file descriptor. The **STDOUT** of a command can be redirected into a file, in such a way that the output of the command is written to a file instead of being printed on the terminal screen.
This is done simply with the help of ``>`` and ``>>`` operators.
Example:
```
echo "Hello World!" > file.txt
```
The above command will not print "Hello World" on the terminal screen, it will instead create a file called ``file.txt`` and will write the "Hello World" string to it.
This can be verified by running the ``cat`` command on the ``file.txt`` file.
```
cat file.txt
```
However, everytime you redirect the **STDOUT** of any command multiple times to the same file, it will remove the existing contents of the file to write the new ones.
Example:
```
echo "Hello World!" > file.txt
echo "How are you?" > file.txt
```
On running ``cat`` on ``file.txt`` file:
```
cat file.txt
```
You will only get the "How are you?" string printed.
```
How are you?
```
This is because the "Hello World" string has been overwritten.
This behaviour can be avoided using the ``>>`` operator.
The above example can be written as:
```
echo "Hello World!" > file.txt
echo "How are you?" >> file.txt
```
On running ``cat`` on the ``file.txt`` file, you will get the desired result.
```
Hello World!
How are you?
```
Alternatively, the redirection operator for **STDOUT** can also be written as ``1>``. Like,
```
echo "Hello World!" 1> file.txt
```
## STDERR (Standard Error)
The error text on the terminal screen is printed via the **STDERR** of the command. For example:
```
ls --hello
```
would give an error messages. This error message is the **STDERR** of the command.
**STDERR** can be redirected using the ``2>`` operator.
```
ls --hello 2> error.txt
```
This command will redirect the error message to the ``error.txt`` file and write it to it. This can be verified by running the ``cat`` command on the ``error.txt`` file.
You can also use the ``2>>`` operator for **STDERR** just like you used ``>>`` for **STDOUT**.
Error messages in Bash Scripts can be undesirable sometimes. You can choose to ignore them by redirecting the error message to the ``/dev/null`` file.
``/dev/null`` is pseudo-device that takes in text and then immediately discards it.
The above example can be written follows to ignore the error text completely:
```
ls --hello 2> /dev/null
```
Of course, you can redirect both **STDOUT** and **STDERR** for the same command or script.
```
./install_package.sh > output.txt 2> error.txt
```
Both of them can be redirected to the same file as well.
```
./install_package.sh > file.txt 2> file.txt
```
There is also a shorter way to do this.
```
./install_package.sh > file.txt 2>&1
```
# Piping
So far we have seen how to redirect the **STDOUT**, **STDIN** and **STDOUT** to and from a file.
To concatenate the output of program *(command)* as the input of another program *(command)* you can use a vertical bar `|`.
Example:
```
ls | grep ".txt"
```
This command will list the files in the current directory and pass output to *`grep`* command which then filter the output to only show the files that contain the string ".txt".
Syntax:
```
[time [-p]] [!] command1 [ | or |& command2 ] …
```
You can also build arbitrary chains of commands by piping them together to achieve a powerful result.
This example creates a listing of every user which owns a file in a given directory as well as how many files and directories they own:
```
ls -l /projects/bash_scripts | tail -n +2 | sed 's/\s\s*/ /g' | cut -d ' ' -f 3 | sort | uniq -c
```
Output:
```
8 anne
34 harry
37 tina
18 ryan
```
# HereDocument
The symbol `<<` can be used to create a temporary file [heredoc] and redirect from it at the command line.
```
COMMAND << EOF
ContentOfDocument
...
...
EOF
```
Note here that `EOF` represents the delimiter (end of file) of the heredoc. In fact, we can use any alphanumeric word in its place to signify the start and the end of the file. For instance, this is a valid heredoc:
```
cat << randomword1
This script will print these lines on the terminal.
Note that cat can read from standard input. Using this heredoc, we can
create a temporary file with these lines as it's content and pipe that
into cat.
randomword1
```
Effectively it will appear as if the contents of the heredoc are piped into the command. This can make the script very clean if multiple lines need to be piped into a program.
Further, we can attach more pipes as shown:
```
cat << randomword1 | wc
This script will print these lines on the terminal.
Note that cat can read from standard input. Using this heredoc, we can
create a temporary file with these lines as it's content and pipe that
into cat.
randomword1
```
Also, pre-defined variables can be used inside the heredocs.
# HereString
Herestrings are quite similar to heredocs but use `<<<`. These are used for single line strings that have to be piped into some program. This looks cleaner that heredocs as we don't have to specify the delimiter.
```
wc <<<"this is an easy way of passing strings to the stdin of a program (here wc)"
```
Just like heredocs, herestrings can contain variables.
## Summary
|**Operator** |**Description** |
|:---|:---|
|`>`|`Save output to a file`|
|`>>`|`Append output to a file`|
|`<`|`Read input from a file`|
|`2>`|`Redirect error messages`|
|`\|`|`Send the output from one program as input to another program`|
|`<<`|`Pipe multiple lines into a program cleanly`|
|`<<<`|`Pipe a single line into a program cleanly`|

View File

Before

Width:  |  Height:  |  Size: 118 KiB

After

Width:  |  Height:  |  Size: 118 KiB

View File

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

View File

Before

Width:  |  Height:  |  Size: 190 KiB

After

Width:  |  Height:  |  Size: 190 KiB

View File

@@ -0,0 +1,23 @@
# Bash Basics
To begin, create a new file with a `.sh` extension. We'll use `shinobi.sh` as our example.
You can create it instantly with `touch`:
```bash
touch shinobi.sh
```
Or, open it directly in your text editor:
```bash
nano shinobi.sh
```
For your script to execute, the very first line must specify the interpreter. This is called the Shebang. While `#!/bin/bash` instructs the operating system to use `/bin/bash`, its location can vary. For wider compatibility, use this:
```bash
#!/usr/bin/env bash
```
This tells the system to find `bash` within your `PATH` environment variable, ensuring your script runs consistently across different systems.

View File

@@ -0,0 +1,45 @@
# Hello World Bash
Let's build your first script. Create `shinobi.sh` and add the essential shebang, followed by your `Hello World` message:
```bash
touch shinobi.sh
```
Now, open `shinobi.sh` and add this content:
```bash
#!/usr/bin/env bash
echo "Hello World!"
```
Save and close the file.
Next, make your script executable:
```bash
chmod +x shinobi.sh
```
Execute your script directly:
```bash
./shinobi.sh
```
You'll see "Hello World!" printed.
Alternatively, you can run the script by explicitly calling the `bash` interpreter:
```bash
bash shinobi.sh
```
For quick tests, `echo` works directly in your terminal:
```bash
echo "Hello Shinobi!"
```
Scripts become powerful when combining multiple commands for automated tasks. This is your first step.

Some files were not shown because too many files have changed in this diff Show More