5.22. Common Core Built-InsThere are a large number of built-in commands that are supported by command shells, of which only a few are common to all. This section describes the most useful common core built-in commands. 5.22.1. evalFigure 5-21 describes the eval shell command.
In the following example, I executed the result of the echo command, which caused the variable x to be set: $ echo x=5 ...first execute an echo directly. x=5 $ eval `echo x=5` ...execute the result of the echo. $ echo $x ...confirm that x was set to 5. 5 $ _ For a more complex example, see the description of tset in Chapter 3, "GNU Utilities for Nonprogrammers" 5.22.2. execFigure 5-22 describes the exec shell command.
In the following example, I exec'ed the date command from my login shell, which caused the date utility to run and then my login process to terminate: $ exec date ...replace shell process by date process. Tue Feb 1 18:55:01 CDT 2005 ...output from date. login: _ ...login shell is terminated. 5.22.3. shiftFigure 5-23 describes the shift shell command.
In the following example, I wrote a C shell script to display its arguments before and after a shift. $ cat shift.csh ...list the script. #!/bin/csh echo first argument is $1, all args are $* shift echo first argument is $1, all args are $* $ ./shift.csh a b c d ...run with four arguments. first argument is a, all args are a b c d first argument is b, all args are b c d $ ./shift.csh a ...run with one argument. 5.22.4. umaskWhen a C program creates a file, it supplies the file's original permission settings as an octal parameter to the system call open (). For example, to create a file with read and write permission for the owner, group, and others, it would execute a system call like this: fd = open ("myFile", O_CREAT | O_RDWR, 0666); For information on the encoding of permissions as octal numbers, see Chapter 3, "GNU Utilities for Nonprogrammers." For information on the open () system call, see Chapter 12, "Systems Programming." When the shell performs redirection using (using the ">" character), it uses a system call sequence similar to the one shown above to construct a file with octal permission 666. However, if you try creating a file using redirection with the ">" character, you'll probably end up with a file that has a permission setting of 644 octal: $ date > date.txt $ ls -lG date.txt -rw-r--r-- 1 glass 29 May 3 18:56 date.txt $ _ The reason for this is that every Linux process contains a special quantity called a umask value, which is used to restrict the permission settings that it requests when a file is created. The default umask value of a shell is 0022 octal. The set bits of a umask value mask out the set bits of a requested permission setting. In the example shown above, the requested permission 0666 was masked with 0022 to produce the final permission 0644, as shown in Figure 5-24.
If a file already exists before it is redirected to, the original file's permission values are retained and the umask value is ignored. Figure 5-25 describes how the umask command may be used to manipulate the umask value.
In the following example, I changed the umask value to 0 and then created a new file to illustrate its effect: $ umask ...display current umask value. 0022 ...mask write permission of group/others. $ umask 0 ...set umask value to 0. $ date > date2.txt ...create a new file. $ ls -lG date2.txt -rw-rw-rw- 1 glass 29 May 3 18:56 date2.txt $ _ |