Bash Scripting at a glance - Learn Bash scripting

1 comment

Bash Scripting at a glance - Learn Bash scripting



Take a variable and print out that in Bash Script

name="Akash"
echo "Welcome $name"

Output
Welcome Akash


Take a value From input and test the input using if statement or else do others


#Simple If else in Bash

echo "Please Enter your age : "
read age

if [ $age < 10 ]; 
    then echo "You are really small.."
else
    echo "You are growing.."
fi
Live Link - Live Demo


Print 1 to 100 by for loop using Bash script [Bash Programming Language]


# Printing 1 to 100 using for loop

for((i=1;i<=100;i++))
do
    echo $i
done


Using seq
for i in `seq 1 100`
do
    echo $i
done



Print 0 to 100 even numbers and odd numbers by for loop using Bash script [Bash Programming Language]

0 to 100 even numbers using Bash Scripting
# Printing 1 to 100 even numbers using for loop
for((i=0;i<=100;i=i+2))
do
    echo $i
done


1 to 100 Odd Numbers using Bash scripting
# Printing 1 to 100 odd numbers using for loop
for((i=1;i<=100;i=i+2))
do
    echo $i
done



Multiplication program upto 10 using for double for loop Bash scripting [Bash Programming Language]

0 to 100 even numbers using Bash Scripting
# Multiplication Program 1 to 10 in Bash

for((i=1;i<=10;i++))
do
    for((j=1;j<=10;j++))
    do
        echo $i "X" $j "=" $(($i*$j))
    done
    echo
done

Simple Output with Code
Live Link - Live Demo



Read a file name in Bash and check if file is exist and if file exist, then delete the file, otherwise create the file as new [Bash Programming Language]

search, create, delete a file using Bash Scripting

echo "Please Enter your File Name : "
read file_name

if [ -f $file_name ];
    then  clear
    rm $file_name
    echo "File $file_name is deleted successfully"
    
else
    clear
    touch $file_name
    echo "No file found for your search $file_name"
fi

echo "All files are Now : "
ls

Explanation :
  1. Read the file_name in  read file
  2. Check if file exist in if clause if [ -f $file_name ]; 
  3. If Exist then remove the file using rm $file_name
  4. If file is not found, create a new file as that name using touch $file_name 
  5. Finally, show the file lists, using ls.




Creating array, inserting value in the array, show the array values and delete the value by searching using Bash script [Bash Programming Language]


echo "How many numbers do you want to add in the array : "
read num

#Take value in the array and put it in a sum variable
arr=()  #Empty Array
sum=0

for ((i=0;i<num;i++));
do
    echo "Array[$i] = "
    read arr[$i]
    sum = $((sum+arr[$i]))
done

echo "Summation Is : $sum"

##
#   Delete a value from array
##

echo "Please enter a value to delete from the array = "
read delete_value;

for ((i=0;i<num;i++));
do
    if [ $((arr[$i])) = $delete_value ]; then 
        unset arr[$i]   #Delete using unset
    fi
done


#Show final array
echo  "Array is = ${arr[@]}"





Reverse a Number using Bash script [Bash Programming Language]

n=123456
rev=0

while [ $n -gt 0 ]
do
    reminder=$(( $n % 10 ))
    rev=$(( rev*10 + reminder  ))
    n=$(( $n / 10 ))
done

echo $rev

Reverse a number using bash script in one line :

n=123456
echo $n | rev


Why is this : In Bash the integer is also a text. The | reverse any string by that system.




Prime Number check using Bash script [Bash Programming Language]

num=1289
i=2

while [ $i -lt $num ]
do
  if [ $(( num%i )) = 0 ];
  then
      echo "$num is not a prime number"
      echo "It is divisible by $i"
      exit
  fi
  $i=$(( i+1 ))
done

echo "$num is a prime number "








Tags:
Operating System, Computer Programming, Bash Programming, Bash Scripting at a glance - Learn Bash scripting, file read, delete, create in bash, bash for loop, bash if else, bash loop, bash even numbers, odd numbers using bash

1 comment :

  1. ::Take a value From input and test the input using if statement or else do others
    your second bash script code is not right.
    For arithmetic comparison,you have to use First Bracket Instead of Third Bracket.
    Wrong arena : if [ $age < 10 ];
    Instead you have to use if (( $age < 10 ));
    Thank you..

    ReplyDelete