1 / 15

Command Files

Command Files. A shell command can be typed directly at the terminal $ who | wc –l

nyla
Télécharger la présentation

Command Files

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Command Files A shell command can be typed directly at the terminal $ who | wc–l or it can be first typed into a file and then the file can be executed by the shell. For example, suppose that you need to find out the number of logged-in users several times throughout the day. It's not unreasonable to type in the preceding pipeline each time you want the information, but for the sake of example, let's type this pipeline into a file. $ cat nu who | wc –l To execute the commands contained inside the file nu, all you now have to do is type nu as the command name to the shell:

  2. $ nu sh: nu: cannot execute Oops! We forgot to mention one thing. Before you can execute a program this way, you must change the file's permissions and make it executable. This is done with the change mode command chmod. To add execute permission to the file nu , you simply type chmod +x file(s) The +x says make the file(s) that follow executable. The shell requires that a file be both readable and executable by you before you can execute it.

  3. $ ls -l nu -rw-rw-r-- 1 stevesteve 12 Jul 10 11:42 nu $ chmod +x nu Make it executable $ ls -l nu -rwxrwxr-x 1 stevesteve 12 Jul 10 11:42 nu Now that you've made it executable, try it again $ nu 8 This time it worked. You can put any commands at all inside a file, make the file executable, and then execute its contents simply by typing its name to the shell.

  4. Suppose that you're working on a proposal called sys.caps and that the following command sequence is needed every time you want to print a new copy of the proposal: tblsys.caps | nroff -mm –Tlp | lp Once again, you can save yourself some typing by simply placing this command sequence into a file—let's call it run—making it executable, and then just typing the name run whenever you want to get a new copy of the proposal: $ cat run tblsys.caps | nroff -mm -Tlp | lp $ chmod +x run $ run request id is laser1-15 (standard input)

  5. Suppose that you need to run other files besides sys.caps through this same command sequence. You could make a separate version of run for each such file; or, you could modify the run program so that you could specify the name of the file to be run on the command line. That is, you could change run so that you could type run new.hire for example, to specify that the file new.hire is to be run through this command sequence, or run sys.caps to specify the file sys.caps.

  6. Whenever you execute a shell program, the shell automatically stores the first argument in the special shell variable 1, the second argument in the variable 2, and so on. These special variables—more formally known as positional parameters—are assigned after the shell has done its normal command-line processing (that is, I/O redirection, variable substitution, filename substitution, and so on). To modify the run program to accept the name of the file as an argument, all you do to the program is change the reference to the file sys.caps so that it instead references the first argument typed on the command line

  7. $ cat run tbl$1 | nroff -mm -Tlp | lp $ run new.hireExecute it with new.hire as the argument request id is laser1-24 (standard input)

  8. The $# Variable Whenever you execute a shell program, the special shell variable $# gets set to the number of arguments that were typed on the command line. As you'll see in the next chapter, this variable can be tested by the program to determine whether the correct number of arguments was typed by the user. The next program called argswas written just to get you more familiar with the way arguments are passed to shell programs. Study the output from each example and make sure that you understand it:

  9. $ cat argsLook at the program echo $# arguments passed echo arg 1 = :$1: arg 2 = :$2: arg 3 = :$3: $ args a b c Execute it 3 arguments passed arg1 = :a: arg 2 = :b: arg 3 = :c: $ args a b Try it with two arguments 2 arguments passed arg1 = :a: arg 2 = :b: arg 3 = :: Unassigned args are null $ argsTry it with no arguments 0 arguments passed arg1 =:: arg 2 =:: arg 3 = ::

  10. $ args "a b c" Try quotes 1 arguments passed arg1 = :a b c: arg 2 = :: arg 3 = :: $ ls x* See what files start with x xact xtra As you can see, the shell does its normal command-line processing even when it's executing your shell programs. This means that you can take advantage of the normal niceties such as filename substitution and variable substitution when specifying arguments to your programs.

  11. The $* Variable The special variable $* references all the arguments passed to the program. This is often useful in programs that take an indeterminate or variable number of arguments. You'll see some more practical examples later. Here's a program that illustrates its use: $ cat args2 echo $# arguments passed echo they are :$*: $ args2 a b c 3 arguments passed they are :a b c: $ args2 one two 2 arguments passed they are :one two:

  12. $ args2 0 arguments passed they are :: $ args2 * 8 arguments passed they are :args args2 names nu phonebook stat xactxtra:

  13. The shift Command The shift command allows you to effectively left shift your positional parameters. If you execute the command shift whatever was previously stored inside $2 will be assigned to $1, whatever was previously stored in $3 will be assigned to $2, and so on. The old value of $1 will be irretrievably lost. When this command is executed, $# (the number of arguments variable) is also automatically decremented by one:

  14. $ cat tshift Program to test the shift echo $# $* shift echo $# $* shift echo $# $* shift echo $# $* shift echo $# $* shift echo $# $*

  15. $ tshift a b c d e 5 a b c d e 4 b c d e 3 c d e 2 d e 1 e 0

More Related