0% found this document useful (0 votes)
24 views3 pages

6a56ec8d-86a0-4b83-83d4-7702d3c2f102

The document is a comprehensive guide to Bash scripting, covering variable declaration, arithmetic operations, conditional statements, loops, and functions. It includes examples of syntax for various commands and constructs, such as if-else statements, for loops, and function definitions. Additionally, it explains how to handle user input, manage local and global variables, and debug scripts.

Uploaded by

Mahmoud Rashad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views3 pages

6a56ec8d-86a0-4b83-83d4-7702d3c2f102

The document is a comprehensive guide to Bash scripting, covering variable declaration, arithmetic operations, conditional statements, loops, and functions. It includes examples of syntax for various commands and constructs, such as if-else statements, for loops, and function definitions. Additionally, it explains how to handle user input, manage local and global variables, and debug scripts.

Uploaded by

Mahmoud Rashad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

#!

/bin/bash --> magaic line


# wich bash
# path ?
# declare -i var_int --> define integare variable
# $x --> value x containe
# declare -i var_int = $x+$y--> define integare variable and give it value
# typeset -i var_int
# z=$(($x+$y)) (or) #typeset -i z=$x+$y
# let var_int = $x+$y--> give value
# env ---> Print local variables in terminal
# bash  to open child shell
#day2_1.sh  run script in child shell so he don’t know value for var (run
without sourcing (.))
# .day2_1.sh - run script in my shell so it know that var = 20 as I define it
# export variable_name=value  make child shell (that will born from my shell
when i run a script) inherit the var from parent shell (script)
# $* --> After script name i will give any number of input then press enter
# echo $$  will print PID
# echo $?  will print 0 if last process run fine else print 1
# $0  print script name and path
# if [ condation ];then
#command
elif [ condation ] ;then
#command
else
#command
fi

# if [ condation ]
then
#command
elif [ condation ]
then
#command
else
#command
fi

# test x=="5" &&echo Y || echo N (or) # [ x-eq5 ] &&echo Y || echo N


# read x --> will take value (after run script) from user and store it in x
# echo $x --> print value in x
# read --> will take value (after run script) from user and store it in REPLY
# echo $REPLY
# read -p "plz enter number" x --> will "plz enter number" and take value (after
run script vs $* or $1 or $2 after script name when we run it ) from user and
store it in x
# if [ -f $file_name ];then ---> print next condation if file_name founded
# if [ -d $dir_name ];then ---> print next condation if dir_name foundrd
# $(($var/2) --> do mathematic on int var
# for item in 1 2 3 4 5 6
do
echo $(($item*2))
done
#for item in 1 2 3 4 5 6 (or) # for item in {1..6}
# var="mahmoud ali ahmed"
for item in $var
do
echo $item
done
#for item in "mahmoud" (or) # for item in {array[@]}
# var="mahmoud ali ahmed"
for item in $var
do
echo $item
done

# ls && echo "done" || "error" --> if ls do will prent done


# test "ali"="ali" &&echo "Y" || "N" --> will print N (logical error) becouse
must be space after and befor = --> "ali" = "ali" Note we can use = or ==
# [ "ali"="ali" ] &&echo "Y" || "N" --> will print N (logical error) the same
last error spaces also [space <condatio> space]
#$ x = "ali" -->sentaces error (spaces before and after = only at condation )
# [ "ali" = "ali" -a "x" = "y" ] &&echo "Y" || "N" --> -a (and) -o (or)
# [[ "ali" = "ali" && "x" = "y" ]] &&echo "Y" || "N" --> if we use [[ ]] we can
use && / ||
# [ $Var = "a*" && "x" = "y" ] &&echo "Y" || "N" --> N (logical error)
# [[ $Var = "a*" && "x" = "y" ]] &&echo "Y" || "N" --> N (logical error)
# [[ $Var = "a*" && "x" = "y" ]] &&echo "Y" || "N" --> N (consider * as char from
word) --> literal matching
# [[ $Var = "a*" && "x" = "y" ]] &&echo "Y" || "N" --> Y (Pattern Matching )
# echo "\$x" (or) # echo '$x' -->out $x becouse ' ' ignore any special char
# declare -i num=1+2 (or) # declare -i num="1"+"2" (or) # declare -i num="1+2"
(or) # let num="1"+2 (or) # typeset -i num=1+2 (or) # typeset -i "1"+2 (or) #
num=((1+2)) (or) # num=$(("1"+2))
# x=$(("3"*3))
x=$x+5 -->out 14
# [$num -le 5 ] (or) # (($num <= 5) --> @[] use -lt / -le / -gt / -ge / -eq / -ne
but @(()) use < / <= / > / >= / == / !=
# [[ $z >= 30] -->syntax error
(NOTE) Remmber --> $*(or)$@ --> allarguments / $#--> number of arguments / $n
EX:$1 (first argument)
# read
if [[ $REPLY = [mM]ahmoud ]] ; then -->for Mahmoud or mahmoud must use [[ ]]
#command
# for item in {1..6}
do
echo $item
done
# while true
do
if [[ ... ]]
#command
break
.....
fi
done
# script_name --> run in the same terminal
# . script_name --> run in the child terminal
# case $1 in # $1 --> take on input
"mahmoud" )
echo "engineer"
;;
"ali" )
echo "pepole"
;;
*)
echo "other case"
;;
esac
# export LC_COLLATE=C --> to shell now the difren't bettween capital and small
letter
# shopt -s extglob --> to be able to write --> @([a-z]) we can replace @ by ! or
+ or * or ? the following what all means
# @([a-z]) --> match exactly one
# +([a-z]) --> match one or more
# !([a-z]) --> match all
# ?([a-z]) --> match one or zero
# *([a-z]) --> match zero or more
# set -x -->start debug code
#code
set +x --> stop debug
#code

# my_arr=(1 2 "mah" 'w')


# echo ${my_arr[@]}
# echo ${my_arr[@]:2:2}
# my_arr+=(7 "ali")
# unset my_arr[0]
# arr2=${my_arr[@]/5/10} -->arr2 the same myarr but replace 5 element to 10

# hello(){
echo"hi"
}
hello --> call
# hello{
echo"hi"
}
hello --> call
# note in bash you can use var in fun before you call it but will have no value
and after call fun will update by value in fun
# if you type local var it will be seen only in function
# num=sum --> Error where sum is fun how to do it ? #num=$(sum) or #num=$? then
#echo $num
# y=$(ls) or y=`ls` -->to store ls out in y varible
# my_fun()
{
echo "input argumrnts:" $*
echo "$1+$2+$3"
}
# my_fun 10 20 30 -->call (out--> 60)

You might also like