Bài giảng Hệ điều hành linux: Chương 6 - Ngô Văn Công

24 61 0
Bài giảng Hệ điều hành linux: Chương 6 - Ngô Văn Công

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Bài giảng Hệ điều hành linux: Chương 6 Lập trình shell do Ngô Văn Công biên soạn cung cấp các kiến thức cơ bản như: Gọi thực hiện lệnh trong Shell, biến trong Shell, câu lệnh alias, tùy biến môi trường, điều khiển công việc, viết chương trình Shell,...

Lập trình shell Ngơ Văn Cơng Overview       Gọi thực lệnh Shell Biến Shell Câu lệnh alias Tùy biến môi trường Điều khiển cơng việc Viết chương trình Shell Invoking the Shell  bash [options] [arguments] Bash related special files    /etc/profile – system initialization file ~/.bash_profile(.bashrc) - personal initialization file ~/.bash_history - record commands history Biến shell     Sử dụng lập trình shell điều khiển môi trường Gán giá trị cho biến: variable_name=value Truy cập vào giá trị biến: $variable_name $ foo="hello there" $ echo $foo hello there export: export variables to the environment or sub-shells Environment Variables        HOME - your home directory SHELL - executable program for the current shell PATH - paths to locate executable files USER - your username TERM - type of current terminal DISPLAY - your X-Window display PS1 - command prompt Aliasing Commands      Substituing a string by a word Making commands with simple, short, useful options alias - create or list aliases unalias - remove aliases $ alias alias cp=’cp -i’ alias ll=’ls -l color=tty’ alias ls=’ls color=tty’ alias mv=’mv -i’ alias rm=’rm -i’  $ unalias ll Customizing Environment   Setting the environment variables Putting them in ~/.bash_profile(.bashrc) for later use Lập trình shell       Một số thuộc tính Shell Shell script Các biến Biểu thức so sánh Câu lệnh điều khiển Hàm(Functions) Một số thuộc tính khác  Biểu thức tốn học $ let "a = + 1" $ echo $a Shell Script      Là tập tin văn chứa cấu trúc điều khiển lệnh Shell Thông dịch hàng Shell Cấp quyền chạy(execute) Có thể gọi từ shell script khác Tham số truyền từ dòng lệnh First Program $ cat > myscript.sh #!/bin/sh echo "Hello, world." save myscript.sh chmod +x myscript.sh /myscript.sh /myscript.sh bash: /hello.sh: Permission denied Hello, world "Hello" program $ cat > hello.sh #!/bin/bash # This is a comment: simple hello shell script echo "Enter your name:" read name echo "Hello $name, have a nice day!" ^D $ /hello.sh bash: /hello.sh: Permission denied $ chmod +x hello.sh $ /hello.sh Enter your name: Cong Hello Cong, have a nice day! Các biến      Các biến môi trường Các biến xây dựng sẵn(Built-in variables) Các biến người dùng(User variables) Biến lưu trữ số hay ký tự Chuỗi kỹ tự phải đặt dấu nháy kép hay nháy đơn Vị trí tham số    Các tham số tham chiếu theo vị trí Các biến có sẵn chương trình shell  $# - Số lượng tham số truyền vào  $0 - Tên shell script  $* - Trả tất tham số truyền vào  $n - Tham chiếu đến tham số thứ n(1-9) $./myscript source dest  $0 = /myscript  $1 = source  $2 = dest Các biến đặc biệt   $$ - ID tiến trình $? - Trạng thái kết thúc tiến trình cuối Các hàm toán học   Các phép toán: +, -, *, /, % Sử dụng Let thực hàm toán học let "sum = + 3" let "area = $len * $width" let "percent = $num / 100" let "remain = $n % $d" Biểu thức so sánh     [ expression ] So sánh chuỗi ký tự: =, !=, -n, -z So sánh số: -eq, -lt, -gt, -ne Phép toán tập tin: -d, -f, , -e, -x Phép toán logic: !, -o, -a [ s1 = s2 ] [ $num -lt 100 ] [ -d mydir ] [ ! -f myfile ] Câu lệnh điều kiện  IF statement if [ expression ]; then statements elif [ expression ]; then statements else statements fi  Example if [ $1 ="" ]; then echo "Enter value:" read num else let "num = $1" fi Câu lệnh điều kiện (cont)  CASE statement case $var in val1) statements;; val2) statements;; *) statements;; esac  Example case $1 in 1) echo "One";; 2) echo "Two";; *) echo "Unknown";; esac Câu lệnh lặp  FOR statement for var [in list]; statements done  Example let "sum = 0" for num in 5; let "sum = $sum + $num" done echo $sum Câu lệnh lặp(cont)  WHILE statement while [ expression ]; statements done  Example let "num = 0" while [ $num -lt 10 ]; echo $num let "num= $num + 2" done Một số câu lệnh khác     BREAK - exit a loop CONTINUE - skip to end of current while or for loop EXIT - end the shell script RETURN - return from funtion or shell script if [ $# -lt ]; then echo "Usage: ‘basename $0‘ source dest" exit fi Hàm  Function format function func_name { statements }  Function called func_name param1 param2 Parameters access $1, $2, $3, Gở lỗi (Debugging)  Hiển thị câu lệnh tham số chạy câu lệnh $ bash -x command ... tự: =, !=, -n, -z So sánh số: -eq, -lt, -gt, -ne Phép toán tập tin: -d, -f, , -e, -x Phép toán logic: !, -o, -a [ s1 = s2 ] [ $num -lt 100 ] [ -d mydir ] [ ! -f myfile ] Câu lệnh điều kiện ... vị trí Các biến có sẵn chương trình shell  $# - Số lượng tham số truyền vào  $0 - Tên shell script  $* - Trả tất tham số truyền vào  $n - Tham chiếu đến tham số thứ n( 1-9 ) $./myscript source... sub-shells Environment Variables        HOME - your home directory SHELL - executable program for the current shell PATH - paths to locate executable files USER - your username TERM - type

Ngày đăng: 30/01/2020, 00:38

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan