Section 5.22. Common Core Built-Ins


[Page 197 (continued)]

5.22. Common Core Built-Ins

There 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. eval

Figure 5-21 describes the eval shell command.

Figure 5-21. Description of the eval shell command.

shell command: eval command

The eval shell command executes the output of command as a regular shell command. It is useful when processing the output of utilities that generate shell commands (e.g., tset).


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. exec

Figure 5-22 describes the exec shell command.


[Page 198]

Figure 5-22. Description of the exec shell command.

Shell Command: exec command

The exec shell command causes the shell's image to be replaced with command in the process' memory space. If command is successfully executed, the shell that performed the exec ceases to exist. If this shell was a login shell, then the login session is terminated when command terminates.


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. shift

Figure 5-23 describes the shift shell command.

Figure 5-23. Description of the shift shell command.

Shell Command: shift

The shift shell command causes all of the positional parameters $2..$n to be renamed $1..$(n-1), and $1 to be lost. It's particularly handy in shell scripts when cycling through a series of command-line parameters. If there are no positional arguments left to shift, an error message is displayed.


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. 
[Page 199]
first argument is a, all args are a first argument is , all args ar $ ./shift.csh ...run with no arguments. first argument is , all args are shift: No more words ...error message. $ _


5.22.4. umask

When 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.

Figure 5-24. Bit-by-bit example of the effect of the umask setting.
 

r

w

x

r

w

x

r

w

x

original

1

1

0

1

1

0

1

1

0

mask

0

0

0

0

1

0

0

1

0

final

1

1

0

1

0

0

1

0

0


If a file already exists before it is redirected to, the original file's permission values are retained and the umask value is ignored.


[Page 200]

Figure 5-25 describes how the umask command may be used to manipulate the umask value.

Figure 5-25. Description of the umask shell command.

Shell Command: umask [octalValue]

The umask shell command sets the shell's umask value to the specified octal number, or displays the current umask value if the argument is omitted. A shell's umask value is retained until changed. Child processes inherit their umask value from their parent.


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 $ _ 





Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net