Viết kịch bản 441echo "/ tmp/tempstuff tìm thấy; aborting!"lối raFiMảnh này gây ra kịch bản để thoát khỏi nếu tập tin/tmp/tempstuffis trình bày và là lớn hơn hơn 0 byte. Thenkeyword đánh dấu sự khởi đầu của một loạt các dây chuyền thực hiện chỉ khi có điều kiện là đúng, và fi(ifbackward) đánh dấu sự kết thúc của ifblock. Mã như vậy có thể thể hữu ích nếu các kịch bản tạo ra và rồi sau đó xoá tệp này, bởi vì sự hiện diện của nó cho thấy rằng một chạy trước của kịch bản không thành công hoặc vẫn đang thực hiện.Một hình thức thay thế cho một biểu hiện có điều kiện sử dụng testkeyword thay vì dấu ngoặc vuông xung quanh có điều kiện:nếu kiểm tra -s /tmp/tempstuffBạn cũng có thể kiểm tra giá trị trả lại một lệnh bằng cách sử dụng lệnh như là điều kiện:Nếu [lệnh]sau đólệnh bổ sungFiTrong ví dụ này, bổ sung commandswill chạy chỉ khi commandcompletes thành công. Nếu commandreturns một mã lỗi, bổ sung commandswon't được chạy.Biểu hiện có điều kiện có thể được mở rộng bằng cách sử dụng elseclause:Nếu [có điều kiện biểu hiện]sau đólệnhkháckhác-lệnhFiQuy tắc này nguyên nhân hình thức hoặc khác – commandsor commandsto thực hiện, tùy thuộc vào đánh giá các biểu hiện có điều kiện. Điều này là hữu ích nếu somethingshould xảy ra trong một phần của chương trình nhưng chính xác những gì sẽ xảy ra phụ thuộc vào một số điều kiện. Cho Ví dụ, bạn có thể muốn để khởi động một trong hai khác nhau tập tin lưu trữ chương trình tùy thuộc trên đầu vào của người dùng.Những gì bạn làm nếu nhiều hơn hai kết quả là có thể-ví dụ, nếu một người sử dụng có thể cung cấp cho bất kỳ một trong bốn có thể đầu hàng vào? Bạn có thể làm tổ một số nếu/đó/elseclauses, nhưng điều này được khó khăn rất nhanh chóng. Một cách tiếp cận sạch hơn là sử dụng trường hợp:trường hợp từ trongpattern1) command(s);pattern2) command(s);...esac442 chương 9 ■ viết kịch bản, cấu hình Email, và bằng cách sử dụng cơ sở dữ liệuCho một casestatement, một wordis có khả năng là một biến, và mỗi patternis một tốt giá trị của biến đó. Các mô hình có thể được mở rộng giống như tên tập tin, sử dụng cùng một ký tự đại diện và mở rộng quy tắc (* đứng cho bất kỳ chuỗi, ví dụ). Bạn có thể phù hợp với một số tùy ý của các mô hình theo cách này. Mỗi tập các lệnh phải kết thúc với một đôi dấu chấm phẩy (;), và casestatement như một toàn thể kết thúc trong chuỗi esac(casebackward).Khi thực hiện, bashexecutes lệnh liên kết với các mô hình đầu tiên để phù hợp với từ. Thực hiện sau đó nhảy vào dòng sau esacstatement; bất kỳ can thiệp lệnh không thực hiện. Nếu không có mô hình phù hợp với từ, không có mã số trong vòng casestatement thực hiện. Nếu bạn muốn có một tình trạng mặc định, sử dụng * như các mô hình cuối cùng; Mô hình này phù hợp với bất kỳ từ nào, do đó, lệnh của nó sẽ thực hiện nếu không có patternmatches khác.Sử dụng các vòngBiểu hiện có điều kiện đôi khi được sử dụng trong vòng. Vòng là cấu trúc cho biết các script to perform the same task repeatedly until some condition is met (or until some condition is no longer met). For instance, Listing 9.5 shows a loop that plays all the .wavaudio files in a directory.Listing 9.5: A script that executes a command on every matching file in a directory#!/bin/bashfor d in `ls *.wav` ; doaplay $ddoneThe aplaycommand is a basic audio file player that works with the Advanced Linux Sound Architecture (ALSA) audio drivers. On some systems, you may need to use playor some other command instead of aplay.The forloop as used here executes once for every item in the list generated by ls *.wav. Each of those items (filenames) is assigned in turn to the $dvariable and so is passed to the aplaycommand.The seqcommand can be useful in creating forloops (and in other ways, too): This command generates a list of numbers starting from its first argument and continuing to its last one. For instance, typing seq 1 10generates 10 lines, each with a number between 1 and 10. You can use a forloop beginning for x in `seq 1 10`to have the loop execute 10 times, with the value of $xincrementing with each iteration. If you pass just one parameter to seq, it interprets that number as an ending point, with the starting point being 1. If you pass three values to seq, it interprets them as a starting value, an increment amount, and an ending value.Another type of loop is the whileloop, which executes for as long as its condition is true. The basic form of this loop type is like this:Writing Scripts 443while [ condition]docommandsdoneThe untilloop is similar in form, but it continues execution for as long as its condition is false—that is, until the condition becomes true.Using FunctionsA functionis a part of a script that performs a specific subtask and that can be called by name from other parts of the script. Functions are defined by placing parentheses after the function name and enclosing the lines that make up the function within curly braces:myfn() {commands}The keyword functionmay optionally precede the function name. In either event, the function is called by name as if it were an ordinary internal or external command.Functions are very useful in helping to create modular scripts. For instance, if your script needs to perform half a dozen distinct computations, you may place each computation in a function and then call them all in sequence. Listing 9.6 demonstrates the use of functions in a simple program that copies a file but aborts with an error message if the target file already exists. This script accepts a target and a destination filename and must pass those filenames to the functions.Listing 9.6: A script demonstrating the use of functions#/bin/bashdoit() {cp $1 $2}function check() {if [ -s $2 ]thenecho “Target file exists! Exiting!”exitfi}check $1 $2doit $1 $2
đang được dịch, vui lòng đợi..
