Unix Switch Case

variables type
1)shell
2)system/environment
3) userdefine variable

!/bin/bash

echo “File Name: $0”
echo “First Parameter : $1”
echo “Second Parameter : $2”
echo “Quoted Values: $@”
echo “Quoted Values: $*”
echo “Total Number of Parameters : $#”


!/bin/sh

for TOKEN in $*
do
echo $TOKEN

done

!/bin/bash

echo “Enter first number”
read x
echo “Enter second number”
read y
sum=$(( $x + $y ))

echo “The result of addition=$sum”

!/bin/bash

echo “enter your choice”
read mycase
case $mycase in
1) echo “You selected unix”;;
2) echo “You selected php”;;
3) echo “You selected java”;;
4) echo “You selected c++”;;
5) exit

esac

!/bin/bash

echo “enter vowel”
read mycase
case $mycase in
‘a’) echo “vowel”;;
‘e’) echo “vowel”;;
‘i’) echo “vowel”;;
‘o’) echo “vowel”;;
‘u’) echo “vowel”;;
*) echo “consonent” ;;

esac

!/bin/bash

echo “Enter your lucky number”
read n
case $n in
101)
echo echo “You got 1stprize” ;;
510)
echo “You got 2nd prize” ;;
999)
echo “You got 3rd prize” ;;
*)
echo “Sorry,tryfor the next time” ;;
esac


!/bin/bash

for i in {1..5}
do
sleep $i &
done
echo “Background processes started…”
wait
echo “All processes have completed!”
~


test 5 -gt 2 && echo “Yes”

test 1 -lt 2 && echo “Yes”

!/bin/bash

if [ expr $a % 2 == 0 -a $a -gt 10 ]
then
echo “$a is even and greater than 10.”
else
echo “$a failed the test.”

fi

!/bin/bash

for (( i=1 ; i<=5 ; i++ ))
do
for (( j=1 ; j<=i ; j++ ))
do
echo -n “*”
done
echo ” “

done

!/bin/bash

for (( i=1 ; i<=5 ; i++ ))
do
for (( j=1 ; j<=i ; j++ ))
do
echo -n “$i”
done
echo ” “
done


//was multiplication table

!/bin/bash

echo “Enter a Number”
read n
i=1

while [ $i -le 10 ]
do
echo ” $n x $i = $(( n * i ))”
i=$(( i + 1 ))
done


!/bin/bash

rows=5
for((i=rows; i>=1; i–))
do
for((j=1; j<=i; j++))
do
echo -n “* “
done
echo
done

!/bin/bash

clear
p=$1
r=$2
n=$3
let int=expr $p \* $r \* $n
int=expr $int / 100

echo “simple interest , $int”

!/bin/bash

echo “enter the year”

read a

year= $a

a=$(( $a %4 ))

if [ $a -eq 0 ];

then
echo “$year is leap”

else
echo “$year is not a leap year”

fi

!/bin/bash

echo “Enter any number”
read n

if [[ ( $n -eq 15 || $n -eq 45 ) ]]
then
echo “You won the game”
else
echo “You lost the game”

fi

!/bin/bash

echo “Enter your lucky number”
read n

if [ $n -eq 101 ];
then
echo “You got 1st prize”
elif [ $n -eq 510 ];
then
echo “You got 2nd prize”
elif [ $n -eq 999 ];
then
echo “You got 3rd prize”

else
echo “Sorry, try for the next time”
fi

========================================

!/bin/bash

echo “enter file name”
read fname
if [ -f $fname ]; then
echo “$fname file exist”
else
echo “sorry, $fname file does not exist”

fi

!/bin/bash

function F1()
{
echo ‘I like bash programming’
}

F1

!/bin/bash

echo “Enter directory name”
read ndir
if [ -d “$ndir” ]
then
echo “Directory exist”
else
mkdir $ndir
echo “Directory created”
fi

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *