case এর আর একটি উদাহরণ

Output First

bash-3.2$ ./ksh_case_chart.sh
Enter your Number
9
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90

—-
কোড

#!/bin/ksh

#example on how to use case and for loop

multi()
{
#clear
for i in 1 2 3 4 5 6 7 8 9 10
do
echo “$Y * $i = `expr $Y \* $i`”
done
}

#take a number from the user ( 1..10)
echo “Enter your Number”
read X

case “$X” in

1)
Y=1
multi
;;

2)
Y=2
multi
;;

3)
Y=3
multi
;;

4)
Y=4
multi
;;

5)
Y=5
multi
;;

6)
Y=6
multi
;;

7)
Y=7
multi
;;

8)
Y=8
multi
;;

9)
Y=9
multi
;;

10)
Y=10
multi
;;

esac
exit

—–

৩০ সেকেন্ড পর পর যে সমস্ত শেল রান করছে তাদের ডাটা একটা লগ ফাইল এ সেভ করতে হবে

#!/bin/sh

#find all currently running shells and create in full listing form for them . Log the data into a file in every 30 secs
# -e means currentl running
# -f means full format output
# sleep takes parameters in seconds

ps -ef | grep shell >> process.log
sleep 30

ps -ef | grep shell >> process.log
sleep 30

ps -ef | grep shell >> process.log
sleep 30

case এর অন্য একটি ব্যবহার . Example use of case

ফলাফল

bash-3.2$ ./ksh_case_chart.sh
Enter your Number
9
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90

—-
কোড

#!/bin/ksh

#example on how to use case and for loop

multi()
{
#clear
for i in 1 2 3 4 5 6 7 8 9 10
do
echo “$Y * $i = `expr $Y \* $i`”
done
}

#take a number from the user ( 1..10)
echo “Enter your Number”
read X

case “$X” in

1)
Y=1
multi
;;

2)
Y=2
multi
;;

3)
Y=3
multi
;;

4)
Y=4
multi
;;

5)
Y=5
multi
;;

6)
Y=6
multi
;;

7)
Y=7
multi
;;

8)
Y=8
multi
;;

9)
Y=9
multi
;;

10)
Y=10
multi
;;

esac
exit

—–

case এর ব্যবহার . Example use of case

Example Output:
10 * 1 = 10
10 * 2 = 20
10 * 3 = 30
10 * 4 = 40
10 * 5 = 50
10 * 6 = 60
10 * 7 = 70
10 * 8 = 80
10 * 9 = 90
10 * 10 = 100

———————————-

#!/bin/bash
multi()
{
clear
for ((i=1;i<=10;i++)); do echo "$Y * $i = `expr $Y \* $i`" done } echo "Enter your Number" read X case "$X" in 1) Y=1 multi ;; 2) Y=2 multi ;; 3) Y=3 multi ;; 4) Y=4 multi ;; 5) Y=5 multi ;; 6) Y=6 multi ;; 7) Y=7 multi ;; 8) Y=8 multi ;; 9) Y=9 multi ;; 10) Y=10 multi ;; esac exit Output 7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49 7 * 8 = 56 7 * 9 = 63 7 * 10 = 70

শেল প্রোগ্রামিং এ for লুপ এবং ফাংশন এর ব্যবহার। Use of For Loop and Function in Shell Programming

bash-3.2$ cat multiplication.sh
#!/bin/bash

# Example use of function/method declaration in Shell Programming
# Example use of for loop

#a function that will multiply 1 to 10 with 5
multiplication()
{
x=5
for i in {1..10}
do
echo $i
echo “the multiplication result is:” $(($i*$x))
done
}

echo “Print 1 to 10; also multiply each number with 5 and show the multiplication result”
multiplication

—————————–
Output – ফলাফল

bash-3.2$ ./multiplication.sh
Print 1 to 10; also multiply each number with 5 and show the multiplication result
1
the multiplication result is: 5
2
the multiplication result is: 10
3
the multiplication result is: 15
4
the multiplication result is: 20
5
the multiplication result is: 25
6
the multiplication result is: 30
7
the multiplication result is: 35
8
the multiplication result is: 40
9
the multiplication result is: 45
10
the multiplication result is: 50

শেল – case এর ব্যবহার ksh – এ। Example use of Case in KSH. Show Today’s Day Name

bash-3.2$ cat ksh_case_example_1.sh
#!/bin/ksh

#show today’s day name
DAY=`date +%a`

#show value of $DAY
echo $DAY

#based on the value of the DAY variable show the day name in full format
case “$DAY” in

Mon)
echo “Monday”
;;

Tue)
echo “Tuesday”
;;

Wed)
echo “Wednesday”
;;

Thu)
echo “Thursday”
;;

Fri)
echo “Friday”
;;

Sat)
echo “Saturday”
;;

Sun)
echo “Sunday”
;;

esac

—————

Output – ফলাফল

bash-3.2$ ./ksh_case_example_1.sh
Sun
Sunday

গাণিতিক অপারেশন – ব্যবহারকারী থেকে দুটি মান ব্যবহার করে : Arithmetic operations using two values from the user

#!/usr/bin/bash
clear
echo “Enter First Number”
read x

echo “Enter Second Number”
read y

#does not work for /bin/sh
(( z = $x + $y ))
(( a = $x – $y ))
(( b = $x \* $y ))
(( c = $x \/ $y ))

#clear

echo “Addition of 2 numbers is =” $z
echo “Subtraction of 2 numbers is =” $a
echo “Multiplication of 2 numbers is =” $b
echo “Division of 2 numbers is =” $c

—————————–

Output – ফলাফল

Enter First Number
20
Enter Second Number
5
Addition of 2 numbers is = 25
Subtraction of 2 numbers is = 15
Multiplication of 2 numbers is = 100
Division of 2 numbers is = 4

এরিথমেটিক অপারেশন দুটি ভেরিয়েবল ব্যবহার করে

#!/bin/sh

#will do addition, subtraction, multiplication, and division of two variables
X=12
Y=5

#print the values of the variables
echo “X value is: “$X
echo “Y value is: “$Y

#addition
echo “X + Y” = `expr $X + $Y`

#subtraction
echo “Y – X” = `expr $Y – $X`

#multiplication
echo “X * Y” = `expr $X \* $Y`

#division
echo “X / Y” = `expr $X \/ $Y”`

ফলাফল – Output

bash-3.2$ ./arithmetics-operations-of-variables.sh
X value is: 12
Y value is: 5
X + Y = 17
Y – X = -7
X * Y = 60
X / Y = 2

ভেরিএবল এর ব্যবহার – use of variables

#!/bin/sh

#assign value to a variable and print to the screen
NAME=”Justetc”
echo $NAME

#Ask the user to enter a Name
echo ##################################
echo “enter name”

#read command is used to take input from the user
read X

#print the value two times to the screen
echo $X
echo $X
—————————
ফলাফল – Output

bash-3.2$ ./variable_use.sh
Justetc

enter name
Sayed Ahmed
Sayed Ahmed
Sayed Ahmed

একটি বেসিক শেল প্রোগ্রাম

#!/bin/sh

#if enabled – will spit out the script to the screen
#set -v

#clear the screen
clear

echo “SCRIPT BEGINS”

#shows current login name
echo “Hello $LOGNAME”

#shows current date in the same line
echo “Todays date is: \c”
date +%m/%d/%y

#shows current time in the same line
echo “and the current time is: \c”
date +%H:%M:%S%n

#shows a list of the processes
echo “Now a list of the processes in the current shell”
ps

echo “SCRIPT FINISHED!!”

#example of how to use a variable
NAME=”Justetc”
echo $NAME
——————————————–
Output – প্রোগ্রাম এর ফলাফল

SCRIPT BEGINS
Hello shell
Todays date is: 04/15/18
and the current time is: 14:46:43

Now a list of the processes in the current shell
PID TTY TIME CMD
14522 pts/2 0:00 bash
14590 pts/2 0:00 a_basic_
14595 pts/2 0:00 ps
14508 pts/2 0:00 sh
SCRIPT FINISHED!!
Justetc