Total Pageviews

Wednesday 31 October 2012

SHELL程序的不同执行方式

在当前shell环境运行:继承并影响当前环境。
. a.sh
source a.sh
实际上,”.”号与source的效果是一样的。
在当前shell运行时,需要注意,shell程序会影响、改变当前shell的环境。假如a.sh中有exit指令,那么用户将退出shell登录。
启动新的shell执行:只继承export输入的变量,并切不影响父进程的环境。exit指令只是退出新启动的shell。
bash a.sh
bash <a.sh
chmod+x a.sh
./a.sh
在( ) 中运行:executed in a subshell environment
也是在新的进程中运行。但是继承关系有点复杂,实验如下:
####START####
$su -
#chmod +x a.sh
#cat a.sh
echo $a
exit
#a=FreeBSD
#(./a.sh)
#(source a.sh)
FreeBSD
#(. a.sh)
FreeBSD
####END####
a.sh中的exit均没有影响到当前shell,但是却继承了当前shell的变量,但是改变a的操作却不能影响当前shell下a的值。
另外:
exec ./a.sh
关于exec:
exec: exec [-cl] [-a name] file [redirection ...]
Exec FILE, replacing this shell with the specified program.
If FILE is not specified, the redirections take effect in this
shell. If the first argument is `-l’, then place a dash in the
zeroth arg passed to FILE, as login does. If the `-c’ option
is supplied, FILE is executed with a null environment. The `-a’
option means to make set argv[0] of the executed process to NAME.
If the file cannot be executed and the shell is not interactive,
then the shell exits, unless the shell option `execfail’ is set.
man bash
Compound Commands
A compound command is one of the following:
(list) list  is  executed in a subshell environment (see COMMAND EXECUTION ENVIRONMENT below).  Variable assignments and builtin commands that affect the shell’s environment do not remain in effect after the command completes.  The return status is the exit status  of list.
{ list; }
list  is simply executed in the current shell environment.  list must be terminated with a newline or semicolon.  This is known as a group command.  The return status is the exit status of list.  Note that unlike the metacharacters ( and ), { and } are reserved words and must occur where a reserved word is permitted to be recognized.  Since they do not cause a word break, they must be sep-arated from list by whitespace.
Command Substitution
Command substitution allows the output of a command to replace the command name.  There are two forms:
$(command)
or
‘command‘
Bash performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing  newlines  deleted.  Embedded newlines are not deleted, but they may be removed during word splitting.  The command substitution $(cat file) can be replaced by the equivalent but faster $(< file).
When the old-style backquote form of substitution is used, backslash retains its literal meaning except when followed by $, ‘, or \.  The first backquote not preceded by a backslash terminates the command substitution.  When using the $(command) form, all characters between the parentheses make up the command; none are treated specially.
Command substitutions may be nested.  To nest when using the backquoted form, escape the inner backquotes with backslashes.
If the substitution appears within double quotes, word splitting and pathname expansion are not performed on the results.
COMMAND EXECUTION ENVIRONMENT
The shell has an execution environment, which consists of the following:
·      open files inherited by the shell at invocation, as modified by redirections supplied to the exec builtin
·      the current working directory as set by cd, pushd, or popd, or inherited by the shell at invocation
·      the file creation mode mask as set by umask or inherited from the shell’s parent
·      current traps set by trap
·      shell parameters that are set by variable assignment or with set or inherited from the shell’s parent in the environment
·      shell functions defined during execution or inherited from the shell’s parent in the environment
·      options enabled at invocation (either by default or with command-line arguments) or by set
·      options enabled by shopt
·      shell aliases defined with alias
·      various process IDs, including those of background jobs, the value of $$, and the value of $PPID
When a simple command other than a builtin or shell function is to be executed, it is invoked in a separate  execution  environment  that consists of the following.  Unless otherwise noted, the values are inherited from the shell.
·      the shell’s open files, plus any modifications and additions specified by redirections to the command
·      the current working directory
·      the file creation mode mask
·      shell variables and functions marked for export, along with variables exported for the command, passed in the environment
·      traps caught by the shell are reset to the values inherited from the shell’s parent, and traps ignored by the shell are ignored
A command invoked in this separate environment cannot affect the shell’s execution environment.
Command substitution, commands grouped with parentheses, and asynchronous commands are invoked in a subshell environment that is a duplicate of the shell environment, except that traps caught by the shell are reset to the values that the shell inherited from its parent  at invocation.   Builtin  commands  that are invoked as part of a pipeline are also executed in a subshell environment.  Changes made to the subshell environment cannot affect the shell’s execution environment.
If a command is followed by a & and job control is not active, the default standard input for the command is the  empty  file  /dev/null. Otherwise, the invoked command inherits the file descriptors of the calling shell as modified by redirections。