Basics of Bash scripting part one

0
1788
bin-bash

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


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 “This is my first script ”

this what the script should look like

echo is use for read only command you type echo ” hello world ” 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

now type ./myscript.sh

the output should be this my first script

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

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

# calulator 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”

save it to Desktop or were want

now lets run it

cd Desktop

chmod 755 myscript

./myscript

YUP SNOTS IT WORKS !!!!!!

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

Comments

comments