Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rathinadev committed Dec 8, 2023
0 parents commit 796be2c
Show file tree
Hide file tree
Showing 48 changed files with 566 additions and 0 deletions.
25 changes: 25 additions & 0 deletions 24_functions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash


function welcomeNote {

echo "......................."
echo "Welcome"
echo "......................."
}


Welcome(){
echo "......................."
echo "Welcome"
echo "......................."

}

#to Call our function

welcomeNote
welcomeNote
welcomeNote
Welcome

11 changes: 11 additions & 0 deletions 25_functions_with_args.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

function welcomenote {
echo "......................."
echo "Welcome $1"
echo "Age is $2"
echo "......................."
}

welcomenote ratu 20
welcomenote pradhul 30
17 changes: 17 additions & 0 deletions 26_args.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

set -x
#to access the arguments

echo "First argument is $1"
echo "Second argument is $2"

echo "All the arguments are - $@"
echo "Number of arguments are - $#"


#For loop to access the values from arguments
for filename in $@
do
echo "Copying file - $filename"
done
9 changes: 9 additions & 0 deletions 27_shift_arguments.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

# to create a user, provide username and description

echo "Creating user...."
echo "Username is $1"

shift
echo "Description is $@"
17 changes: 17 additions & 0 deletions 28_break.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

#example fo break in a lopp
#we just need to confirm if a given no. if present or not

no=6

for i in 1 2 3 4 5 6 7 8 9
do
#break the loop if no. found
if [[ $no -eq $i ]]
then
echo "$no is found!!!"
break
fi
echo "Number is $i"
done
14 changes: 14 additions & 0 deletions 29_continue.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

#example of using continue in a loop
# Suppose we only need to print odd no.

for i in 1 2 3 4 5 6 7 8 9 10
do
let r=$i%2
if [[ $r -eq 0 ]]
then
continue
fi
echo "odd no. is $i"
done
13 changes: 13 additions & 0 deletions 30_connectivity_check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

read -p "Which site you want to check? " site

ping -c 1 $site &> /dev/null
#sleep 5s

if [[ $? -eq 0 ]]
then
echo "Successfuly connected to $site."
else
echo "Unable to connect with $site."
fi
13 changes: 13 additions & 0 deletions 31_file_exist_check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

FILEPATH="/home/ratu/Desktop/programs/bash/practice/ratu.test"


if [[ -f $FILEPATH ]]
then
echo "File exist"
else
echo "Creating file now..."
touch $FILEPATH
fi

7 changes: 7 additions & 0 deletions 32_dice.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash


#generating a random number between 1 to 6

no=$(( $RANDOM % 6 + 1 ))
echo "Random Number is $no."
10 changes: 10 additions & 0 deletions 33_root_user_check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

# Checking if the user is root or not

if [[ $UID -eq 0 ]]
then
echo "User is root"
else
echo "User is not root"
fi
3 changes: 3 additions & 0 deletions 34_redirect.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

ping -c 1 www.google.com > redirect.log
3 changes: 3 additions & 0 deletions 35_script_name.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

echo "Name of this script is ${0}"
5 changes: 5 additions & 0 deletions 36_logger.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

# Example of logging

logger " This is a log from ${0}"
10 changes: 10 additions & 0 deletions 37_debugging.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

#when set -e is there then the script is stopped when a command fails
set -e

pwd
date
cd /root
hostname

3 changes: 3 additions & 0 deletions Basic_operations/01_basic.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

echo "HEY buddy!"
11 changes: 11 additions & 0 deletions Basic_operations/02_comments.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

echo "checking comments"

#this is a single line comment

<<comment
this
is a multiline
COMMENT
comment
12 changes: 12 additions & 0 deletions Basic_operations/03_var.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash


a=10
name="ratu"
age=18

echo "My name is $name and age is $age"

name="paul"

echo "my name is $(hostname)"
10 changes: 10 additions & 0 deletions Basic_operations/04_read_only_var.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

readonly variable="svce"

echo $variable


#read only variable cannot be initiated with a new value
variable="rec"

19 changes: 19 additions & 0 deletions Basic_operations/05_array.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

