Shell script basics & Exercise 8,9,10 & 11

                                                                                                            

Linux shell scripting is a way to automate tasks and perform complex operations using the command-line interface (CLI) of a Linux system. Shell scripts are plain text files that contain a series of commands and instructions that are executed in a sequential manner. Here are some basics of Linux shell scripting:

  1. Choosing a Shell: There are several shells available in Linux, such as Bash (Bourne Again Shell), sh (Bourne Shell), csh (C Shell), and more. Bash is the most common and widely used shell, so we'll focus on it.

  2. Creating a Shell Script: To create a shell script, open a text editor and save the file with a .sh extension. For example, script.sh. Alternatively, you can use the touch command in the terminal to create an empty file.

  3. Shebang: The first line of a shell script is called the shebang line, and it specifies the shell to be used to interpret the script. For Bash scripts, the shebang line is #!/bin/bash.

  4. Comments: Use comments to make your script more understandable. Comments begin with the # character and are ignored by the shell. They are helpful for documenting your code. For example:

bash
#!/bin/bash # This is a comment echo "Hello, world!"
  1. Running a Shell Script: To run a shell script, you need to make it executable. Use the chmod command to add the execute permission. For example:
bash
chmod +x script.sh

Then, you can execute the script using ./script.sh or by specifying the full path to the script.

  1. Variables: You can define and use variables in shell scripts. Variables can store data and are referenced by their names. To assign a value to a variable, use the = operator. For example:
bash
#!/bin/bash name="John" echo "Hello, $name!"
  1. Command Substitution: You can capture the output of a command and assign it to a variable using command substitution. Command substitution is done by enclosing the command within $() or backticks. For example:
bash
#!/bin/bash current_date=$(date +%Y-%m-%d) echo "Today's date is $current_date"
  1. Input from User: You can read input from the user using the read command. It allows you to store user input into variables. For example:
bash
#!/bin/bash echo "Enter your name:" read name echo "Hello, $name!"
  1. Control Structures: Shell scripts support control structures like conditionals (if, elif, else) and loops (for, while) to control the flow of execution based on certain conditions or iterate over a set of values.

  2. Functions: Shell scripts can define functions to group a set of commands together and make the code more modular. Functions can take parameters and return values.

Shell scripts support various control structures to control the flow of execution based on certain conditions or iterate over a set of values. Here are some common control structures used in shell scripting, along with examples:
  1. if-else statement: The if-else statement allows you to execute different code blocks based on a condition. Here's an example:
bash
#!/bin/bash num=10 if [ $num -gt 0 ]; then echo "Number is positive" else echo "Number is zero or negative" fi
  1. if-elif-else statement: The if-elif-else statement allows you to check multiple conditions and execute different code blocks accordingly. Here's an example:
bash
#!/bin/bash num=5 if [ $num -gt 0 ]; then echo "Number is positive" elif [ $num -lt 0 ]; then echo "Number is negative" else echo "Number is zero" fi
  1. for loop: The for loop allows you to iterate over a list of values or elements. Here's an example that prints the numbers 1 to 5:
bash
#!/bin/bash for i in 1 2 3 4 5; do echo $i done

You can also use a range of numbers using the {start..end} syntax. For example, to print numbers 1 to 5:

bash
#!/bin/bash for i in {1..5}; do echo $i done
  1. while loop: The while loop repeatedly executes a set of commands as long as a condition is true. Here's an example that counts down from 5 to 1:
bash
#!/bin/bash count=5 while [ $count -gt 0 ]; do echo $count count=$((count - 1)) done
  1. until loop: The until loop is similar to the while loop but keeps executing the set of commands until a condition becomes true. Here's an example that counts from 1 to 5:
bash
#!/bin/bash count=1 until [ $count -gt 5 ]; do echo $count count=$((count + 1)) done

Functions in shell scripting allow you to group a set of commands together and make your code more modular. Here's an example of how to define and use functions in a shell script:

bash
#!/bin/bash # Function definition greet() { echo "Hello, $1!" } # Function call greet "John"

In the above example, we define a function named greet that takes one parameter. Within the function, we use the parameter $1 to reference the first argument passed to the function. When we call the function greet "John", it will print Hello, John! as the output.

You can define functions with multiple parameters as well. Here's an example:

bash
#!/bin/bash # Function definition add_numbers() { sum=$(( $1 + $2 )) echo "Sum: $sum" } # Function call add_numbers 5 10

In this example, the function add_numbers takes two parameters. We use $1 and $2 to access the first and second arguments, respectively. When we call the function add_numbers 5 10, it will calculate the sum and print Sum: 15.

Functions can also have a return value. You can use the return statement to specify the value to be returned. Here's an example:

bash
#!/bin/bash # Function definition multiply() { product=$(( $1 * $2 )) return $product } # Function call multiply 3 4 # Capture the return value result=$? echo "Product: $result"

In this example, the function multiply calculates the product of two numbers and uses the return statement to specify the value to be returned. After calling the function multiply 3 4, we capture the return value using $? and assign it to the variable result. Then, we print the value of result, which will be 12 in this case.

Functions can be used to encapsulate complex logic, reuse code, and improve the readability and maintainability of your shell scripts. You can call functions from different parts of your script as needed.

These are some of the common control structures in shell scripting. You can combine them and use them creatively to solve more complex problems or automate tasks in your shell scripts.


These basics should help you get started with Linux shell scripting. Shell scripting is a vast topic, and there are many more concepts and features you can explore as you delve deeper into it.


User

Post a Comment

0 Comments