Bài 1:
Ví dụ 1:
In [ ]:
%%bash
touch rmif
chmod 755 rmif
touch a
In [ ]:
%%shell
./rmif aaaa
./rmif a
rm: cannot remove 'aaaa': No such file or directory
a is deleted successfully!
Out[ ]:
Ví dụ 2:
In [ ]:
%%bash
touch ispositive
chmod 755 ispositive
In [ ]:
%%bash
./ispositive 3
3 is positive number
Ví dụ 3:
In [ ]:
%%bash
touch is_positive_negative
chmod 755 is_positive_negative
In [ ]:
%%bash
./is_positive_negative 3
./is_positive_negative -3
3 is positive
-3 is negative
Ví dụ 4:
In [ ]:
%%bash
touch testfor
chmod 755 testfor
In [ ]:
%%bash
./testfor
Ví dụ 5:
In [ ]:
%%bash
touch for2
chmod 755 for2
In [ ]:
%%bash
./for2
Welcome
Welcome
Welcome
Welcome
Welcome
1
2
3
4
5
times
times
times
times
times
Ví dụ 6:
In [ ]:
%%bash
touch nestedfor
chmod 755 nestedfor
In [ ]:
%%bash
i=1
while [ $i -le 5 ]
do
echo “Welcome $i times”
i=`expr $i + 1`;
done
“Welcome
“Welcome
“Welcome
“Welcome
“Welcome
1
2
3
4
5
times”
times”
times”
times”
times”
Ví dụ 7:
In [ ]:
%%bash
i=1
while [ $i -le 5 ]
do
echo "Welcome $i times"
i=`expr $i + 1`
done
Welcome
Welcome
Welcome
Welcome
Welcome
Ví dụ 8:
In [ ]:
1
2
3
4
5
times
times
times
times
times
%%bash
touch casetest
chmod 755 casetest
In [ ]:
%%shell
./casetest
Please choose a number:
1: Fried Eggs
2: Salad
3: Noodle
3
Thanks, you chose Fried Noodle.
Out[ ]:
Bài 2:
In [ ]:
%%bash
n=10
for (( i = 1 ; i <= $n ; i++ ))
do
for (( j = 1 ; j <= $i ; j++ ))
do
echo -e "*\c"
done
echo ""
done
*
**
***
****
*****
******
*******
********
*********
**********
In [ ]:
%%bash
n=5
for(( i = 1; i <= $n; i++))
do
for(( j = 1; j <= $n; j++))
do
echo -n "$j "
done
echo ""
done
1
1
1
1
1
2
2
2
2
2
3
3
3
3
3
4
4
4
4
4
Bài 3:
In [ ]:
%%bash
5
5
5
5
5
%%bash
apt install bc
Reading package lists...
Building dependency tree...
Reading state information...
The following NEW packages will be installed:
bc
0 upgraded, 1 newly installed, 0 to remove and 39 not upgraded.
Need to get 86.2 kB of archives.
After this operation, 223 kB of additional disk space will be used.
Get:1 bionic/main amd64 bc amd64 1.07.1-2 [86.2 kB]
Fetched 86.2 kB in 0s (182 kB/s)
Selecting previously unselected package bc.
(Reading database ... 155455 files and directories currently installed.)
Preparing to unpack .../archives/bc_1.07.1-2_amd64.deb ...
Unpacking bc (1.07.1-2) ...
Setting up bc (1.07.1-2) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
In [ ]:
%%shell
echo -e "Nhap vao he so a: \c"; read a
echo -e "Nhap vao he so b: \c"; read b
if [ $a -eq 0 ]
then
if [ $b -eq 0 ]
then
printf "%s \n " "Phuong trinh co vo so nghiem."
else
printf "%s \n " "Phuong trinh vo nghiem."
fi
else
x=`echo "scale=2; -$b / $a" | bc`
printf "Phuong trinh co nghiem la x=%.2f\n " $x
fi
Nhap vao he so a: 19
Nhap vao he so b: 6
Phuong trinh co nghiem la x=-0.31
Out[ ]:
Bài 4:
In [ ]:
%%shell
echo -e "Nhap n: \c"; read n
sum=0
for(( i = 1; i <= $n; i++))
do
sum=`expr $sum + $i`
done
echo "Tong tu 1 den $n la: $sum"
Nhap n: 9
Tong tu 1 den 9 la: 45
Out[ ]:
Bài 5:
In [ ]:
%%shell
flag=0
while [ $flag = 0 ]
do
echo -e "Hien thi Bang Cuu Chuong so: \c";read number
for(( i = 1 ; i <= 10; i++))
do
echo "$i x $number = `expr $i \* $number`"
done
echo -e "Nhap vao phim so 0 de tiep tuc. Phim bat ki de ket thuc: \c"
read flag
done
Hien thi Bang Cuu Chuong so: 9
1 x 9 = 9
2 x 9 = 18
3 x 9 = 27
4 x 9 = 36
5 x 9 = 45
6 x 9 = 54
7 x 9 = 63
8 x 9 = 72
9 x 9 = 81
10 x 9 = 90
Nhap vao phim so 0 de tiep tuc. Phim bat ki de ket thuc: vuphuong
Out[ ]:
Bài 6:
In [ ]:
%%shell
echo -e "So thu muc can tao: \c"; read n
for(( i = 1; i <= $n; i++ ))
do
mkdir "user_$i"
if [ $? -eq 0 ]
then
echo "Thu muc user_$i tao thanh cong"
else
echo "Thu muc user_$i tao khong thanh cong"
fi
done
So thu muc can
Thu muc user_1
Thu muc user_2
Thu muc user_3
Thu muc user_4
Out[ ]:
tao: 4
tao thanh
tao thanh
tao thanh
tao thanh
cong
cong
cong
cong