Danh mục

Bài giảng Linux và phần mềm mã nguồn mở - Chương 15: Lập trình Shell

Số trang: 38      Loại file: pdf      Dung lượng: 769.44 KB      Lượt xem: 29      Lượt tải: 0    
10.10.2023

Phí tải xuống: 15,000 VND Tải xuống file đầy đủ (38 trang) 0
Xem trước 4 trang đầu tiên của tài liệu này:

Thông tin 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 Shell. Chương này cung cấp cho sinh viên những nội dung gồm: lập trình shell là gì; soạn và thực thi chương trình shell; biến trong shell; định nghĩa các biến của người dùng; quy tắc đặt tên biến; các phép toán số học;... Mời các bạn cùng tham khảo chi tiết nội dung bài giảng!
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ài liệu được xem nhiều:

Gợi ý tài liệu liên quan: