NEXT UP previous
Next: Variables

Command Files

Any standard Linux command or pipeline, or any sequence of these things, can be entered into a text file with cat or with an editor like vi, made executable with climod and then executed just by entering its name, like any other system command. The following command sequence shows this idea in action:

	$ cat >dirsize
	ls /usr/bin | wc -w
	Ctrl-d
	$ chmod 700 dirsize
	$ ls -l dirsize
	-rwx------ 1 pc		book	20 Jun 10 22:04 dirsize
	$ dirsize
	   459

Here, the pipeline ls /usr/bin | wc -w is entered into the text file dirsize. This file is then flagged as executable, with the command chmod 700 dirsize. The following ls command now allows you to check that the file's permission flags have been set as required. Finally, using the filename (dirsize) as a command name causes the pipeline in the file to be executed by a shell, just as though it had been entered from the command line. The output (459) is the count of the number of files in the directory specified in the shell script.

When you write any shell scripts that you will use frequently, they should be placed in a personal directory, which you set aside for the purpose, and executed from there. The best plan is, probably, to create a bin directory under your home directory and place the files in there. The question then arises as to how you execute these commands without having to type a long pathname, specifying how the commands can be reached from your current working directory. This question will be answered in the next two sections.


NEXT UP previous
Next: Variables