Saturday, March 2, 2019

BASICS OF BASH SCRIPTING

Bash aka (shell) is used mostly in the Unix and Linux systems this the commands we use Example ls list files in a directory or are mostly use cd changes the shells current working directory. Bash is great to write scripts with as it easy to learn and faster to script

so open your fav text editor and type the following line
#!/bin/sh

Hacking A Rise favtextediter

the first line tells Unix that the file is to be executed by /bin/sh this the standard location of the shell on just about every Unix/Linux system this includes mac if you use termux #!/data/data/com.termux/files/usr/bin/bash

the next line type # my first script

the # symbol is use with out the ! as this marks the line as a comment and its ignored by the shell

new line type echo “HACKING A RISE ”

Hacking A Rise ECHO

echo is use for read only command you type echo ” HACKING A RISE ” in terminal it will read whats in the brackets

so now we save the script so press Ctrl and s to save the script name it myscript.sh and save to root

now that its saved lets see does it work so open terminal and type chmod +x myscript.sh and press enter

Hacking A Rise chmod

now type ./myscript.sh

Hacking A Rise Hackingariseecho

the output should be this HACKING A RISE

congrats you just wrote a simple script look lets start adding some more lines

so now we are going to man a calculator in bash to be ran in terminal

here the commands/statement we are Using
1. echo
echo is one of the mostly used command.
It is used to print a line of text in standard output.

$ echo [-neE] [arg …]
2. read
The command read in the Linux is used to read the input from the keyboard.

3. Switch-Case
When there are a lot of if statement in Shell and it becomes confusing. Then it is good to use case statement.

4. bc Command
bc command is used for command line calculator. It is similar to basic calculator by using which we can do basic mathematical calculations. Arithmetic operations are the most basic in any kind of programming.

# !/bin/bash

# the Input
echo “Enter Two numbers : ”
read a
read b

Hacking A Rise INPUT

# Input type of operation
echo “Enter Choice :”
echo “1. Addition”
echo “2. Subtraction”
echo “3. Multiplication”
echo “4. Division”
read ch

Hacking A Rise input2

# calculator operations
case $ch in
1)res=`echo $a + $b | bc`
;;
2)res=`echo $a – $b | bc`
;;
3)res=`echo $a \* $b | bc`
;;
4)res=`echo “scale=2; $a / $b” | bc`
;;
esac
echo “Result : $res”

Hacking A Rise input3

Full script

Hacking A Rise fullscript

save it to Desktop or were want

now lets run it

cd Desktop

chmod 755 ./myscript.sh

./myscript.sh

Hacking A Rise chmod

YUP SNOTS IT WORKS !!!!!!

Hacking A Rise itwoks

Now u know a little bit of bash now u can start making simple scripts to automate your life in hacking

Related

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.