Project
5 min read

Linux Command Line

1 - Hello Bash Scripting

kali ) -C —'Desktop/projects] /etc/shells # /etc/shells: valid login shells /bin/sh /bin/bash /usr/bin/bash /bin/rbash /usr/bin/rbash /bin/dash /usr/bin/dash /usr/bin/tmux /bin/zsh /usr/bin/zsh /usr/bin/zsh /usr/bin/pwsh / opt/microsoft/powersheII/7 /pwsh /usr/bin/screen

What this command does is shows all the available shells on system. Any can be used.

kali)- which bash /usr/bin/bash —/Desktop/projects]

Shows path of bash. Add this to any script that is written

kali —'Desktop/ projects] heUoscript. sh /usr/bin/bash echo "hello bash script"
(kali@ —'Desktop/ projects] drwxr-xr-x 2 drwxr-xr-x 4 11:18 total 12 -rw-r—r kali kali 1 kali kali 4096 kali 4096 kali 42 May May May 5 15:07 5 15:08 helloscript.sh

In order to make script executable, have to add chmod +x to it.

—'Desktop/projects] L-$ chmod heUoscript.sh (kali@ —'Desktop/projects] helloscript. sh

./ in front to run script

(kali@ —'Desktop/projects] $ ./helloscript.sh hello bash script

2 - Redirect to file

Show how to capture output and then send it to a file. 

home > kali > Desktop > Projects > S helloscript.sh 2 3 /usr/bin/bash echo "hello bash script" > file. tx

When ran, creates a file called file.txt

(kali@ —'Desktop/projects] $ ./helloscript.sh (kali@ —'Desktop/projects] file . txt helloscript.sh —'Desktop/projects] file. txt hello bash script

 

home > kali > Desktop > Projects > S helloscript.sh 2 3 /usr/bin/bash cat > file. txt

Script becomes an editor. Whatever is written when script is executed

 

(kali@ —'Desktop/projects] —$ ./helloscript.sh lello, this is a shell overview, I am Jelani. Welcome

 

In order to come out of it, you have to press Ctrl + D.

Output of one displays output of another. Essentially replaces text.

kali@kali: —'Desktop/Projects File Actions Edit View Help (kali@ —'Desktop/projects] $ ./helloscript.sh hello bash script —'Desktop/projects] helloscript.sh /usr/bin/bash echo "hello bash script" (kali@ —'Desktop/projects] $ ./helloscript.sh (kali@ —/Desktop/projects] file . txt helloscript.sh —/Desktop/projects] file. txt hello bash script (kali@ —'Desktop/projects] $ ./helloscript.sh Hello, this is a shell overview, I am Jelani. (kali@ —/Desktop/projects] file.txt - Visual Studio Code File Edit Selection View Go Run Terminal Help Restricted Mode is intended for safe code browsing. Trust this window to enable all features. Man.ge Leam More $ helloscript.sh file.txt X home > kali > Desktop > Projects > file.txt Hello, this is a shell overview, Welcome @Restricted Mode @0A0 I am Jelani. Ln 1, coli Welcome Spaces: 4 I_JTF -8 LF Plain Text

 

In order to append, put 2 >>

 

home > kali > Desktop > Projects > S helloscript.sh 2 3 /usr/bin/bash cat file. txt

 

(kali@ —'Desktop/projects] $ ./helloscript.sh This shows how to append to a file $ helloscript.sh file.txt home > kali > Desktop > Projects > file.txt 2 Hello, this is a shell overview, I am Jelani. . This shows how to append to a file Welcome

 

3 - comments

 

Has no value except explaining code to programmer.

home > kali > Desktop > Projects > S helloscript.sh 2 3 4 /usr/bin/bash #this is a cat command cat >> file. txt

 

(kali@ —'Desktop/projects] $ ./helloscript.sh

 

Doesn't show in execution of code. If there are 500 lines of code it does not make to out # on each individual line

 

#this is a cat •ommand : 'this this this this this this this this this nd is a cat comma a a a a a a a a cat cat cat cat cat cat cat cat command command comma n command command command command command cat >> file. txt

 

:'

' to multi line comment

 

Heredoc delimeter

 

2 3 4 5 6 7 8 9 /usr/bin/bash #this is a cat command cat << 8reati this is hello text add another line Kreatvg

 

(kali@ —'Desktop/projects] $ ./helloscript.sh this is hello text add another line

 

Similar to the variable like

 

4 - Conditional statements

 

2 3 4 5 6 7 8 9 /usr/bin/bash count—IO if $count -eq 10 t hen echo "the condition is true" fi

 

Inside if condition, checks that count equals 10. Since condition is true, execute the block.

 

(kali@ —'Desktop/projects] $ ./helloscript.sh the condition is true

 

2 3 4 5 6 7 8 9 /usr/bin/bash count—IO if $count -eq 5 j] t hen echo "the condition is true" fi

 

No output, since no else block and program doesn’t know what to do.

 

2 3 4 5 6 7 8 9 1€ 11 12 /usr/bin/bash count—IO if [ $count -eq 5 ] t hen echo else echo fi "the condition is true" "the condition is false "l

 

(kali@ —'Desktop/projects] $ ./helloscript.sh the condition is false

 

2 3 4 5 6 7 8 9 10 11 12 /usr/bin/bash count—IO if (( Scount > 9 )) t hen echo else echo "the condition is true" "the condition is false"

 

count—IO if (( Scount < 9 )) t hen echo "the condition is true" elif (($count t hen echo else echo "the condition is true" "the condition is false"

 

AND Operator

 

Checks if age is greater than 18 and less than 40

 

2 3 4 5 6 7 8 9 12 13 /usr/bin/bash age—IO if [ "$age" -gt 18 ] t hen echo "age is correct" else echo fi " Sage " -It 4€ ] "age is not correct"

 

(kali@ —'Desktop/projects] $ ./helloscript.sh age is not correct

 

Both conditions have to be true

 

home > kali > Desktop > Projects > /usr/bin/bash 2 3 4 5 6 7 8 9 10 11 12 13 14 age—IO if [ "$age" t hen echo else echo -It 18 S helloscript.sh " Sage " -gt 46 ] "age is correct" "age is not correct"

 

One true condition, one false condition

(kali@ —'Desktop/projects] $ ./helloscript.sh age is not correct

 

 

2 3 4 5 6 7 8 9 1€ 11 12 13 14 /usr/bin/bash age—IO if [ "$age" -It 18 -o "$age" t hen echo "age is correct" else echo "age is not correct" fi -gt 40 ]

 

-o for OR operator . -o can also be substituted with ||

(kali@ —'Desktop/projects] $ ./helloscript.sh age is correct

 

5 - Loops

 

While:

2 3 4 5 6 7 8 9 /usr/bin/bash number—jl while [ $number -It 10 ] do echo "$number" ($number+,Ä ) ) done

 

(kali@ —'Desktop/projects] $ ./helloscript.sh

 

For:

 

(kali@ —'Desktop/projects] $ ./helloscript.sh S helloscript.sh X file.txt home > kali > Desktop > Projects > S helloscript.sh 2 3 4 5 6 /usr/bin/bash for i in O. .1$ do echo $i done

 

home > kali > Desktop > Projects > $ helloscript.sh (kali@ —'Desktop/projects] $ ./helloscript.sh (kali@ —/Desktop/projects] 2 3 4 5 /usr/bin/bash for ((i=€; i<5 ; i++)) do echo $i

 

home > kali > Desktop > Projects > S helloscript.sh 2 3 4 5 6 7 8 9 10 /usr/bin/bash for ((i=€; i<10 ; i")) do if $i -gt 7 then break echo $i done

 

(kali@ —'Desktop/projects] $ ./helloscript.sh (kali@ —/Desktop/projects] S helloscript.sh X file.txt home > kali > Desktop > Projects > S helloscript.sh 2 3 4 5 6 7 8 9 1€ /usr/bin/bash for ((i=€; i<10 ; i")) do if [ $i -gt 7 ] then break echo $i one

 

6 - Script Input

 Input represents $1, $2, $3

home > kali > Desktop > Projects > $ helloscript.sh (kali@ —'Desktop/projects] $ ./helloscript.sh BMW Mercedes Toyota BMW Mercedes Toyota (kali@ —/Desktop/projects] 2 3 /usr/bin/bash echo $1 $2 $3

 

Create an array with unknown number of inputs

(kali@ —'Desktop/projects] $ ./helloscript.sh BMW Porsche Mercedes BMW Porsche Mercedes (kali@ —/Desktop/projects] 2 3 4 5 /usr/bin/bash echo

 

home > kali > Desktop > Projects > S helloscript.sh 2 3 4 5 /usr/bin/bash echo $@

 

home > kali > Desktop > Projects > $ helloscript.sh (kali@ —'Desktop/projects] $ ./helloscript.sh BMW Porsche Mercedes BMW Porsche Mercedes Hyundai Kia (kali@ —/Desktop/projects] Hyundai Kia 2 3 4 5 /usr/bin/bash echo $4

Prints all the values that are taken in.

 

(kali@ —'Desktop/projects] $ ./helloscript.sh BMW Porsche Mercedes BMW Porsche Mercedes Hyundai Kia (kali@ —/Desktop/projects] Hyundai Kia S helloscript.sh X file.txt home > kali > Desktop > Projects > S helloscript.sh 2 3 4 5 6 7 /usr/bin/bash echo $@ echo $#

Prints length of array as well

6 Script Input

 

helloscript.sh - Visual Studio Code File Edit Selection View Go Run Terminal Help Restricted Mode is intended for safe code browsing. Trust this window to enable all fei S helloscript.sh X file.txt home > kali > Desktop > Projects > S helloscript.sh kali@kali: —'Desktop/Projects File Actions Edit View Help (kali@ —/Desktop/projectsJ $ ./helloscript.sh linuxconmands Hello Bash Scripting Redirect to file Comments 2 3 4 5 6 7 @Restricted Mode /usr/bin/bash while read line do echo "$line" done < "${1:- /dev/stdin}" @0A0 4- 8- 10 11- 12 14- 15- 16 17- 18- 19- 20- 21- 22 Ln7,C0127 Conditional Statements Loops Script Input Script Output Pipes Strings Processing Numbers and Arithmetic Declare Command Arrays Functions Files and Directories Sending email via script Curl in scripts Professional menus Wait for filesystem events with inotify Introduction to grep Introduction to awk Introduction to sed Debugging Bash Scripts

 

7 Script Output

S helloscript.sh X file.txt home > kali > Desktop > Projects > 2 3 Is /usr/bin/bash -al l>filel.txt S helloscript.sh 2>file2. txt (kali@ "Desktop/projects] $ ./helloscript.sh (kali@ —'Desktop/projects] filel . txt file2 . txt file . txt helloscript.sh (kali@ —'Desktop/projects] Iinuxcommands

 

1 file for standard output and 1 file for standard error

home 2 3 > kali > Desktop > Projects > /usr/bin/bash Is l>filel. txt S helloscript.sh (kali@ —'Desktop/projects] $ ./helloscript.sh 2>file2. txt —'Desktop/projects] f nel. txt —'Desktop/projects] file2. txt Is: cannot access No such file or directory (kali@ —'Desktop/projects]

8 Sending Output

—'Desktop/projects] touch secondscript .sh —'Desktop/projects] L-$ chmod secondscript.sh (kali@ —/Desktop/projects] filel . txt file. txt Iinuxcommands file2 . txt helloscript.sh secondscript.sh

 

home > kali > Desktop > Projects > S secondscript.sh 1 #! / usr/bin/bash 2 3 echo "The message from hell*cript is: $MESSAGE"

 

home > kali > Desktop > Projects > S helloscript.sh 2 3 4 5 /usr/bin/bash MESSAGE; "Hello Reader" export MESSAGE ./secondscript . sh

 

(kali@ —'Desktop/projects] $ ./helloscript.sh The message from helloscript is: Hello Reader (kali@ —/Desktop/projects]

 

Output from one script is being sent to another

 

9 Strings Processing

 

home > kali > Desktop > Projects > S helloscript.sh /usr/bin/bash 2 3 4 5 6 7 8 9 10 11 12 13 14 15 echo read echo read t hen "'enter 1st string" stl "'enter 2nd string" st2 " l"$st2" ) "$stl echo else echo fi "The strings match" "Strings dont match"

 

(kali@ —'Desktop/ projects] $ ./helloscript.sh enter 1st string hello enter 2nd string bye Strings dont match

 

Compares 2 inputs to see if they match

 

10 Numbers and Arithmetic

 

To add numbers:

S helloscript.sh • $ secondscript.sh home > kali > Desktop > Projects > S helloscript.sh /usr/bin/bash 2 3 4 5 echo $(( ril +n2

 

(kali@ —'Desktop/projects] $ ./helloscript.sh

 

home > kali > Desktop > Projects > /usr/bin/bash n1=4 $(( nl S helloscript.sh 2 3 4 5 6 7 8 echo echo echo echo

 

(kali@ —'Desktop/projects] $ ./helloscript.sh

Or can use expression

 

me > kali > Desktop > Projects > /usr/bin/bash n1=4 helloscript.sh 2 3 4 5 6 7 echo $(expr $nl + (kali@ —'Desktop/projects] $ ./helloscript.sh (kali@ —'Desktop/projects]

 

 

 

11 Declare Command

Bash does not have a strong type system. Cannot restrict variables, however you can use attributes set by a command. Declare is a bash built in command that allows you to update attributes applied to variables within scope of shell.

Declare -p shows ALL variables

 

declare myvariab1e=8 echo $myvariable (kali@

 

2 3 4 5 6 7 /usr/bin/bash declare -r pwdfile=/etc/passwd echo "$pwdfile" pwdfile=/etc/abc . txt L$ cd projects (kali@ —'Desktop/ projects] $ ./helloscript.sh /etc/passwd (kali@ —'Desktop/ projects] $ ./helloscript.sh /etc/passwd ./helloscript.sh: (kali@ line 7: pwdfile: readonly variable —'Desktop/ projects]

 

Can see that it is trying to print out twice. This is because pwd file is being declared as a read variable

12 Arrays

/usr/bin/bash 2 3 4 5 • 'Honda echo 'BMW' 'Toyota "${car (kali@ —'Desktop/projects] $ ./helloscript.sh BMW Toyota Honda (kali@ —/Desktop/projects]

Prints out all of the arrays

 

home > kali > Desktop > Projects > 2 3 4 5 6 7 8 9 10 11 12 /usr/bin/bash 'BMW' 'Toyota' #prints all indexes echo "${car #shows what lies in echo #position of index echo ! S helloscript.sh ' Honda ' ) index 1

 

(kali@ —'Desktop/projects] $ ./helloscript.sh BMW Toyota Honda Toyota 012

 

2 3 4 5 6 7 8 9 10 11 12 13 14 15 /usr/bin/bash 'BMW' 'Toyota' #prints all indexes echo "${car ' Honda ' ) #shows what lies in index 1 echo #position of index echo ! #number of index echo •

 

13 Functions

In order to make functions work, they need to be called

home > kali > Desktop > Projects > S helloscript.sh 2 3 4 5 6 7 8 9 /usr/bin/bash function funcname() echo "this is new function" funcname

 

(kali@ —'Desktop/projects] $ ./helloscript.sh this is new function (kali@ —/Desktop/projects]

 

14 Files and Directories

Learn how to create directories/check if current directories exist or not, how to append text from file and how to read line by line

 

home > kali > Desktop > Projects > S helloscript.sh 2 3 /usr/bin/bash mkdir -p testfolder

 

(kali@ —/Desktop/projects] filel . txt file2 . txt file . txt helloscript.sh (kali@ —/Desktop/projects] Iinuxcommands secondscript. sh testfolder

 

home > kali > Desktop > Projects > /usr/bin/bash S helloscript.sh 2 3 4 5 6 7 8 9 10 12 13 echo "Enter Directory name to check" read direct if [ -d "$direct" ] t hen echo else echo fi "directory exists" "directory doesnt exist"

 

filel . txt file2 . txt file . txt helloscript.sh (kali@ —'Desktop/projects] $ ./helloscript.sh Enter Directory name to check testfolder directory exists (kali@ —'Desktop/projects] $ ./helloscript.sh Enter Directory name to check jkl directory doesnt exist Iinuxcommands secondscript. sh testfolder

 

 

home > kali > Desktop > Projects > /usr/bin/bash S helloscript.sh 2 3 4 5 6 7 echo "Enter filename to create" read filename touch $filename

Creating a filename

(kali@ —'Desktop/projects] $ ./helloscript.sh Enter filename to create testedfile (kali@ —/Desktop/projects] filel . txt file. txt Iinuxcommands file2 . txt helloscript.sh secondscript.sh testedfile testfolder

 

S helloscript.shX $ secondscript.sh home > kali > Desktop > Projects > S helloscript.sh 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 /usr/bin/bash echo "Enter filename to create" read filename touch $filename echo "enter filename to check" read filename if [ [ -f t hen echo else echo fi "$filename" ] ] "$filename exists" "$filename doesnt exist"

 

(kali@ —'Desktop/projects] $ ./helloscript.sh Enter filename to create jelanitest enter filename to check jelanitest jelanitest exists

 

15 Curl in scripts

 

2 3 4 /usr/bin/bash u rl "http://'.•m•l.ovh . net/files/1Mio . dat" curl ${url} -O

 

16 Professional menus

Select loops and how to work with it

 

S helloscript.shX $ secondscript.sh home > kali > Desktop > Projects > S helloscript.sh /usr/bin/bash Rover Toyota ./helloscript .sh: line 6: syntax error: (kali@ —'Desktop/projects] $ ./helloscript.sh 2) Mercedes 3) Tesla 4) Rover 5) Toyota You have selected Tesla You have selected Toyota You have selected BMW You have selected unexpected end of fi 2 3 4 5 6 select car in do echo "You done Mercedes Tesla have selected $car"

Needs a check statement.

 

home > kali > Desktop > Projects > S helloscript.sh 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2€ /usr/bin/bash select car in Mercedes Tesla Rover Toyota do case $car in BMW) echo Me rcedes ) echo Tesla) echo Rover) echo Toyota ) echo echo esac pel "BMW Selected"; ; "Mercedes Selected" , "Tesla Selected"; , "Rover Selected"; , "Toyota Selected" ; , "ERROR! Please choose the correct option"

 

(kali@ —'Desktop/projects] $ ./helloscript.sh 1) BMW 2) Mercedes 3) Tesla 4) Rover 5) Toyota 89 ERROR! Please choose the correct option

 

17 Wait for filesystem events with inotify

Inotify is a linux kernel subsystem that acts to extend file systems. Whatever changes are made inotify will monitor the files and directories

 

(kali@ —'Desktop/projects] $ ./helloscript.sh Setting up watches. Couldn't watch /temp/newfolder: No such file or directory (kali@ —/Desktop/projects]

 

(kali@ —'Desktop/ projects J $ ./helloscript.sh Setting up watches. Watches established. 2 3 4 /usr/bin/bash sudo mkdir -p /newfolder inotifywait -m /newfolder

 

18 Introduction to grep

 

$ helloscript.sh filegrep.txtx $ secondscript.sh home > kali > Desktop > Projects > filegrep.txt 2 3 This is Linux This is windows This is

 

S helloscript.sh X filegrep.txt $ secondscript.sh home > kali > Desktop > Projects > S helloscript.sh 2 3 4 5 6 7 8 9 1€ 11 12 13 14 15 /usr/bin/bash echo "Please enter the file to search text from: " read filename if [ [ -f t hen echo read g rep else echo fi $filename ] ] "enter text to search" grepvar -i $grepvar $filename "file doesnt exist"

 

19 Introduction to awk

Awk - scripting language used to manipulate data and generate reports

 

home > kali > Desktop > Projects > S helloscript.sh 2 3 4 5 6 7 8 9 10 11 12 /usr/bin/bash echo "Please enter the file to print from awk" read filename if [ [ -f $filename J] t hen awk else echo /windows/ {print}' "file doesnt exist" $filename

 

(kali@ —'Desktop/projects] $ ./helloscript.sh Please enter the file to print from awk filegrep . txt This is windows

 

(kali@ —'Desktop/projects] $ ./helloscript.sh Please enter the file to print from awk filegrep . txt

 

home > kali > Desktop > Projects > S helloscript.sh 2 3 4 5 6 7 8 9 10 11 12 /usr/bin/bash echo "Please enter the file to print from awk" read filename if [ [ -f $filename J] t hen awk else echo fi ' /windows/ {print $21} "file doesnt exist" $filename

 

20 Introduction to sed

Sed stands for stream editor and can be used to manipulate text files

(kali@ —'Desktop/projects] $ ./helloscript.sh Please enter the filename to substitute using sed filegrep . txt Thls is Linux Thls is windows Thls is mac

 

(kali@ —'Desktop/projects] $ ./helloscript.sh Please enter the filename to substitute using sed filegrep . txt This Is Linux Thls Is wlndows Thls Is mac

 

21 - Debugging Bash Scripts

 

Bash offers an extensive debugging facility so you know if it doesn’t go according to plan. The error includes the line and where the error is

 

Bash -x shows you the code that is being executed.

Previous

network security and python software engineer

Next

Linux Challenge 1