Bash Scripting
In Linux, all tasks from execution of services to loading and
unloading of modules are carried out by programs and all programs need to be
executed. You use the commands to access all the basic features of kernel.
Shell scripting is a way to automate such tasks, and bash is one of the
language, that has capabilities enough to be called as scripting as well as a
language that can be used for programming on the POSIX platform, for small
tasks. Bash Scripting with read file line by line
Read a File:
You can read any file line by line in bash by using loop. Create a file named, ‘readfile1.sh’ and add the following code to read an existing file named, ‘bookbash.txt’.
Bash Scripting with Make directory
#!/bin/bash
file='bookbash.txt'
while read line; do
echo $line
done < $file
Run the file with bash command.
Example -2: Reading file content from command line:
Suppose, you want to read the file, company.txt, line by line from the command line without ‘cat’ command. Run the following command to do the task. while loop will read each line from the file bookbash2.txt in each step and store the content of the line in $line variable which will be printed later.
while read line; do echo $line; done < bookbash2.txt"
fi
Run the file with bash command.
Example -3: Reading file content using script:
Create a bash file and add the following code to read the content of a particular file. Here, an existing filename is stored in $filename variable and $n variable is used to keep the value of the line number of that file. Like previous example, while loop is used to read this file with line number.
#!/bin/bash
filename='bookbash2.txt'
n=2 do
while read line; do
# reading each line
echo "Line No. $n : $line"
n=$((n+1))
done < $filename
Run the file with bash command.
Example -4: Passing filename from the command line and reading the file:
Create a bash file and add the following script. This script will take the filename from the command line argument. First argument value is read by the variable $1 which will contain the filename for reading. If the file exists in the current location then while loop will read the file line by line like previous example and print the file content.
#!/bin/bash
filename=$1
while read line; do
# reading each line
echo $line
done < $filename
Run the file with bash command.
I hope you liked this post, then you should not forget to share this post at
all.
Thank you so much :-)
Disclaimer
This was written for educational purpose and pentest only.
The
author will not be responsible for any damage ..!
The author of this tool
is not responsible for any misuse of the information.
You will not misuse
the information to gain unauthorized access.
This information shall only
be used to expand knowledge and not for causing malicious or damaging
attacks. Performing any hacks without written permission is illegal ..!
All tutorials and videos have been made using our own routers, servers, websites and other resources, they do not contain any illegal activity. We do not promote, encourage, support or excite any illegal activity or hacking without written permission in general. We want to raise security awareness and inform our readers on how to prevent themselves from being a victim of hackers. If you plan to use the information for illegal purposes, please leave this website now. We cannot be held responsible for any misuse of the given information.
- Hacking Truth by Kumar Atul Jaiswal