special variables:
$0 - information of scrpit name
$1, $2 .. postional arguments
$# - count of arguments
$@ - All arguments
$? - status of the scrpit
#!/bin/bash
echo "users inputs is $#"
echo "my scprit name is $0"
echo "first argument value is $1"
echo "second argument value is $2"
echo "third argument value is $3"
echo "Total count of arguments is $#"
ls /etc
echo $? # status sucesscode is 0 , if failed not equal 0
Array Variables:
- multiple values for single variable
fruits=("apple" "mango" "banana")
# idex will start from 0
echo ${fruits[0]} # print first value
echo ${fruits[1]} # print second value
echo ${fruits[2]} # print third value
echo ${fruits[@]} # print all value
Example where we use array
for fruit in ${fruits[@]}; do
echo "fruit name is $fruit "
mkdir -p demo/$fruit
done
Read-only variables
- constand variable – fixed values
readonly PI=3.14
readonly userid=$(id -u)
echo $PI
#!/bin/bash
read -p "enter user name: " user_name
read -p "please enter current age: " age
read -p "please eneter location: " location
read -p "enter current org: " company
echo "my name is $user_name , current age is $age , i am coming from $location , current org name is $company"
shell scrpiting:
- mutiple commands
- ls, cd, mkdir…etc sudo, apt or yum or dnf
Ex:
# ubuntu
sudo apt update
sudo apt install apache2 -y
# dnf or yum
sudo yum update
sudo yum install httpd -y
-
multiple conditions
-
for loop – list array varible run as loop
-
while –
-
if condition
- check condition if true run cmds
-
if else condition
- if condition is true run specific true cmds , false run other cmds
-
if elsif else condition
-
case
-
nested if elsif conditions
Ex: if [ -f demo.txt ] then echo “sucess if condition” elseif [ -f demo1.txt ] then echo “sucess if condition” else echo “else cmds”
