Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nikhil3000/34958cbdee2b8532137b8c1e11c4acd5 to your computer and use it in GitHub Desktop.
Save nikhil3000/34958cbdee2b8532137b8c1e11c4acd5 to your computer and use it in GitHub Desktop.
Shell scripting for beginners
#! /bin/bash
#Created by following tutorials by https://www.youtube.com/channel/UCs6nmQViDpUw0nuIx9c_WvA Programming Knowledge
# echo "hello world"
# echo "Enter names: "
# read name1 name2 name3
# echo "Name : $name1 , $name2 , $name3"
# read -p 'username :' user_var #-p to get input in the same line
# read -sp 'password' pass_var #s for silent
# echo #for blank line
# echo username $user_var
# echo password $pass_var
# echo "Enter names :"
# read -a names #a for array
# echo Names ${names[0]} , ${names[1]}
# echo "Enter name"
# read
# echo Name $REPLY #reply is the default variable for input in case you don't specify any variable
# echo $0 $1 $2 $3 #value of first argument(flag) goes to $1 second to $2 and so on
#$0 contains the value of command executed i.e ./hello.sh in this case
# args=("$@") #to get the value of argum
# echo ${args[0]} ${args[1]} ${args[2]}
# echo $@ #to print all the arguments
# echo $# #to print the number og arguments
# (( )) --> for inequality
# [] otherwise
# count=10
# if (( $count > 10 ))
# then
# echo "true"
# fi
# string=abc
# if [ $string = "abc" ]
# then
# echo "true"
# fi
# echo -e "enter the file name \c" # \c to keep the cursor in the same line but it won't interpret escape sequences without -e
# read file_name
# if [ -e $file_name ] # -e flag to check if file exists
# then # -f flag to check if file exists and it is regular file
# echo $file_name found # -d for directory -b block special -c character special -s to check if file is empty
# else
# echo $file_name not found #-w for write permission
# fi
# > file will be overwritten
# >> data will be appended to the file
# echo -e "enter file_name \c"
# read file_name
# if [ -f $file_name ]
# then
# if [ -w $file_name ]
# then
# echo "enter the data you want to append to this file. Press ctrl+d to exit"
# cat >> $file_name
# else
# echo "write permission denied"
# fi
# else
# echo "file not found"
# fi
# age=25
# if (( $age > 20 )) && (( $age < 30 ))
# then
# echo "valid age"
# else
# echo "invalid age"
# fi
#arithmetic operations
#num1=20
# num2=5
# echo $(( num1 + num2 ))
# echo $( expr $num1 + $num2 ) #for multiplication * won't work use \*
#for decimal numbers we will use bc(basic calculator)
# whatever is written on the left side of | acts as input for the right side
# echo 5.2+7.3 | bc
# echo "scale=2;4.5/9" | bc #scale sets the number of digits after decimal
# echo "scale=2;sqrt($num1)" | bc -l #-l to include the math library of bc which contains the function sqrt
# echo "scale=2;3^3" | bc -l
# vehicle=$1
# case $vehicle #switch case
# "car" )
# echo "rent of $vehicle is 100 dollar" ;;
# "van" )
# echo "Rent of $vehicle is 80 dollar" ;;
# * )
# echo "unknown";;
# esac
# echo -e "Enter some character \c"
# read value
# case $value in
# [a-z] )
# echo "user entered $value a to z ";; #[a-z] pattern recog
# [A-Z] )
# echo "user entered $value A to Z";; #in case it doesn't work set LANG=c in terminal
# [0-9] )
# echo "user entered $value 0-9";;
# ? )
# echo "special character";;
# * )
# echo unknown;;
# esac
# os=('ubuntu' 'mac' 'windows') #initiating array
# os[6]='kali' #adding elements----gaps are allowed in an array
# unset os[2] #deleting an element
# echo "${os[@]}" #print all the elements of array
# echo "${!os[@]}" #print indices
# echo "${#os[@]}" #print length
#we can also acess strigs index wise
# n=1
# while (( $n < 10 ))
# do
# echo $n
# (( n++ )) #n=$(( n+1 ))
# done
# sleep 1 #will sleep for 1 second
# gnome-terinal #to open a new terminal
#reading file
# while read p #reading a file line by line
# do
# echo $p
# done < hello.sh #this data is read by p
# cat hello.sh | while read p #content read using cat command is used a input for while command
# do
# echo $p
# done
# while IFS=' ' read -r p #to read escape sequences -r pevents /escape to be interpreted
# do
# echo $p
# done < hello.sh
# num=1
# until (( num == 2 )) #commands in this loop are executed when the conditions are false
# do
# echo "$num"
# (( num++ ))
# done
#for loops
# for i in 1 2 3 4 5
# do
# echo $i
# done
# for i in {1..10..2} #a..b..c a->starting value b->ending value c->value by which i is incremented
# do # we can skip c and it will be defaulted to 1
# echo $i
# done
# for (( i=0; i<5; i++))
# do
# echo $i
# done
# for command in ls pwd date
# do
# echo "-----$command----------" #to print the name of command
# $command #to execute the command
# done
# for item in * # * would give all the items
# do
# if [ -d $item ] #to check if item is directory
# then
# echo $item
# fi
# done
#select loop
# select name in mark john tom ben
# do
# echo "$name selected"
# done
# select name in mark john tom ben
# do
# case $name in
# mark )
# echo mark selected
# ;;
# john )
# echo john selected
# ;;
# ben )
# echo ben selected
# ;;
# tom )
# echo tom selected
# ;;
# * )
# echo "error";;
# esac
# done
#break and continue condition
# for (( i=0; i<10; i++))
# do
# if [ $i == 0 ]
# then
# continue
# fi
# echo $i
# if (( $i > 5 ))
# then
# break
# fi
# done
#functions
# function Hello(){
# echo "hello"
# echo $1
# }
# quit () { #another way of declaring functions
# exit
# }
# Hello test #test 1 the argument passed with function
# quit
# echo "he"
#function example
# is_file_exist()
# {
# local file=$1 #local is used to set the scope of variable local. By default it is global
# # $1 here is the argument passed to this function
# # and not the argument passed with command in terminal
# [[ -f "$file" ]] && return 0 || return 1
# }
# if(is_file_exist $1)
# then
# echo "File found"
# else
# echo "File not found"
# fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment