Bash Scripting Get Arguments from Command Line

Hacking Truth
0

Bash Scripting Get Arguments from Command Line



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 else if statement

 

Get Arguments from Command Line

Bash script can read input from command line argument like other programming language. For example, $1 and $2 variable are used to read first and second command line arguments. Create a file named “argument.sh” and add the following script. Two argument values read by the following script and prints the total number of arguments and the argument values as output. Bash Scripting Get Arguments from Command Line


#!/bin/bash
echo "Total arguments : $#"
recho "1st argument = $1"
echo "2nd Argument = $2"
echo "3rd argument = $3"
echo "4th Argument = $4"
echo "5th argument = $5"
echo "6th Argument = $6"
echo "7th argument = $7"
echo "8th Argument = $8"
echo "9th argument = $9"
echo







Run the file with bash command.
 





 


Reading arguments by getopts function

Create a bash file and add the following script to understand the use of getopts function. ‘getopts’ function is used with while loop to read command line argument options and argument values. Here, 4 options are used which are ‘i’, ‘n’, ‘m’ and ‘e’. case statement is used to match the particular option and store the argument value in a variable. Finally, print the values of the variable.


#!/bin/bash
while getopts ":i:n:m:e:" arg;
do
case $arg in
i) ID=$OPTARG;;
n) Name=$OPTARG;;
m) Manufacturing_data=$OPTARG;;
e) Expire_data=$OPTARG;;
esac
done
echo -e "\n$ID $Name $Manufacturing_data $Expire_data\n"
echo







Run the file with bash command.

 

bash arg2.sh -i pOO1 -n 'Fafda n Jalebi' -m '05-02-2000' -e '05-02-2025'
 










Taking filename as argument

Create a bash file and add the following code to count the total number of characters of any file. Here, filename will be passed as command line argument.


#!/bin/bash

filename=$1
totalchar=`wc -c $filename`
echo "Total number of characters are $totalchar"
echo




Run the bash script with the filename as single argument value and run another command to check the total number of characters of that file. Here, student.txt file is used as argument value. Total number of characters of student.txt file is 100p>


Run the file with bash command.

bash arg1.sh student.txt 

wc -c student.txt



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 !