diff --git a/24_functions.sh b/24_functions.sh new file mode 100755 index 0000000..5d39589 --- /dev/null +++ b/24_functions.sh @@ -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 + diff --git a/25_functions_with_args.sh b/25_functions_with_args.sh new file mode 100755 index 0000000..3294605 --- /dev/null +++ b/25_functions_with_args.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +function welcomenote { + echo "......................." + echo "Welcome $1" + echo "Age is $2" + echo "......................." +} + +welcomenote ratu 20 +welcomenote pradhul 30 diff --git a/26_args.sh b/26_args.sh new file mode 100755 index 0000000..c220677 --- /dev/null +++ b/26_args.sh @@ -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 diff --git a/27_shift_arguments.sh b/27_shift_arguments.sh new file mode 100755 index 0000000..eabd2a1 --- /dev/null +++ b/27_shift_arguments.sh @@ -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 $@" diff --git a/28_break.sh b/28_break.sh new file mode 100755 index 0000000..96cc99c --- /dev/null +++ b/28_break.sh @@ -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 diff --git a/29_continue.sh b/29_continue.sh new file mode 100755 index 0000000..5782644 --- /dev/null +++ b/29_continue.sh @@ -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 diff --git a/30_connectivity_check.sh b/30_connectivity_check.sh new file mode 100755 index 0000000..0ea072a --- /dev/null +++ b/30_connectivity_check.sh @@ -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 diff --git a/31_file_exist_check.sh b/31_file_exist_check.sh new file mode 100755 index 0000000..b00d69c --- /dev/null +++ b/31_file_exist_check.sh @@ -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 + diff --git a/32_dice.sh b/32_dice.sh new file mode 100755 index 0000000..cf069c0 --- /dev/null +++ b/32_dice.sh @@ -0,0 +1,7 @@ +#!/bin/bash + + +#generating a random number between 1 to 6 + +no=$(( $RANDOM % 6 + 1 )) +echo "Random Number is $no." diff --git a/33_root_user_check.sh b/33_root_user_check.sh new file mode 100755 index 0000000..7aca06c --- /dev/null +++ b/33_root_user_check.sh @@ -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 diff --git a/34_redirect.sh b/34_redirect.sh new file mode 100755 index 0000000..f4df469 --- /dev/null +++ b/34_redirect.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +ping -c 1 www.google.com > redirect.log diff --git a/35_script_name.sh b/35_script_name.sh new file mode 100755 index 0000000..3b68de6 --- /dev/null +++ b/35_script_name.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +echo "Name of this script is ${0}" diff --git a/36_logger.sh b/36_logger.sh new file mode 100755 index 0000000..65948f2 --- /dev/null +++ b/36_logger.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +# Example of logging + +logger " This is a log from ${0}" diff --git a/37_debugging.sh b/37_debugging.sh new file mode 100755 index 0000000..70d354a --- /dev/null +++ b/37_debugging.sh @@ -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 + diff --git a/Basic_operations/01_basic.sh b/Basic_operations/01_basic.sh new file mode 100755 index 0000000..532a07d --- /dev/null +++ b/Basic_operations/01_basic.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +echo "HEY buddy!" diff --git a/Basic_operations/02_comments.sh b/Basic_operations/02_comments.sh new file mode 100755 index 0000000..b37ea57 --- /dev/null +++ b/Basic_operations/02_comments.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +echo "checking comments" + +#this is a single line comment + +< $BASE/archive" + gzip $i || exit 1 + mv $i.gz $BASE/archive || exit 1 + fi +done + diff --git a/projects/ram_status.sh b/projects/ram_status.sh new file mode 100644 index 0000000..0e372e9 --- /dev/null +++ b/projects/ram_status.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +FREE_SPACE=$(free -mt | grep "Total" | awk '{print $4}') +THRESH_HOLD=10000 + + +if [[ $FREE_SPACE -le $THRESH_HOLD ]] +then + echo "WARNING, RAM is running low" +else + echo "RAM Space is sufficient - $FREE_SPACE " +fi + diff --git a/projects/user_make.sh b/projects/user_make.sh new file mode 100755 index 0000000..81fab39 --- /dev/null +++ b/projects/user_make.sh @@ -0,0 +1,64 @@ +#!/bin/bash + +# Script should be executed with root user. + +if [[ "$UID" -ne 0 ]] +then + echo "Please run with sudo or root." + exit 1 +fi + +# User should provide atleast one argument as username else guide him + +if [[ "$#" -lt 1 ]] +then + echo "Usage: ${0} USER_NAME [COMMENT]..." + echo "Create a user with name USER_NAME and comments field of COMMMENT" + exit 1 +fi + +# Store 1st argument as username + +USER_NAME="${1}" + +# In case of more than one argument , store it as account comments +shift +COMMENT="${@}" + + +# create a password +PASSWORD=$(date +%s%N) + +# Create a user +useradd -c "$COMMENT" -m $USER_NAME + +# Check if the user is created successfully created or not +if [[ $? -ne 0 ]] +then + echo "The Account could not be created" + exit 1 +fi + +# Set the password for the user. +echo "$USER_NAME:$PASSWORD" | chpasswd + +# Check if the password is successfully set or not +if [[ $? -ne 0 ]] +then + echo "Password could not be set" + exit 1 +fi + +# Force password change on first login +passwd -e $USER_NAME + +#Display the username,password and the host where the user was created. + +echo +echo "Username: $USER_NAME" +echo +echo "Password: $PASSWORD" +echo +echo "Hostname: $(hostname)" + +