Shell Script to Read File Brief: This example will help you to read a file in a bash script. This tutorial contains two methods to read a file line by line using a shell script. Method 1 – Using simple loop You can use while read loop to read a file content line by line and store into a variable.
| #!/bin/bash # A shell script to read file line by line filename="/var/log/xyz.log" while read line do # $line variable contains current line read from the file # display $line text on the screen or do something with it. echo "$line" done < $filename |
Note – In above script line is a variable only. You can use any variable name in place of the line of your choice. Method 2 – Using IFS…
Read More Switch (Case) Statment in Bash The case statement is useful and processes faster than an else-if ladder. Instead of checking all if-else conditions, the case statement directly select the block to execute based on an input. Create the first program using the case statement in a shell script. The program takes input from the user and executes statements based on the input value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #!/bin/bash read -p "Enter your choice [yes/no]:" choice case $choice in yes) echo "Thank you" echo "Your type: Yes" ;; no) echo "Ooops" echo "You type: No" ;; *) echo "Sorry, invalid input" ;; esac |
Save the above script in case1.sh and execute the script on bash shell. chmod +x case1.sh ./case1.sh Enter your choice [yes/no]:yes Thank you Your type: Yes…
Read More Shell Script to Add Two Integers Brief: This example will help you to understand to add two numbers in the bash script. This script takes the input of two numbers from the user and prints the sum of both numbers. This is an example script initializes two variables with numeric values. Then perform an addition operation on both values and store results in the third variable.
| #!/bin/bash # Calculate the sum of two integers with pre initialize values # in a shell script a=10 b=20 sum=$(( $a + $b )) echo $sum |
Command Line Arguments In this second example, shell script reads two numbers as command line parameters and perform the addition operation.
| #!/bin/bash # Calculate the sum via command line arguments # $1 and $2 refers to the first and second argument passed as command line arguments sum=$(( $1 + $2 )) echo "Sum is: $sum" |
Output:…
Read More Include Files in Bash Similar to other programming languages which allow to include other files to a file, Bash scripting also allows to include (source) another shell script file to script file. For example, to include config.sh script to current script file use following syntax, where config.sh is available in the same directory of the current script.
| #!/bin/bash source other-file.sh |
Include Shell Script in Other Shell Script For this example, First, I have created a data.sh file with the following content.
| #!/bin/bash id=100 name="TecAdmin" |
Now create another file show.sh, which includes data.sh file.
| #!/bin/bash source data.sh echo "Welcome $name" echo "Your id is $id" |
…
Read More User Input In Bash Linux read command is used for interactive user input in bash scripts. This is helpful for taking input from users at runtime. Syntax: read [options] variable_name Example 1: There is no need to specify any datatype for the variables with the read operation. This simply takes an input of any data type values. Try with a sample command. Open bash shell terminal and type following command. read myvar The above command will wait for user input. so just type anything and press enter. Let’s create a…
Read More Command Line Arguments in Shell Script Command line arguments are also known as positional parameters. These arguments are specific with the shell script on terminal during the run time. Each variable passed to a shell script at command line are stored in corresponding shell variables including the shell script name. Synatx: ./myscript.sh ARG1 ARG2 ARG3 ARG4 ARG5 ARG6 ARG7 ARG8 ARG9 ARG10 See the below image to understand the command line values and variables. Here ARG1, ARG2 to ARG10 are command line values, which is assigned to corresponding shell variables.…
Read More