$ vi hw [Enter]
echo "Hello World!" |
$ hw [Enter] ksh: hw: cannot execute - Permission deniedWhat is the problem here, and how do we fix it?
$ hw [Enter] Hello World!
1 In most cases, there is no significant difference between a program, a script, an application, or an executable. These are merely differing names for the same thing. Source languages may differ, program size and complexity may vary, translation methods may be different (e.g. compilation vs. interpretation), but all of these are simply instructions to be executed by a computer.
2 While this is feasible, if a program is going to have this size and complexity, typically a language other than the shell is used; for example Perl, C, or C++.
3 Depending upon your implementation and PATH variable values, you may see an error like hw: not found. If this happens, you may need to execute this as follows: ./hw (that is a dot-slash preceeding the file name). See scenario 1 below.
Thus given the following (hypothetical) example, try to identify the meaning of each dot:
From left to right, the first (leftmost) dot has whitespace on its right, and precedes a command, so this is context # 4, specifying that the program .setup.sh be run without spawning a child process. Note the name of this file is .setup.sh, that is dot-setup-dot-sh. The next dot precedes a / indicating this is a relative file specification referencing our current position in the file system, thus context # 1. The third dot has no whitespace following it, thus context # 2, i.e. specifying a hidden file. And the rightmost dot is in the middle of the filename, so this describes a file extension, context # 3.
It is noteworthy here to mention that identical characters, operators, or commands mean different things to different applications. For example, the dot character has a different meaning within the shell than it does to the vi editor application, or than it does to other applications. A user must always remember what application they are interacting with, and to use the language that application understands.
cd /usr/binThe functionality of each command should be apparent, the first changes the working directory to the /usr/bin directory, and the second prints the location of the working directory. As before, to run this program we simply type the name of the program (ch_dir) followed by [Enter]. As we have seen, the shell clones a copy of itself with the fork command, overwrites the clone with the ch_dir process using exec, and begins execution. A diagram of this is below:
pwd
As we can see, the child program displays the working directory of /usr/bin when the pwd command is executed, which is what we would expect. The question we must address now is where are we? One might guess that since the pwd command displayed that we were in the /usr/bin directory, that's where we are. But you must keep in mind that the working directory of /usr/bin was set in the child process. When the child process completes, it goes away (as does all of its process space) and control returns to the parent. Therefore, in the parent process, our working directory is the original directory where we executed the ch_dir process.
If we wish to make such changes permanent in our parent process, we must run this shell program without creating a child process. How do we do this you ask? See dot context # 4 above. When using the dot operator in this context, no child process is created and the commands are interpreted by the current shell process. Thus any environmental changes that take place change the current shell environment. Refer to the diagram below.
# Short-Description: Bring up/down networking
One slight variation on the use of the # character is when used in combination with the exclamation character, as #!. This is commonly referred to as the she-bang (derived from sharp-bang). Since not all script commands are portable between all shells, the she-bang gives the programmer a mechanism to specify which interpreter will execute a particular program. To accomplish this, the she-bang must appear as the first two characters (2 bytes/16 bits) on the first line in a shell program. Following the she-bang must be the file specification of the intepreter program which will interpret the script. Thus if the script is a korn shell script, the first line should look like
#!/bin/kshand similarly a bash shell program
#!/bin/bashSome modern shells may allow whitespace between the she-bang characters and the interpreter specification, however this is not a standard convention between all shells. Note that the she-bang must be the first two characters in the file and the interpreter specification must be valid for this to work.
Another bit of useful miscellany is the type command. The type command can be used to understand where a command is invoked from, or what type of program it actually is. Is a program a simple shell program, a shell built-in program, a user defined shell program, or a function? Examine the following:
$ type cd [Enter] cd is a shell builtin $ type cp [Enter] cp is /bin/cp $ type ddir [Enter] ddir is a functionIn the first example, type informs us that the cd command is a command built into the shell; the second example tells the user that cp is a utility that is loaded from the /bin directory; and the third example informs the user that the command ddir is a function, in this case, a user defined function. Some closely related commands are listed in the summary below.
©2019, Mark A. Thomas. All Rights Reserved.