You can write shell scripts with the Cshell
Pełen tekst
(2)
(3)
(4)
(5) . . 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 Cshell; however, this is not covered here..
(6) . .
(7) . . Suppose you often type the command
(8)
(9) . and you'd rather type a simple command, say . . Create a shell script
(10)
(11)
(12)
(13) .
(14)
(15) 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.
(16) . From the man page for exec(2):
(17)
(18)
(19)
(20)
(21)
(22)
(23)
(24)
(25)
(26) !"
(27)
(28)
(29)
(30) # $ %& '
(31)
(32) ( ) * & .
(33) .
(34) . 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..
(35) .
(36) . All shell scripts should include a search path specifica- tion: !"#$ % %&
(37) !"#. A PATH specification is recommended -- often times a script will fail for some people because they have a different or incomplete search path. +& '
(38)
(39) , ,
(40)
(41)
(42)
(43)
(44)
(45) .
(46)
(47) .
(48)
(49) . 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. 1 2 '1 .-1 +-..(& .
(50) )*+3 451 45 +0 ! 6 7 -. A non-zero exit status indicates an error condition of some sort while a zero exit status indicates things worked as expected. &'- ! . '
(51) .
(52) .
(53) . Exit codes are important for those who use your code. Many constructs test on the exit status of a command. +
(54) / & . For example, 1& 8 94:; . Your code should be written with the expectation that others will use it. Making sure you return a meaningful exit status will help..
(55) .
(56) . Standard input, output, and error are file descriptors 0, 1, and 2. Each has a particular role and should be used accordingly: 1 2 '1 .-1 +-..(& )*+3 451 45 1 +0 .
(57) 761< 1 =6 >
(58) 10> -? @&& ? " && . Error messages should appear on stderr not on stdout! Output should appear on stdout. As for input/output dialogue: < 9 A 1& "9 < B /7
(59) 2& 55 BCDB? E
(60) & -&& F@$5 5 F@$5 5 . 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))..
(61) .
(62) . For loop iteration ' ,
(63) ,
(64) 0/ < 9 . For example: >G/HI>.
(65) <"/;!3
(66) < JJK . Alternatively you may see: < 9 & & •. Case ' 9 '
(67) 'C
(68) (? &&( . For example: 51 5 '-.('-.(? 1 $.L1 M 1 $>
(69) 1 .-> && '-.('-.('-.('-.(? 1 $>
(70) 1 .-> && B? )*+3 451 45 +0 && •. Conditional Execution + ' .
(71) ( . For example: ' (& )*+, %-./. +0 . Alternatively you may see: & &' &( •. While/Until Iteration 1 0
(72) L9 C M . For example: 6
(73) 1 9 ' (& N
(74) . Alternatively you may see: 9 & & •. Variables 2
(75) 3
(76)
(77) + ,
(78) 4 4.
(79) 5 ,
(80) %
(81) 046(
(82) ,
(83) o. Variable Assignment " ,
(84) ,
(85) ,
(86) 7,
(87)
(88) / !"#$ % %&
(89) !"#. or "/;!3$>O 4> 4>& ?> o. Exporting Variables 2
(90)
(91)
(92)
(93)
(94) 0 P @,I"< ;QI G!3 < < '5;QI G!35$55(& 1& 5;QI G!3O> >%--?245& ;QI G!3 '5;QI G!35$55(& ;QI G!3$> >%--
(95) ;QI G!3 . Likewise, for variables like the PRINTER which you want hon- ored by lpr(1). From a user's .profile:. FQD"8F$ I
(96) &
(97) FQD"8F. Note: that the Cshell exports all environment variables. o. Referencing Variables 8 4,
(98) % 49,
(99) :( ,
(100) @ R< 9.
(101) '5,I8F5$5 5(& !"#$#/@8% !"# !"#$ % % !"# . The braces are required for concatenation constructs.
(102) N-. The value of the variable "p_01". L
(103) MN-. The value of the variable "p" with "_01" pasted onto the end. o. Conditional Reference. o. L< 9 M. If the variable has been set, use it's value, else use word. /I"ISFQ "$L /I"ISFQ " I
(104) M&.
(105) /I"ISFQ " L< %9 M. If the variable has been set and is not null, use it's value, else use word. +
(106) , ; ,. ,
(107) )
(108) 0
(109) %6( <1;5+=1, ,
(110) 0
(111)
(112) L< %29 M. If variable is set use it's value, else print out word and exit. Useful for bailing out.. o. Arguments )
(113)
(114)
(115)
(116) ,
(117) /.
(118) -66. The command and arguments. With $0 the command and the rest the arguments.. . The number of arguments. B6T. All the arguments as a blank separated string. Watch out for "$*" vs. "$@". And, some commands:. . Shift the postional variables down one and decrement number of arguments. . Set the positional variables to the argument list. )
(119) /
(120) 9 ' (&
(121) . A use of the set command: 91 "/;!3$>O 4> 4>& ?> I //G >G/HI>.
(122) <"/;!3
(123) < JJK o. Special Variables. o. . Current process id. This is very useful for constructing temporary files.
(124) $
(125) -
(126) 5
(127)
(128)
(129) +5
(130) +U
(131) )
(132) 2. The exit status of the last command. F '2 A-( •. Quotes/Special Characters '
(133) / &*O?C:V) 9
(134) . These are for command sequences, background jobs, etc. To quote any of these use a backslash (\) or bracket with quote marks ("" or '')..
(135) .
(136) >
(137) 3
(138)
(139) 3
(140) 0
(141) +.
(142)
(143) %LM%
(144) C97E%RL
(145) MR. Double Quotes >
(146) 3 ,,
(147) %
(148)
(149) (
(150) % ?@ 3 (+
(151) '5L
(152) M5(&
(153) $L
(154)
(155) ML
(156) ML M . Back Quotes &03 '5> >5$55(& $55 $545 $55 $55 . and "/;!3$>O 4> 4>& ?> •. Functions
(157) ! ' O? L M. For example:.
(158) 1 N
(159) O? L . 1 '(& %D 1)*+ M. Within a function the positional parmeters $0, $1, etc. are the arguments to the function (not the arguments to the script).. >
(160)
(161) / 9 6
(162)
(163) N O? L 9
(164) N M 7
(165) O
(166) ? '55$55(& CN N .
(167) •. Sourcing commands
(168)
(169)
(170)
(171) "
(172) / . +
(173)
(174)
(175)
(176)
(177) ' / . . command +
(178)
(179) .
(180)
(181)
(182) / F . What are the virtues of each? What's the difference? The second form is useful for configuration files where environment variable are set for the script. For example:. #/I"#/I"I& . 2 ' LW!SX, #/@8ML#/I"M(& LW!SX, #/@8ML#/I"M . Using configuration files in this manner makes it possible to write scripts that are automatically tailored for differ- ent situations..
(183) . . •. Test +
(184) %6(.
(185)
(186) & . and (note the matching bracket argument) '
(187) (& . On System V machines this is a builtin (check out the com- mand /bin/test). &'- %
(188) 0' ( A 8
(189) / L96 666M . is file writeable, readable, executeable, empty, etc? L A6 66M+. are numbers equal, not equal, greater than, etc.? L$6$M+. Are strings the same or different? L6M+. Binary or; binary and; use ! for unary negation.
(190) '1 .-1 +-..(& )*+3 451 45 +0 . Learn this command inside out! It does a lot for you. •. String matching.
(191) + ,
(192) "
(193) 0
(194) 9 ' (& B? $> C>&& ?& $&&
(195) B?
(196) $> C>&&
(197) ?&
(198) $&& B? , & && B?7$B& 7&& . Of course getopt would work much better. •. SysV vs BSD echo &'- ! / /7
(199) 2& . On SysV systems you'd say: /7
(200) 24& . In an effort to produce portable code we've been using: 97 '5> >5$55(& $55&$545 $55&$55 .
(201) /7
(202) 2& •. Is there a person? +8
(203) 3
(204)
(205) =
(206)
(207)
(208) $ 8 ! 3 ! Q. R
(209) . 6
(210) 1& 8 94:; . The tradition also extends to output. Q
(211) 6 < 1V*& < $ < $ . Beware: just because stdin is a tty that doesn't mean that stdout is too. User prompts should be directed to the user terminal.. Q. R
(212) . 6
(213) 1& 8 94:;)*- . Have you ever had a program stop waiting for keyboard input when the output is directed elsewhere? •. Creating Input >!
(214)
(215) /.
(216) 7
(217) O
(218) ? '55$55(& CN N . alternatively, redirection from a file: 7
(219) O
(220) ? '55$55(& N V N . You can also construct files on the fly.
(221) V
(222) % %VT 99) % IY %I+ +, E. ,P/ A 8/E. Note: that variables are expanded in the input. •. String Manipulations !
(223)
(224) ' 0 "Q@8$> C+.> "Q@8$> C RBBB4OB4?BB4R> .
(225) "Q@8$> C97RL
(226) KMR> "Q@8$> 4> 4>& K> "Q@8$> CO <91Z& ?>. With some care, redefining the input field separators can help. < Q
(227) O? L >QEI$55& > K+
(228) M ' (& )*+, %1 Q +0 $> > 7
(229) VV8/EC
(230) 55C RB$R 1
(231) $1 8/E •. Debugging +
(232)
(233)
(234) 0 / . 1
(235)
(236) ! ;=0 . -
(237) ! ;
(238)
(239)
(240) !
(241)
(242) .
(243) , .
(244)
Powiązane dokumenty
• To configure methods as a REST style services one should configure them by specifying RequestFormat,. ResponseFormat in
Przy charakterystyce czeladzi pod względem wieku i płci zabiegiem koniecz nym jest oddzielenie czeladzi w gospodarstwach chłopskich od czeladzi i służby
Zauważmy bowiem, że stary mistrz oprócz tego, że jest stary (w znaczeniu – tradycyjny, klasyczny), jest także zawsze mężczyz ną; skoro więc upersonifikowany kanon to
The above remarks o f two renowned theologians yield an explanation not only to the specificity of biological and theological concepts of World, but also to the origin of their
Tekst przestaje być przez to postrze- gany jako wynik działania wszystkich sił wyodrębnianych w układzie komuni- kacyjnym, nie jest już narzędziem porozumiewania się, nie
Przeszukiwanie adaptacyjnej książki kodowej odbywa się na podstawie trzech sygnałów: sekwencji pobudzającej (wyznaczonej na podstawie sygnałów pobudzających z
Biblioteki nabyły prawo do: udostępniania nieodpłatnego, w zakresie swoich zadań statutowych, egzemplarzy utworów rozpowszechnionych (a więc nie tylko
The results of our analysis show that Scratch programmers often prefer longer identifier names than developers in other languages, while Scratch procedure names have even longer