Bash Scripting with read file line by line

Hacking Truth
0

Bash Scripting with make directory



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.
 



Bash Scripting with Add Numbers via Expr and let cmd

 

 

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.
 



Bash Scripting

 



 

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.
 



Bash Scripting

 





 

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.
 



Bash Scripting

 



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 video’s and tutorials are for informational and educational purposes only. We believe that ethical hacking, information security and cyber security should be familiar subjects to anyone using digital information and computers. We believe that it is impossible to defend yourself from hackers without knowing how hacking is done. The tutorials and videos provided on www.hackingtruth.in is only for those who are interested to learn about Ethical Hacking, Security, Penetration Testing and malware analysis. Hacking tutorials is against misuse of the information and we strongly suggest against it. Please regard the word hacking as ethical hacking or penetration testing every time this word is used.


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


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !