Translate by Vũ Đình Tùng
----------------------------------------------------------------------------------------------------
Q.1. How to write shell script that will add two nos, which are supplied as command line
argument, and if this two nos are not given show error and its usage
Viết shell script tính tng ca 2 s,các s được cung cp như đối s dòng lnh, và nếu không
đưa đủ 2 s hin thông báo li, và cách khc phc
if [ $# -ne 2 ] #Ki m tra s đ i s truy n vàoế
then
echo "Usage - $0 x y"
echo " x và y là 2 s mà chúng ta s tính t ng "
exit 1
fi
echo "T ng c a hai s `expr $1 + $2`"
Q.2.Write Script to find out biggest number from given three nos. Nos are supplies as
command line argument. Print error if sufficient arguments are not supplied.
Viết Script để tìm ra s ln nht trong ba s. Các s được truyn qua đối s dòng lnh. In li, nếu
đối s không được cung cp đầy đủ.
# 1) START: Take three nos as n1,n2,n3. B t đ u: coi 3 s là n1, n2, n3.
# 2) Is n1 is greater than n2 and n3, if yes
# print n1 is bigest no goto step 5, otherwise goto next step
# - N u n1 l n h n n2 và n3, in n2 là s l n nh t, nh y đ n b c 5, #ế ơ ế ư
# tr ng h p khác nh y đ n b c ti p theo.ườ ế ướ ế
# 3) Is n2 is greater than n1 and n3, if yes
# print n2 is bigest no goto step 5, otherwise goto next step
# - N u n2 l n h n n1 và n3, in n2 là s l n nh t, nh y đ n b c 5, #ế ơ ế ư
# tr ng h p khác nh y đ n b c ti p theo.ườ ế ướ ế
# 4) Is n3 is greater than n1 and n2, if yes
# print n3 is bigest no goto step 5, otherwise goto next step
# - N u n3 l n h n n2 và n1, in n3 là s l n nh t, nh y đ n b c 5, #ế ơ ế ư
# tr ng h p khác nh y đ n b c ti p theo.ườ ế ướ ế
# 5) END
#
#
if [ $# -ne 3 ]
then
echo "$0: So n1 so n2 so n3 chua duoc nhap" >&2
exit 1
fi
n1=$1
n2=$2
n3=$3
if [ $n1 -gt $n2 ] && [ $n1 -gt $n3 ]
then
echo "$n1 la so lon nhat"
elif [ $n2 -gt $n1 ] && [ $n2 -gt $n3 ]
then
echo "$n2 la so lon nhat"
elif [ $n3 -gt $n1 ] && [ $n3 -gt $n2 ]
then
echo "$n3 la so lon nhat"
elif [ $1 -eq $2 ] && [ $1 -eq $3 ] && [ $2 -eq $3 ]
then
echo "tat ca 3 so bang nhau"
else
echo "Khong the tim duoc so lon nhat"
1
Translate by Vũ Đình Tùng
----------------------------------------------------------------------------------------------------
fi
2
Translate by Vũ Đình Tùng
----------------------------------------------------------------------------------------------------
Q.3.Write script to print nos as 5,4,3,2,1 using while loop.
Q.3.Write kch bn để in s dưới dng 5,4,3,2,1 khi s dng vòng lp.
Answer: See Q3 shell Script.
Algo:
# 1) START: set value of i to 5 (since we want to start from 5, if you
# want to start from other value put that value)
#B t đ u: gán giá tr cho i=5 ( đây chúng ta b t đ u t 5, n u ế
#mu n b t đ u t m t giá tr khác hãy nh p vào giá tr đó)
# 2) Start While Loop
#B t đ u vòng l p While
# 3) Chechk, Is value of i is zero, If yes goto step 5 else
# continue with next step
#Kiêm tra, giá tr c a i=0? n u đúng nh y đ n b c 5 ng c l i ế ế ướ ượ
#ti p t c v i b c ti p theo.ế ướ ế
# 4) print i, decement i by 1 (i.e. i=i-1 to goto zero) and
# goto step 3
#In giá tr c a i, giám giá tr c a i đi 1 và quay l i b c 3 ướ
# 5) END
i=5
while test $i != 0
do
echo "$i"
i=`expr $i - 1`
done
Q.4. Write Script, using case statement to perform basic math operation as
follows
+ addition
- subtraction
x multiplication
/ division
The name of script must be 'q4' which works as follows
$ ./q4 20 / 3, Also check for sufficient command line arguments
Answer: See Q4 shell Script.
Q.4. Viết Script, s dng các ký hiu sau để mô phng hot động cơ bn ca các phép toán hc:
+ Phép Cng
- Phép tr
x nhân
/ phé chia
if test $# = 3
then
case $2 in
+) let z=$1+$3;;
-) let z=$1-$3;;
/) let z=$1/$3;;
x|X) let z=$1*$3;;
*) echo c nh báo - $2 không ph i là phép toán, ch +,-,x,/ là phép toán
exit;;
esac
echo Answer is $z
else
echo "Usage - $0 value1 operator value2"
echo " Where, value1 and value2 are numeric values"
3
Translate by Vũ Đình Tùng
----------------------------------------------------------------------------------------------------
echo " operator can be +,-,/,x (For Multiplication)"
fi
Q.5.Write Script to see current date, time, username, and current directory
Viết shell scrip hin ngày tháng hin hành, thi gian, tên người dùng, và thư mc hin hành.
Answer: See Q5 shell Script.
echo "Hello, $LOGNAME"
echo "Current date is `date`"
echo "User is `who i am`"
echo "Current direcotry `pwd`"
Q.6.Write script to print given number in reverse order, for eg. If no is 123 it must print as
321.
Viết Shell script để in s đảo ngược, ví d: nếu s là 123 thì in ra 321
Answer: See Q6 shell Script.
# Algo:
# 1) Input number n - Nh p vào s n
# 2) Set rev=0, sd=0 - đ t rev=0(S đ o ng c), sd=0(S trái nh t) ượ
# 3) Find single digit in sd as n % 10 it will give (left most digit)
#Tìm s sd=n%10(% Phép chia l y ph n d ) (là s ph i nh t c a n) ư
# 4) Construct revrse no as rev * 10 + sd - Xây d ng s rev: rev=rev*10+sd
# 5) Decrment n by 1 (Ch này sai) ph i gán n=n/10 (Phép chia l y ph n nguyên)
# 6) Is n is greater than zero, if yes goto step 3, otherwise next step
#n v n còn l n h n 0? n u đúng, quay l i b c 3, ng c l i đ n b c ti p ơ ế ướ ượ ế ướ ế
# 7) Print rev - In s đ o ng c rev ượ
#
if [ $# -ne 1 ] # ki m tra s đ i s nh p vào ( đây ch c n nh p 1 đ i s )
then
echo "Usage: $0 Ch a nh p sư "
echo " I will find reverse of given number -" # Không c n quan tâm
echo " For eg. $0 123, I will print 321" # Không c n quan tâm
exit 1
fi
n=$1
rev=0
sd=0
while [ $n -gt 0 ] #Vòng l p down to
do
sd=`expr $n % 10`
rev=`expr $rev \* 10 + $sd`
n=`expr $n / 10` #VD: n u n=123 khi gán n=n/10 thì n=12ế
done
echo "S đ o ng c là $rev ượ "
Q.7.Write script to print given numbers sum of all digit, For eg. If no is 123 it's sum of all digit
will be 1+2+3 = 6.
Q.7.Viết Shell scrip để in tng tt c các s trong ch s, ví d: nếu s là 123 ta phi in ra tng
ca tt c các ch s 1 2 3 = 6.
Algo:
# 1) Input number n - Nh p vào s n(Truy n qua đ i s )
# 2) Set sum=0, sd=0 - Đ t sum=0 (S t ng), Sd=0
# 3) Find single digit in sd as n % 10 it will give (left most digit)
#Tìm đ c s sd=n%10(% Phép chia l y ph n d ) (là s bên ph i nh t c a n)ượ ư
# 4) Construct sum no as sum=sum+sd - Xây d ng s t ng sum=sum+sd
# 5) Decrment n by 1 (Ch này sai) ph i Gán n=n/10 (Chia l y ph n nguyên)
4
Translate by Vũ Đình Tùng
----------------------------------------------------------------------------------------------------
# 6) Is n is greater than zero, if yes goto step 3, otherwise next step
#n v n l n h n 0? n u đúng, nh y đ n b c 3, n u không chuy n b c ti p. ơ ế ế ướ ế ướ ế
# 7) Print sum - In t ng
#
if [ $# -ne 1 ] #Ki m tra s đ i s nh p vào ( đây ch c n 1 đ i s )
then
echo "Usage: $0 ki m tra đ i sế "
echo " I will find sum of all digit for given number" #Kô c n quan tâm
echo " For eg. $0 123, I will print 6 as sum of all digit (1+2+3)"
exit 1
fi
n=$1
sum=0
sd=0
while [ $n -gt 0 ]#Vong l p down to
do
sd=`expr $n % 10` #l y s ph i nh t c a n,Vd: n=123 thì sd=3
sum=`expr $sum + $sd` #C ng d n vào t ng sum
n=`expr $n / 10` #VD: n u n=123 khi gán n=n/10(Chia l y ph n nguyên) thì n=12ế
done
echo "T ng c a t t c các ch s là $sum"
Q.8.How to perform real number (number with decimal point) calculation in Linux
Làm thế nào để tính toán s thc (S vi phn thp phân) trong linux
Tr l i: S d ng l nh bc trong linux
Q.9.How to calculate 5.12 + 2.5 real number calculation at $ prompt in Shell ?
Làm thế nào để thc hin phép tính s thc 5.12+2.5 vi du nhc $ trong Shell Scrip
Tr l i: S d ng l nh echo $ 5,12 + 2,5 | bc
Q.10.How to perform real number calculation in shell script and store result to
third variable , lets say a=5.66, b=8.67, c=a+b?
Answer: See Q10 shell Script.
Q.10. Làm thế nào để tính toán s thc trong Shell scrip vi 3 biến lưu tr, hãy thc hin vi a =
5.66, b = 8.67, tính c = a + b?
a=5.66
b=8.67
c=`echo $a + $b | bc`
echo "$a + $b = $c"
Q.11.Write script to determine whether given file exist or not, file name is supplied as
command line argument, also check for sufficient number of command line argument
Answer: See Q11 shell Script.
Q.11. Viết Shell Scipt để xác định tp tin đã cho có tn ti hay không, tên tp tin được cung cp
như đối s dòng lnh, Kim tra xem có đủ đối s không
if [ $# -ne 1 ] #Ki m tra s đ i s truy n vào ( bài này ch c n 1)
then
echo "Usage - $0 file-name"
exit 1
fi
5