Lập trình shell (Tiếp Theo 3)

2.2 Điều kiện. (Condition)

2.2.1 Lệnh test / [].

Cách sử dụng của 2 lệnh là như nhau nhưng tôi thường sử dụng lệnh [] nhiều hơn là lệnh test. Cấu trúc của lệnh test: test -f <filename>, lệnh []: [ -f <filename> ] ta đặc dấu khoảng cách giữa lệnh [].

ví dụ:

if test -f hello.c
then
 ….....
fi

fi [ -f hello.c ]
then
 …....
fi

So sánh chuỗi:

So sánh

Kết quả

string1 = string2

True khi 2 chuỗi bằng nhau

string1 != string2

True khi 2 chuỗi khác nhau

-n string1

True nếu string1 không rỗng not null

-z string1

True nếu string1rỗng null

So sánh toán học:

So sánh

Kết quả

Expression1 -eq Expression2

True khi 2 biểu thức bằng nhau

Expression1 -nq Expression2

True khi exp1 != exp2

Expression1 -gt Expression2

True khi exp1 > exp2

Expression1 -ge Expression2

True khi exp1 >= exp2

Expression1 -lt Expression2

True khi exp1 < exp2

Expression1 -le Expression2

True khi exp1 <= exp2

! expression

Toán tử đảo not

Kiểm tra điều kiện tập tin:

So sánh

Kết quả

-d file

True nếu file là thư mục

-f file

True nếu file là tập tin

-s file

True nếu kích thước file != 0

-u file

True nếu set-user-id được thiết lập trên file

-g file

True nếu set-group-id được thiết lập trên file

-r file

True nếu file được phép ghi

-w file

True nếu file được phép viết

-x file

True nếu file được phép đọc

2.2.2 Cấu trúc điều kiện:

Lệnh if:

Cấu trúc lệnh if

if condition
then
 statements
else
 statements
fi

Ví dụ: if.sh

# !bin/sh

echo "Do you love me? Please answer yes or no"
read answer
if [ "$answer"= "yes" ]; then
 echo "I love you ^^"
else
 echo "I hate you"
fi

exit 0

Biến $answer được bỏ trong dấu nháy kép để trách trường hợp người dùng không nhập ký tự nào cả.

$ chmod +x if.sh
$ ./if.sh
Do you love me? Please answer yes or no
yes
I love you ^^

Biến $answer bạn không bỏ trong dấu ngoặc kép và bạn nhấp enter lúc trả lời câu hỏi thì hệ thống sẽ thông báo lỗi sau và trả về kết quả trong đoạn else vậy nếu người dùng nhập sai hoặc một ký tự bất kỳ nào mà không phải NO thì cũng sẽ trả về đoạn else này.

./if.sh
Do you love me? Please answer yes or no
./if.sh: line 6: [: =: unary operator expected
I hate you

Lệnh elif:

Ví dụ: if.sh

# !bin/sh

echo "Do you love me? Please answer yes or no"
read answer
if [ "$answer"= "yes" ]; then
 echo "I love you ^^"
elif [ "$answer"= "no" ]; then
 echo "I hate you"
else
 echo "Sorry, $answer not recognized. Enter yes or no”
 exit 1
fi

exit 0

$ chmod +x elif.sh
$ ./elif.sh
Do you love me? Please answer yes or no
no
I hate you

2.3 Vòng lặp.

2.3.1 Lệnh for

Sủ dụng for khi biết trước số lần lặp, cấu trúc lệnh for.

for variable in values
do
 statements
done

Ví dụ:

$ nano for.sh

# !bin/sh
for i in N T K Q
do
 echo $i
done

exit 0

$ chmod +x for.sh
$ ./for.sh
N
T
K
Q

2.3.2 Lệnh while

Sủ dụng while khi chưa biết trước số lần lặp.

while condition do
 statements
done

Ví dụ:

$ nano while.sh

# !bin/sh

echo "Passwork"
read pk
while [ "$pk" != "NTKQ" ]; do
 echo "Sorry, try again"
 read pk
done

exit 0

$ chmod +x while.sh
$ ./while.sh
Passwork
NTKQ

Ví dụ:

$ nano while_expr.sh

# !/bin/sh

i=1

while [ "$i" -le 3 ]; do

echo "Here $i"

i=$(($i+1))

done

exit 0

$ chmod +x while_expr.sh

$ ./while_expr.sh

Here 1

Here 2

Here 3

Lưu ý $(()) tương tự như lệnh chuyển chuỗi thành số.

2.3.3 Lệnh until

Sủ dụng tương tự như lệnh while nưng điều kiện kiểm tra bị đảo ngược, vòng lặp sẽ dừng nếu điều kiện kiểm tra là đúng.

until condition do
 statements
done

Ví dụ:

$ nano until.sh

# !/bin/sh

echo "Locate for user ... "
until who | grep "$1" > /dev/null; do
 sleep 60
done

echo -e \\a
echo "******* $1 has jusst logged in ********"

exit 0

$ chmod +x until.sh
$ ./until.sh user
Locate for user ...
******* has just logged in ********

Lệnh lọc ra danh sách user đăng nhập vào hệ thống, sau đó sanh sách này được lọc bằng lệnh grep để lấy user theo biến môi trường $1 có biến môi trường cũng là user đối với máy mình và dữ liệu lọc ra được lưu vào tập tin rỗng /dev/null trả về giá trị True.

2.3.3 Lệnh case

Lệnh case cho phép bạn so khớp nội dung của một biến với các trường hợp khác nhau:

case variable in
 pattern [ | pattern ] … ) statements;;
 pattern [ | pattern ] … ) statements;;
 * ) default;;
esac

Ví dụ:

$ nano case.sh

# !/bin/sh

echo "Do you love me? Please answer yes or no"
read answer
case "$answer" in
 "yes" | "y" | "Yes" | "Y" ) echo "I love you ^^";;
 "n*" | "N*" ) echo "I hate you";;
 * ) echo "Sorry, $answer not recognized. Enter yes or no";;
esac

exit 0

$ nano case.sh
$ chmod +x case.sh
$ ./case.sh
Do you love me? Please answer yes or no
y
I love you ^^

Bài viết các bạn có thể tham khảo:

Viết một bình luận

This site uses Akismet to reduce spam. Learn how your comment data is processed.