myarray=( 1 20 30.5 Hello "HEY buddy!" )


echo"all the values in array are ${myarray[*]}"
echo "${myarray[2]}"

#how to fin no. of avlues in an array

echo "NO.of values, length of an array is ${#myarray[*]}"

echo "values form index 2-3 ${myarray[*]:2:2}"


#updating our array withh new values
myarray+=( new apple 30 )

echo "values of new array are ${myarray[*]}"
11 changes: 11 additions & 0 deletions Basic_operations/06_keyvalue.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

#how to store the key value pairs
#declaring a array
declare -A myarray

myarray=( [name]=ratu [age]=18 [city]=chennai )

echo "Name is ${myarray[name]}"

echo "Age is ${myarray[age]}"
17 changes: 17 additions & 0 deletions Basic_operations/07_string_operations.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

myvar="Hey buddy, how are you?"

myvarlength=${#myvar}
echo "Length of the myvar is $myvarlength"

echo "Upper case is ---${myvar^^}"
echo "Lower case is ---${myvar,,}"

#To replace a string here replace 1st term by 2nd one
newvar=${myvar/buddy/ratu}
echo "NEW VAR is ---${newvar}"

#To slice a string :1st number is starting point :2nd number is the count of words to slice

echo "After slice ${myvar:4:5}"
6 changes: 6 additions & 0 deletions Basic_operations/08_user_interaction.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash


read -p "What is your name?" name

echo "Your name is $name"
23 changes: 23 additions & 0 deletions Basic_operations/09_arithmetic_operation.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

#Maths Calculation
x=10
y=2


#won't work
mul=$x*$y
echo "The multiplied value is :$mul"


#should use let command to work

let mul=$x*$y
echo "the multiplied value is :$mul"

let sum=$x+$y
echo "the sum is $sum"

#There is another version where we can also use double brackets to get the action of let command

echo "subtraction is $(($x-$y))"
10 changes: 10 additions & 0 deletions Basic_operations/10_if_else.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

read -p "Enter your marks: " marks

if [[ $marks -gt 40 ]]
then
echo "Your are PASS."
else
echo "You are FAIL!!"
fi
16 changes: 16 additions & 0 deletions Basic_operations/11_elif_demo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

read -p "Enter your marks: " marks

if [[ $marks -ge 80 ]]
then
echo "1st Division."
elif [[ $marks -ge 60 ]]
then
echo "2nd Division"
elif [[ $marks -ge 40 ]]
then
echo "3rd Division"
else
echo "You are FAIL!!"
fi
15 changes: 15 additions & 0 deletions Basic_operations/12_case_demo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

echo "Provide an option"
echo "a for Print date"
echo "b for list of scripts"
echo "c to check the current location"

read choice

case $choice in
a)date;;
b)ls;;
c)pwd;;
*)echo "Kindly provide a valid input."
esac
13 changes: 13 additions & 0 deletions Basic_operations/13_operators.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

#AND OPERATOR

read -p "What is your age?" age
read -p "Your country: " country

if [[ $age -ge 18 ]] || [[ $country == "india" ]]
then
echo "You can vote"
else
echo "You can't vote"
fi
8 changes: 8 additions & 0 deletions Basic_operations/14_ternary_ops.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

#can be used instead of if-else statement
#cond1 & if statement || else statement

age=18

[[ $age -ge 18 ]] && echo "Adult" || echo "minor"
21 changes: 21 additions & 0 deletions loops/15_for_loop1.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

for i in 1 2 3 4 5 6 7 8 9 10
do
echo "Number is $i"
done

#for strings
for name in Rathina Devan
do
echo "Name is $name"
done

#for iterating through numbers

for i in {1..5}
do
echo "number is $i"
sleep 3s
done

10 changes: 10 additions & 0 deletions loops/16_for_with_file.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

#getting values from a file names.txt

FILE="/home/ratu/Desktop/programs/bash/practice/names.txt"

for name in $(cat $FILE)
do
echo "Name is $name"
done
Loading

0 comments on commit 796be2c

Please sign in to comment.