Bài 1
Ví dụ 1
In [ ]:
%%bash
touch firstscript
In [ ]:
clear
echo "This is my first script!!!">firstscript
In [ ]:
%%bash
chmod 755 firstscript
ls -l
./firstscript
total 8
-rwxr-xr-x 1 root root
40 Apr 4 07:25 firstscript
drwxr-xr-x 1 root root 4096 Mar 23 14:22 sample_data
This is my first script!!!
Bài tập 1
In [ ]:
%%bash
clear
echo "Hello $USER"
echo -e "Today is \c ";date
echo -e "Number of user login : \c" ; who | wc -l
echo -e "Calendar"
cal
exit 0
Hello
Today is Mon Apr 4 04:33:06 UTC 2022
Number of user login : 0
Calendar
April 2022
Su Mo Tu We Th Fr Sa
1 2
3 _ _4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Bài tập 2
In [ ]:
%%bash
myname=Vivek
myos = TroubleOS
myno=5
echo "My name is $myname"
echo "My os is $myos"
echo "My number is myno, can you see this number"
My name is Vivek
My os is
My number is myno, can you see this number
bash: line 2: myos: command not found
Dòng 2 bị lỗi. Lý do: do cách khai báo biến sai(Có khoảng trống 2 bên dấu bằng)
Bài tập 3
In [ ]:
%%bash
expr 1 + 3
echo $?
echo Welcome
echo $?
wildwest canwork ?
echo $?
date
echo $?
4
0
Welcome
0
127
Mon Apr
0
4 04:42:29 UTC 2022
bash: line 5: wildwest: command not found
Ví dụ 2
In [ ]:
%%bash
mkdir -p /root/shellscript/
touch /root/shellscript/hello
In [ ]:
%%bash
chmod 775 /root/shellscript/hello
sh /root/shellscript/hello
Hello World!!
Today is
Mon Apr 4 05:58:26 UTC 2022
Ví dụ 3
In [ ]:
%%bash
echo $BASH
echo $PWD
/bin/bash
/content
Ví dụ 4
In [ ]:
%%bash
no=10
n=100
echo $no
echo $n
10
100
Ví dụ 5
In [ ]:
%%bash
No=99
no=10
vech=
echo $No
echo $no
echo $vech
n=66
echo $n
echo "My name is $LOGNAME"
99
10
66
My name is
Ví dụ 6
In [ ]:
%%bash
echo -e "\t Khoa CNTT & TT \n\t Dai Hoc Can Tho"
Khoa CNTT & TT
Dai Hoc Can Tho
Ví dụ 7
In [ ]:
%%bash
expr 1 + 3
expr 2 - 1
expr 10 / 2
expr 20 % 3
expr 10 \* 3
4
1
5
2
30
Ví dụ 8
In [ ]:
%%bash
echo "Tong cua 6 va 3 la: `expr 6 + 3`"
Tong cua 6 va 3 la: 9
Ví dụ 9
In [ ]:
%%bash
echo "Tong cua 6 va 3 la: `expr 6 + 3`"
echo "Tong cua 6 va 3 la: 'expr 6 + 3'"
echo "Tong cua 6 va 3 la: "expr 6 + 3""
Tong cua 6 va 3 la: 9
Tong cua 6 va 3 la: 'expr 6 + 3'
Tong cua 6 va 3 la: expr 6 + 3
Ví dụ 10
In [ ]:
%%bash
rm unknowfile
echo $?
1
rm: cannot remove 'unknowfile': No such file or directory
Ví dụ 11
In [ ]:
%%shell
echo "Your first name please:"
read ten
echo "Hello, $ten, let's be friend!"
Your first name please:
Vu Phuong
Hello, Vu Phuong, let's be friend!
Out[ ]:
In [ ]:
%%bash
echo "Today is:";date
Today is:
Mon Apr 4 07:32:40 UTC 2022
Ví dụ 12
In [ ]:
%%bash
touch demo
chmod 755 demo
In [ ]:
%%bash
./demo aa bb
Total number of command line argument are 2
./demo is script name
aa is first argument
bb is second argument
All of them are: aa bb or aa bb
Bài 2
In [ ]:
%%shell
echo "\\ ****************************************\\ "
echo
date
echo
echo
echo
echo
read
echo
echo
"Today is:"
"Hello $LOGNAME !!!"
"Your current working directory: $PWD"
"Your home directory: $HOME"
"Please press any key to finish:"
n
"
Thank you very much!!"
"\\ ****************************************\\ "
\****************************************\
Today is:
Mon Apr 4 07:36:12 UTC 2022
Hello !!!
Your current working directory: /content
Your home directory: /root
Please press any key to finish:
9
Thank you very much!!
\****************************************\
Out[ ]:
Bài 3
In [ ]:
%%bash
touch BAI_3
chmod 755 BAI_3
In [ ]:
%%bash
./BAI_3 8 9
Tham so ban da truyen vao la 2 so: 8 va 9
Tong: 8 + 9 = 17
Hieu: 8 – 9 = -1
Tich: 8 * 9 = 72
Thuong: 8 / 9 = 0
So du: 8 % 9 = 8
Bài 4
In [1]:
%%bash
touch taothumuc
In [ ]:
echo "Ten thu muc muon tao: "
read tm
mkdir $tm
kq=$?
if [ $kq -ne 0 ]
then
echo "Khong the tao duoc thu muc $tm"
fi
In [2]:
%%shell
chmod 755 taothumuc
./taothumuc
Ten thu muc muon tao:
Ten thu muc muon tao:
Phuong
Out[2]:
Bài 5
In [3]:
%%bash
touch thuchien
In [ ]:
$1 $2
status=$?
if [ $status -ne 0 ]
then
echo "Thuc thi khong thanh cong!"
else
echo "Thuc thi thanh cong!"
fi
In [4]:
%%shell
chmod 755 thuchien
./thuchien mkdir thumuc1
Thuc thi thanh cong!
Out[4]: