How to write a shell script.pdf
(
65 KB
)
Pobierz
A shell is a command line interpretor. It takes commands and executes them. As such, it
implements a programming language. The Bourne shell is used to create shell scripts -- ie.
programs that are interpreted/executed by the shell. You can write shell scripts with the C-
shell; however, this is not covered here.
Suppose you often type the command
and you'd rather type a simple command, say
Create a shell script
This quick example is far from adequate but some observations:
1. Shell scripts are simple text files created with an editor.
2. Shell scripts are marked as executeable
4. Should be located in your search path and ~/bin should be in your search path.
5. You likely need to rehash if you're a Csh (tcsh) user (but not again when you login).
6. Arguments are passed from the command line and referenced. For example, as $1.
All Bourne Shell scripts should begin with the sequence
From the man page for exec(2):
!"
#$%&'(
)*&
Comments are any text beginning with the pound (#) sign. A comment can start anywhere on
a line and continue until the end of the line.
All shell scripts should include a search path specifica- tion:
!"#$%%& !"#
A PATH specification is recommended -- often times a script will fail for some people
because they have a different or incomplete search path.
+&',,
A good shell script should verify that the arguments sup- plied (if any) are correct.
'(&
)*+,%-./.
+0
This script requires three arguments and gripes accordingly.
All Unix utilities should return an exit status.
12
'1.-1+-..(&
)*+345145
+0
!67
-
A non-zero exit status indicates an error condition of some sort while a zero exit status
indicates things worked as expected.
&'- !.
'
Exit codes are important for those who use your code. Many constructs test on the exit status
of a command.
+/
&
For example,
1&
894:;
Your code should be written with the expectation that others will use it. Making sure you
return a meaningful exit status will help.
Standard input, output, and error are file descriptors 0, 1, and 2. Each has a particular role and
should be used accordingly:
12
'1.-1+-..(&
)*+3451451
+0
761<1=6
>10>
-?
@&&
?
"&&
Error messages should appear on stderr not on stdout! Output should appear on stdout. As for
input/output dialogue:
<9A
1&
"9<B
/72&
55
BCDB?
E&
-&&
F@$55
F@$55
Note: this code behaves differently if there's a user to communicate with (ie. if the standard
input is a tty rather than a pipe, or file, or etc. See tty(1)).
For loop iteration
',,0/
<9
For example:
>G/HI>
<"/;!3
<
JJK
Alternatively you may see:
<9&&
Case
'
9
''C(?
&&(
For example:
515
'-.('-.(?
1$.L1M
1$>1.->
&&
'-.('-.('-.('-.(?
1$>1.->
&&
B?
)*+345145
+0
&&
Conditional Execution
+
'
Plik z chomika:
Phoob
Inne pliki z tego folderu:
So15 - Programowanie wspolbiezne.pdf
(52 KB)
So10 - Rozproszona pamiec dzielona 1.pdf
(111 KB)
abs-guide.pdf
(895 KB)
BASH - Programowanie w powloce.pdf
(343 KB)
LinuxPG.pdf
(481 KB)
Inne foldery tego chomika:
ALG
AM2
ANG1
ANG2
ASD
Zgłoś jeśli
naruszono regulamin