Chatbox

Các bạn vui lòng dùng từ ngữ lịch sự và có văn hóa,sử dụng Tiếng Việt có dấu chuẩn. Chúc các bạn vui vẻ!
14/02/2014 20:02 # 1
vnttqb
Cấp độ: 13 - Kỹ năng: 8

Kinh nghiệm: 5/130 (4%)
Kĩ năng: 39/80 (49%)
Ngày gia nhập: 21/03/2011
Bài gởi: 785
Được cảm ơn: 319
[Lập Trình Shell] Xử Lý Mảng


1. Khai báo một mảng và gán giá trị cho mảng.
array[index]
hoặc:  declare -a arrayname=(e1 e2 e3 ...)
2. Lấy giá trị của 1 phần tử trong mảng
${array_name[index]}
3. In toàn bộ các giá trị trong mảng:
${array_name[#]}
${array_name
  • }
  • 4. Xác định  độ dài mảng - tổng số phần tử trong mảng:
    ${#array_name[#]}
    ${#array_name
  • }
  • 5. Độ dài của một phần tử bất kỳ trong mảng:
    ${#array_name[index]}
    6. Tách phần tử trong mảng:
    Bạn muốn lấy 2 phần tử bắt đầu từ phần tử thứ 3 trong mảng thì làm như thế nào?
    ${array_name[@]:3:2}
    Ví dụ trên bạn muốn lấy 4 ký tự đầu tiên của phần tử thứ 2 thì làm như thế nào?
    ${array_name[2]:0:2}
    7. Tìm và thay thế phần tử trong mảng:
    ${array_name[@]/pt_find/pt_replace}
    8. Gán phần tử mới vào mảng đã có sẵn:
    vd:
     $cat arraymanip.sh
    Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
    Unix=("${Unix[@]}" "AIX" "HP-UX")
    echo ${Unix[@]}
    echo ${Unix[7]}

    $./arraymanip.sh
    Debian Red hat Ubuntu Suse Fedora UTS OpenLinux AIX HP-UX
    AIX
    9. Xóa một phần tử trong mảng:
    unset array_name[index]
    ví dụ:
    $ cat arraymanip.sh
    Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
    pos=3
    Unix=(${Unix[@]:0:$pos} ${Unix[@]:$(($pos + 1))})
    echo ${Unix[@]}

    $./arraymanip.sh
    Debian Red hat Ubuntu Fedora UTS OpenLinux
    10. Xóa phần tử theo mẫu (patter)
    $ cat arraymanip.sh

    #!/bin/bash

    declare -a Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora');

    declare -a patter=( ${Unix[@]/Red*/} )

    echo ${patter[@]}

    $ ./arraymanip.sh

    Debian Ubuntu Suse Fedora
    11. Copy một mảng
     #!/bin/bash
    Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
    Linux=("${Unix[@]}")
    echo ${Linux[@]}

    $ ./arraymanip.sh
    Debian Red hat Ubuntu Fedora UTS OpenLinux
    12. Nối 2 hay nhiều mảng lại với nhau
     $cat arraymanip.sh
    #!/bin/bash
    Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
    Shell=('bash' 'csh' 'jsh' 'rsh' 'ksh' 'rc' 'tcsh');

    UnixShell=("${Unix[@]}" "${Shell[@]}")

    echo ${UnixShell[@]}
    echo ${#UnixShell[@]}

    $ ./arraymanip.sh
    Debian Red hat Ubuntu Suse Fedora UTS OpenLinux bash csh jsh rsh ksh rc tcsh
    14
    13. Load một file vào mảng
     #Example filecat logfile
    Welcome
    to
    thegeekstuff
    Linux
    Unix

    cat loadcontent.sh#!/bin/bashfilecontent=( `cat "logfile" `)

    for 
    t in "${filecontent[@]}"do
    echo 
    $t
    done
    echo "Read file content!"
    $ ./loadcontent.sh
    Welcome
    to
    thegeekstuff
    Linux
    Unix
    Read file content
    !


    Nguồn: Sưu tầm tutorial



    ======================================================================================================

    Cuộc đời là một dòng sông. Ai không bơi thì chết. 
     

    Name: Tien (Tory) TRAN
    Email: TranTien29@gmail.com


     
    Copyright© Đại học Duy Tân 2010 - 2024