Many Linux startup scripts, including SysV startup scripts, are in fac dịch - Many Linux startup scripts, including SysV startup scripts, are in fac Việt làm thế nào để nói

Many Linux startup scripts, includi

Many Linux startup scripts, including SysV startup scripts, are in fact shell
scripts. Therefore, understanding shell scripting is necessary if you want
to modify a Linux startup script.
Like any programming task, shell scripting can be quite complex. Consequently, this chapter barely scratches the surface of what can be accomplished through shell scripting. Consult a book on the topic, such as
Cameron Newham’s Learning the Bash Shell, 3rd Edition(O’Reilly, 2005)
or Richard Blum and Christine Bresnahan’s Linux Command Line and Shell
Scripting Bible, 2nd Edition (Wiley, 2011), for more information.
To create a shell script, you must first know how to begin editing one. Once you do so,
you’ll find that one of the easiest tasks to do is to call external commands. More advanced
tasks include using variables and using conditional expressions.
434 Chapter 9 ■ Writing Scripts, Configuring Email, and Using Databases
Beginning a Shell Script
Shell scripts are plain-text files, so you create them in text editors. A shell script begins with
a line that identifies the shell that’s used to run it, such as the following:
#!/bin/sh
The first two characters are a special code that tells the Linux kernel that this is a script
and to use the rest of the line as a pathname to the program that’s to interpret the script.
(This line is sometimes called the shebang, hashbang, hashpling, or pound bangline.) Shell
scripting languages use a hash mark (#) as a comment character, so the script utility ignores
this line, although the kernel doesn’t. On most systems, /bin/shis a symbolic link that
points to /bin/bash, but it can point to some other shell. Specifying the script as using
/bin/shguarantees that any Linux system will have a shell program to run the script; but
if the script uses any features specifi c to a particular shell, you should specify that shell
instead—for instance, use /bin/bashor /bin/tcshinstead of /bin/sh.
When you’re done writing the shell script, you should modify it so that it’s executable.
You do this with the chmodcommand, as described in Chapter 4, “Managing Files.” Specifically, you use the +xoption to add execute permissions, probably in conjunction with a
to add these permissions for all users. For instance, to make a file called my-scriptexecutable, you should issue the following command:
$ chmod a+x my-script
You’ll then be able to execute the script by typing its name, possibly preceded by ./to tell
Linux to run the script in the current directory rather than searching the current path. If you
fail to make the script executable, you can still run the script by running the shell program
followed by the script name (as in bash my-script), but it’s generally better to make the
script executable. If the script is one you run regularly, you may want to move it to a location on your path, such as /usr/local/bin. When you do that, you won’t have to type the
complete path or move to the script’s directory to execute it; you can just type my-script.
It’s possible to set a script’s SUID or SGID bits. (See Chapter 4 for information about the SUID and SGID bits.) Doing so is potentially dangerous, particularly if the script is owned by root, for reasons described in
Chapter 4. You should therefore be very cautious about applying the
SUID bit to scripts.
Another way to run a script requires mention: sourcingit. You can source a script by
using the sourcekeyword or a dot (.), as follows:
$ source my-script
$ . my-script
Sourcing a script causes it to run in the current shell, as opposed to launching a new
instance of the shell, as occurs when you run a script by typing its name alone or using the
execcommand, as described in Chapter 1. This has some important implications:
Writing Scripts 435
■ When you source a script, it will have access to environment variables set in the calling
shell, even if you haven’t exported them. Ordinarily, only environment variables that
you explicitly export become available to scripts you run.
■ If you source a script and if that script sets an environment variable, that variable will
become available (or will be changed) in the calling shell. If you run the script normally, any environment variables it sets will remain local to it and to the programs that
it calls, even if the script exports the variables.
■ Running a script in the normal ways imposes overhead costs associated with launching
the new shell. These costs are normally negligible, but if a script calls itself recursively
or calls many other scripts, sourcing those scripts within the first script may improve
performance.
■ Sourcing a script causes it to execute in the calling shell’s language, whereas running a
script normally causes it to use the shell language specified on the hashbang line.
Using Commands
One of the most basic features of shell scripts is the ability to run commands. You can use
both shell internal commands and external commands. Most of the commands you type in
a shell prompt are external commands—they’re programs located in /bin, /usr/bin, and
other directories on your path. You can run such programs, as well as internal commands,
by including their names in the script. You can also specify parameters to such programs in
a script. For instance, suppose you want a script that launches two xtermwindows and the
KMail mail reader program. Listing 9.1 presents a shell script that accomplishes this goal.
Listing 9.1: A simple script that launches three programs
#!/bin/bash
/usr/bin/xterm &
/usr/bin/xterm &
/usr/bin/kmail &
Aside from the first line that identifies it as a script, the script looks just like the commands you might type to accomplish the task manually, except for one fact: The script lists
the complete paths to each program. This is usually not strictly necessary, but listing the
complete path ensures that the script will find the programs even if the PATHenvironment
variable changes. On the other hand, if the program files move (say, because you upgrade
the package from which they’re installed and the packager decides to move them), scripts
that use complete paths will break.
Each program-launch line in Listing 9.1 ends in an ampersand (&). This character tells
the shell to go on to the next line without waiting for the first to finish. If you omit the
ampersands in Listing 9.1, the effect will be that the first xtermwill open but the second
won’t open until the first is closed. Likewise, KMail won’t start until the second xterm
terminates.
0/5000
Từ: -
Sang: -
Kết quả (Việt) 1: [Sao chép]
Sao chép!
Many Linux startup scripts, including SysV startup scripts, are in fact shell scripts. Therefore, understanding shell scripting is necessary if you want to modify a Linux startup script.Like any programming task, shell scripting can be quite complex. Consequently, this chapter barely scratches the surface of what can be accomplished through shell scripting. Consult a book on the topic, such as Cameron Newham’s Learning the Bash Shell, 3rd Edition(O’Reilly, 2005) or Richard Blum and Christine Bresnahan’s Linux Command Line and Shell Scripting Bible, 2nd Edition (Wiley, 2011), for more information.To create a shell script, you must first know how to begin editing one. Once you do so, you’ll find that one of the easiest tasks to do is to call external commands. More advanced tasks include using variables and using conditional expressions.434 Chapter 9 ■ Writing Scripts, Configuring Email, and Using DatabasesBeginning a Shell ScriptShell scripts are plain-text files, so you create them in text editors. A shell script begins with a line that identifies the shell that’s used to run it, such as the following:#!/bin/shThe first two characters are a special code that tells the Linux kernel that this is a script and to use the rest of the line as a pathname to the program that’s to interpret the script. (This line is sometimes called the shebang, hashbang, hashpling, or pound bangline.) Shell scripting languages use a hash mark (#) as a comment character, so the script utility ignores this line, although the kernel doesn’t. On most systems, /bin/shis a symbolic link that points to /bin/bash, but it can point to some other shell. Specifying the script as using /bin/shguarantees that any Linux system will have a shell program to run the script; but if the script uses any features specifi c to a particular shell, you should specify that shell instead—for instance, use /bin/bashor /bin/tcshinstead of /bin/sh.When you’re done writing the shell script, you should modify it so that it’s executable. You do this with the chmodcommand, as described in Chapter 4, “Managing Files.” Specifically, you use the +xoption to add execute permissions, probably in conjunction with ato add these permissions for all users. For instance, to make a file called my-scriptexecutable, you should issue the following command:$ chmod a+x my-scriptYou’ll then be able to execute the script by typing its name, possibly preceded by ./to tell Linux to run the script in the current directory rather than searching the current path. If you fail to make the script executable, you can still run the script by running the shell program followed by the script name (as in bash my-script), but it’s generally better to make the script executable. If the script is one you run regularly, you may want to move it to a location on your path, such as /usr/local/bin. When you do that, you won’t have to type the complete path or move to the script’s directory to execute it; you can just type my-script.It’s possible to set a script’s SUID or SGID bits. (See Chapter 4 for information about the SUID and SGID bits.) Doing so is potentially dangerous, particularly if the script is owned by root, for reasons described in Chapter 4. You should therefore be very cautious about applying the SUID bit to scripts.Another way to run a script requires mention: sourcingit. You can source a script by using the sourcekeyword or a dot (.), as follows:$ source my-script$ . my-scriptSourcing a script causes it to run in the current shell, as opposed to launching a new instance of the shell, as occurs when you run a script by typing its name alone or using the execcommand, as described in Chapter 1. This has some important implications:Writing Scripts 435■ When you source a script, it will have access to environment variables set in the calling shell, even if you haven’t exported them. Ordinarily, only environment variables that you explicitly export become available to scripts you run.■ If you source a script and if that script sets an environment variable, that variable will become available (or will be changed) in the calling shell. If you run the script normally, any environment variables it sets will remain local to it and to the programs that it calls, even if the script exports the variables.■ Running a script in the normal ways imposes overhead costs associated with launching the new shell. These costs are normally negligible, but if a script calls itself recursively or calls many other scripts, sourcing those scripts within the first script may improve performance.■ Sourcing a script causes it to execute in the calling shell’s language, whereas running a script normally causes it to use the shell language specified on the hashbang line.Using CommandsOne of the most basic features of shell scripts is the ability to run commands. You can use both shell internal commands and external commands. Most of the commands you type in a shell prompt are external commands—they’re programs located in /bin, /usr/bin, and other directories on your path. You can run such programs, as well as internal commands, by including their names in the script. You can also specify parameters to such programs in a script. For instance, suppose you want a script that launches two xtermwindows and the KMail mail reader program. Listing 9.1 presents a shell script that accomplishes this goal.Listing 9.1: A simple script that launches three programs#!/bin/bash/usr/bin/xterm &/usr/bin/xterm &/usr/bin/kmail &Aside from the first line that identifies it as a script, the script looks just like the commands you might type to accomplish the task manually, except for one fact: The script lists the complete paths to each program. This is usually not strictly necessary, but listing the complete path ensures that the script will find the programs even if the PATHenvironment variable changes. On the other hand, if the program files move (say, because you upgrade the package from which they’re installed and the packager decides to move them), scripts that use complete paths will break.Each program-launch line in Listing 9.1 ends in an ampersand (&). This character tells the shell to go on to the next line without waiting for the first to finish. If you omit the ampersands in Listing 9.1, the effect will be that the first xtermwill open but the second won’t open until the first is closed. Likewise, KMail won’t start until the second xtermterminates.
đang được dịch, vui lòng đợi..
Kết quả (Việt) 2:[Sao chép]
Sao chép!
Nhiều kịch bản khởi động Linux, bao gồm kịch bản khởi động SysV, là trong thực tế shell
script. Do đó, sự hiểu biết kịch bản shell là cần thiết nếu bạn muốn
thay đổi một kịch bản khởi động Linux.
Giống như bất kỳ công việc lập trình, kịch bản shell có thể khá phức tạp. Do đó, chương này chỉ gãi bề mặt của những gì có thể được thực hiện thông qua kịch bản shell. Tham khảo một số cuốn sách về chủ đề, chẳng hạn như
Cameron Newham của Học Bash Shell, 3rd Edition (O'Reilly, 2005)
hoặc Richard Blum và Linux Command Line Christine Bresnahan và Shell
Scripting Kinh Thánh, 2nd Edition (Wiley, 2011), để biết thêm thông tin .
Để tạo ra một kịch bản, trước tiên bạn phải biết làm thế nào để bắt đầu chỉnh sửa một. Một khi bạn làm như vậy,
bạn sẽ thấy rằng một trong những nhiệm vụ đơn giản nhất để làm là để gọi lệnh bên ngoài. Nâng cao hơn
nhiệm vụ bao gồm sử dụng các biến và sử dụng các biểu thức điều kiện.
434 Chương 9 ■ Scripts Viết, Cấu hình Email, và sử dụng cơ sở dữ liệu
Bắt đầu một Shell Script
kịch bản Shell được các tập tin văn bản đơn giản, vì vậy bạn tạo chúng trong soạn thảo văn bản. Một kịch bản bắt đầu với
một dòng nhận dạng vỏ mà được sử dụng để chạy nó, chẳng hạn như sau:
#! / Bin / sh
Hai ký tự đầu tiên là một mã số đặc biệt mà nói với các hạt nhân Linux rằng đây là một kịch bản
và sử dụng phần còn lại của các dòng như là một tên đường dẫn đến chương trình đó là để giải thích kịch bản.
(dòng này đôi khi được gọi là xe củ, hashbang, hashpling, hoặc bảng bangline.) Shell
ngôn ngữ kịch bản sử dụng một dấu thăng (#) là một nhân vật bình luận, vì vậy các tiện ích kịch bản bỏ qua
dòng này, mặc dù hạt nhân thì không. Trên hầu hết các hệ thống, / bin / shis một liên kết tượng trưng mà
chỉ vào / bin / bash, nhưng nó có thể chỉ ra một số vỏ khác. Xác định các kịch bản như sử dụng
/ bin / shguarantees rằng bất kỳ hệ thống Linux sẽ có một chương trình shell để chạy các kịch bản; nhưng
nếu các kịch bản sử dụng bất kỳ tính năng specifi c để một lớp vỏ đặc biệt, bạn nên xác định vỏ mà
thay vào đó, ví dụ, sử dụng / bin / bashor / bin / tcshinstead của / bin / sh.
Khi bạn đã hoàn tất việc viết kịch bản shell, bạn nên sửa đổi nó để nó thực thi.
Bạn làm điều này với các chmodcommand, như được mô tả trong Chương 4, "Quản lý tập tin." Cụ thể, bạn sử dụng + xoption thêm quyền thực thi, có thể kết hợp với một
bổ sung thêm các điều khoản cho tất cả người dùng . Ví dụ, để tạo một tập tin gọi là my-scriptexecutable, bạn nên dùng lệnh sau:
$ chmod a + x my-script
Sau đó bạn sẽ có thể thực thi kịch bản bằng cách gõ tên của nó, có thể bắt đầu bằng ./to cho
Linux để chạy các script trong thư mục hiện tại thay vì tìm kiếm những con đường hiện tại. Nếu bạn
không thực hiện các thực thi kịch bản, bạn vẫn có thể chạy các script bằng cách chạy chương trình shell
theo sau là tên script (như trong bash my-script), nhưng nói chung tốt hơn để làm cho
thực thi kịch bản. Nếu kịch bản là một trong những bạn chạy thường xuyên, bạn có thể muốn di chuyển nó đến một vị trí trên con đường của bạn, chẳng hạn như / usr / local / bin. Khi bạn làm điều đó, bạn sẽ không phải gõ
đường dẫn đầy đủ hoặc di chuyển đến thư mục của kịch bản để thực hiện nó; bạn có thể chỉ cần gõ my-script.
Nó có thể thiết lập SUID hoặc SGID bit của một kịch bản. (Xem chương 4 để biết thông tin về SUID và SGID bit.) Làm như vậy là nguy hiểm, đặc biệt là nếu kịch bản được sở hữu bởi root, vì những lý do được mô tả trong
Chương 4. Bạn do đó cần phải rất thận trọng về việc áp dụng các
bit SUID để kịch bản.
Một cách khác để chạy một kịch bản yêu cầu đề cập đến: sourcingit. Bạn có thể nguồn một kịch bản bằng
cách sử dụng các sourcekeyword hoặc một dấu chấm, như sau (.):
$ Nguồn của tôi-script
$. my-script
Sourcing một kịch bản làm cho nó chạy ở shell hiện hành, như trái ngược với phát động mới
dụ của lớp vỏ, như xảy ra khi bạn chạy một kịch bản bằng cách gõ tên của nó một mình hoặc sử dụng các
execcommand, như được mô tả trong Chương 1. Điều này có một số ý nghĩa quan trọng:
Scripts Viết 435
■ Khi bạn nguồn một kịch bản, nó sẽ có quyền truy cập đến các biến môi trường thiết lập trong gọi
vỏ, thậm chí nếu bạn đã không xuất khẩu chúng. Thông thường, chỉ có các biến môi trường mà
bạn một cách rõ ràng xuất khẩu có trở thành có sẵn cho kịch bản mà bạn chạy.
■ Nếu bạn nguồn một kịch bản và nếu kịch bản mà bộ biến môi trường, biến đó sẽ
trở nên có sẵn (hoặc sẽ được thay đổi) trong vỏ gọi. Nếu bạn chạy các kịch bản bình thường, bất kỳ biến môi trường nó đặt sẽ vẫn địa phương để nó và các chương trình mà
nó gọi, thậm chí nếu kịch bản xuất khẩu các biến.
■ Chạy một kịch bản trong những cách bình thường vào các chi phí trên không liên quan với tung ra
chiếc vỏ mới . Các chi phí này thường không đáng kể, nhưng nếu một kịch bản tự gọi đệ quy
hoặc các cuộc gọi nhiều kịch bản khác, tìm nguồn cung ứng các kịch bản này trong kịch bản đầu tiên có thể cải thiện
hiệu suất.
■ Tìm nguồn cung ứng một kịch bản gây ra nó để thực hiện trong ngôn ngữ shell gọi của, trong khi chạy một
kịch bản thường gây ra nó sử dụng ngôn ngữ shell định trên dòng hashbang.
Sử dụng lệnh
Một trong những tính năng cơ bản nhất của kịch bản shell là khả năng chạy các lệnh. Bạn có thể sử dụng
cả vỏ nội lệnh và lệnh bên ngoài. Hầu hết các lệnh bạn gõ vào
dấu nhắc shell là chương trình bên ngoài lệnh-chúng nằm trong / bin, / usr / bin, và
các thư mục khác trên con đường của bạn. Bạn có thể chạy các chương trình như vậy, cũng như các lệnh nội bộ,
do đó có tên của họ trong kịch bản. Bạn cũng có thể chỉ định các tham số cho chương trình như vậy trong
một kịch bản. Ví dụ, giả sử bạn muốn có một kịch bản mà ra mắt hai xtermwindows và các
chương trình đọc email KMail. Bảng liệt kê 9.1 trình bày một kịch bản hoàn thành mục tiêu này.
Listing 9.1: Một kịch bản đơn giản mà ra mắt ba chương trình
# / bin / bash!
/ Usr / bin / xterm &
/ usr / bin / xterm &
/ usr / bin / KMail &
Ngoài dòng đầu tiên mà xác định nó như một kịch bản, kịch bản trông giống như các lệnh bạn có thể gõ vào để thực hiện các công việc bằng tay, ngoại trừ một thực tế: Các kịch bản liệt kê
các đường dẫn đầy đủ cho mỗi chương trình. Điều này thường là không thực sự cần thiết, nhưng danh sách các
đường dẫn đầy đủ đảm bảo rằng kịch bản sẽ tìm các chương trình ngay cả khi PATHenvironment
biến thay đổi. Mặt khác, nếu các tập tin chương trình di chuyển (nói, bởi vì bạn nâng cấp
các gói từ mà họ đang cài đặt và đóng gói quyết định di chuyển chúng), các kịch bản
mà sử dụng đường dẫn đầy đủ sẽ phá vỡ.
Mỗi dòng chương trình khởi động trong Ví dụ 9.1 đầu trong một dấu và (&). Nhân vật này nói với
vỏ để đi vào các dòng tiếp theo mà không cần chờ đợi cho người đầu tiên để kết thúc. Nếu bạn bỏ qua các
ký hiệu trong Liệt kê 9.1, hiệu quả sẽ được rằng xtermwill đầu tiên mở nhưng thứ hai
sẽ không mở cửa cho đến khi người đầu tiên được đóng lại. Tương tự như vậy, KMail sẽ không bắt đầu cho đến khi xterm thứ hai
chấm dứt.
đang được dịch, vui lòng đợi..
 
Các ngôn ngữ khác
Hỗ trợ công cụ dịch thuật: Albania, Amharic, Anh, Armenia, Azerbaijan, Ba Lan, Ba Tư, Bantu, Basque, Belarus, Bengal, Bosnia, Bulgaria, Bồ Đào Nha, Catalan, Cebuano, Chichewa, Corsi, Creole (Haiti), Croatia, Do Thái, Estonia, Filipino, Frisia, Gael Scotland, Galicia, George, Gujarat, Hausa, Hawaii, Hindi, Hmong, Hungary, Hy Lạp, Hà Lan, Hà Lan (Nam Phi), Hàn, Iceland, Igbo, Ireland, Java, Kannada, Kazakh, Khmer, Kinyarwanda, Klingon, Kurd, Kyrgyz, Latinh, Latvia, Litva, Luxembourg, Lào, Macedonia, Malagasy, Malayalam, Malta, Maori, Marathi, Myanmar, Mã Lai, Mông Cổ, Na Uy, Nepal, Nga, Nhật, Odia (Oriya), Pashto, Pháp, Phát hiện ngôn ngữ, Phần Lan, Punjab, Quốc tế ngữ, Rumani, Samoa, Serbia, Sesotho, Shona, Sindhi, Sinhala, Slovak, Slovenia, Somali, Sunda, Swahili, Séc, Tajik, Tamil, Tatar, Telugu, Thái, Thổ Nhĩ Kỳ, Thụy Điển, Tiếng Indonesia, Tiếng Ý, Trung, Trung (Phồn thể), Turkmen, Tây Ban Nha, Ukraina, Urdu, Uyghur, Uzbek, Việt, Xứ Wales, Yiddish, Yoruba, Zulu, Đan Mạch, Đức, Ả Rập, dịch ngôn ngữ.

Copyright ©2025 I Love Translation. All reserved.

E-mail: