Bài giảng Linux và phần mềm mã nguồn mở - Chương 15: Lập trình Shell
Thông tin tài liệu:
Nội dung trích xuất từ tài liệu:
Bài giảng Linux và phần mềm mã nguồn mở - Chương 15: Lập trình ShellLậptrìnhShellTrươngDiệuLinh Lậptrìnhshelllàgì• ShelllàtrìnhthôngdịchlệnhcủaLinux – Thườngtươngtácvớingườidùngtheotừngcâu lệnh. – Shellđọclệnhtừbànphímhoặcfile – NhờhạtnhânLinuxthựchiệnlệnh• Shellscript – Cácchươngtrìnhshell,baogồmchuỗicáclệnh. Soạnvàthựcthichươngtrìnhshell• Sửdụngmọitrìnhsoạnthảodạngtext: – vi,emacs,gedit – Nộidungbaogồmcáccâulệnhđượcsửdụngtrêndòng lệnhcủaLinux – Cáccâulệnhtrêncùng1dòngphảiphântáchbằngdấu;• Thiếtlậpquyềnthựcthichochươngtrìnhshell – chmodo+xten_file• Thựcthi – bashten_file – shten_file – ./ten_file Vídụshellđơngiản• $vifirst# My first shell scriptclearecho Hello $USERecho –e Today is c ;dateecho –e Number of user login : c ; who |wc –lecho Calendar• $chmod755first• $./first Biếntrongshell• TrongLinuxshellcó2loạibiến: – Biếnhệthống: • TạoravàquảnlýbởiLinux. • TênbiếnlàCHỮHOA – Biếndongườidùngđịnhnghĩa • Tạoravàquảnlýbởingườidùng • Tênbiếnlàchữthường – Xemhoặctruynhậpgiátrịcácbiến: • $tên_biến • echo$HOME • echo$USERNAME Phảicódấu$trướctênbiếndivided into small locations, and each location had unique number called memory location/address,which is used to hold our data. Programmer can give a unique name to this memory location/addresscalled memory variable or variable (Its a named storage location that may take different values, but onlyone at a time). MộtsốbiếnhệthốngIn Linux (Shell), there are two types of variable:(1) System variables - Created and maintained by Linux itself. This type of variable defined inCAPITAL LETTERS.(2) User defined variables (UDV) - Created and maintained by user. This type of variable defined inlower letters.You can see system variables by giving command like $ set, some of the important System variables are: System Variable MeaningBASH=/bin/bash Our shell nameBASH_VERSION=1.14.7(1) Our shell version nameCOLUMNS=80 No. of columns for our screenHOME=/home/vivek Our home directoryLINES=25 No. of columns for our screenLOGNAME=students students Our logging nameOSTYPE=Linux Our Os typePATH=/usr/bin:/sbin:/bin:/usr/sbin Our path settingsPS1=[u@h W]$ Our prompt settingsPWD=/home/students/Common Our current working directorySHELL=/bin/bash Our shell nameUSERNAME=vivek User name who is currently login to this PCNOTE that Some of the above settings can be different in your PC/Linux environment. You can print anyof the above variables contains as follows:$ echo $USERNAME$ echo $HOMEExercise:1) If you want to print your home directory location then you give command: Địnhnghĩacácbiếncủangườidùng• Cúpháp: tên_biến=giá_trị • Ingiátrịcủabiến echo$tên_biến• Vídụ:no=10echo$no Quytắcđặttênbiến • Tênbiếnphảibắtđầubằngkýtự – HOME – SYSTEM_VERSION – no – vech• Khôngđượcđểdấucáchhaibêntoántử=khi gángiátrịchobiến no=10#làđúng no=10#làsai no=10#làsai Quytắcđặttênbiến • Tênbiếncóphânbiệtchữhoa,thường – Cácbiếnsauđâylàkhácnhau: no=10 No=11 NO=20 nO=2• Mộtbiếnkhôngcógiátrịkhởitạothìbằng NULL• Khôngđượcdùngdấu?,*đểđặttêncácbiếnExerciseQ.1.How to Define variable x with value 10 and print it on screen. VídụQ.2.How to Define variable xn with value Rani and print it on screenQ.3.How to print sum of two numbers, lets say 6 and 3?Q.4.How to define two variable x=20, y=5 and then to print division of x and y (i.e. x/y)Q.5.Modify above and store division of x and y to variable called zQ.6.Point out error if any in following script$ vi variscript### Script to test MY knowledge about variables!#myname=Vivekmyos = TroubleOSmyno=5echo My name is $mynameecho My os is $myosecho My number is myno, can you see this numberFor Answers Click herePrev Home Lệnhecho• Cúpháp: echo[opŒon][string,variables…]• Inmộtsốkýtựđặcbiệttrongthamsốvớitùychọn-e: aalert(bell) backspace c ...
Tìm kiếm theo từ khóa liên quan:
Bài giảng Linux và phần mềm mã nguồn mở Linux và phần mềm mã nguồn mở Lập trình Shell Thực thi chương trình shell Chương trình shell Quy tắc đặt tên biến Lệnh đọc dữ liệu đầu vàoGợi ý tài liệu liên quan:
-
Giáo trình Hệ điều hành Linux (Ngành: Công nghệ thông tin) - CĐ Công nghiệp Hải Phòng
103 trang 195 0 0 -
Bài giảng Linux và phần mềm mã nguồn mở: Bài 10 - Trương Xuân Nam
19 trang 90 0 0 -
Đề thi học kì 1 môn Tin học lớp 10 năm 2023-2024 - Trường THPT Hồ Nghinh, Quảng Nam
4 trang 31 2 0 -
Giáo trình Hệ điều hành Unix - Linux
212 trang 28 0 0 -
Bài giảng Linux và phần mềm mã nguồn mở: Bài 2 - Trương Xuân Nam
15 trang 27 0 0 -
Bài giảng Phần mềm nguồn mở: Bài 4 - Đoàn Thiện Ngân
10 trang 27 0 0 -
47 trang 25 0 0
-
Bài giảng Linux và phần mềm mã nguồn mở: Bài 1 - Trương Xuân Nam
33 trang 24 0 0 -
Bài giảng Quản trị mạng Linux: Bài 2 - TC Việt Khoa
25 trang 24 0 0 -
Bài giảng Linux và phần mềm mã nguồn mở: Bài 8 - Trương Xuân Nam
26 trang 24 0 0 -
Bài giảng Lập trình Shell (2017)
0 trang 24 0 0 -
Bài giảng Linux và phần mềm mã nguồn mở: Bài 3 - Trương Xuân Nam
26 trang 23 0 0 -
Bài giảng Linux và phần mềm mã nguồn mở: Bài 12 - Trương Xuân Nam
29 trang 23 0 0 -
Bài giảng Linux và phần mềm mã nguồn mở: Bài 4 - Trương Xuân Nam
36 trang 23 0 0 -
Bài giảng Linux và phần mềm mã nguồn mở - Chương 10: Sao lưu
23 trang 22 0 0 -
Bài giảng Linux và phần mềm mã nguồn mở: Bài 11 - Trương Xuân Nam
23 trang 22 0 0 -
Bài giảng Phát triển ứng dụng: Chương 3.5
17 trang 21 0 0 -
Bài giảng Linux và phần mềm mã nguồn mở: Bài 6 - Trương Xuân Nam
44 trang 21 0 0 -
Bài giảng Linux và phần mềm mã nguồn mở - Chương 7: Quản lý phần mềm và dịch vụ
10 trang 18 0 0 -
Bài giảng Hệ điều hành linux: Chương 6 - Ngô Văn Công
24 trang 18 0 0