Wednesday, July 30, 2008

Shell Commands

source, . (dot command)

         This command, when invoked from the command line, executes a script. Within a script, a source file-name loads the file file-name. Sourcing a file (dot-command) imports code into the script, appending to the script (same effect as the #include directive in a C program).

Ex. cat data-file

variable=11
variable1=111
variable2=222

# cat source.sh 
#!/bin/bash
# import from data-file.
. data-file
echo "$variable"
echo "$variable1"

exec
  This shell builtin replaces the current process with a specified command. Normally when the shell encounters a command, it forks off a child process to actually execute the command. Using the exec builtin, the shell does not fork, and the command exec'ed replaces the shell. When used in a script, therefore, it forces an exit from the script when the exec'ed command terminates. 
Ex. 

#!/bin/bash
exec echo "Exiting \"$0\"." # Exit from script here.
# ----------------------------------
# The following lines never execute.
echo "This echo will never echo."
exit 99

wait
  Suspend script execution until all jobs running in background have terminated, or until the job number or process ID specified as an option terminates. Returns the exit status of waited-for command.

You may use the wait command to prevent a script from exiting before a background job finishes executing (this would create a dreaded orphan process).

No comments: