The source (or dot) utility executes a script or function within the current process, not in a child process. Running a script or function under the source utility is equivalent to keying the commands within the current process.
The source utility has the following two forms:
. scriptname source scriptname
In releases prior to V5R2, you must use the "dot" form of the source utility. As of V5R2, you may use either form.
A common use of the
source
utility is to initialize
The
An example of the source utility is shown in Figure 12.26. Whoami.qsh is a single-line script that prints the ID of the process in which the script is running. The interactive Qshell session is process 2399, as the print command shows. Running whoami.qsh without the dot operator causes Qshell to start another process, 2405. Running whoami.qsh with the dot operator causes Qshell to run the script in the current process, as if the print command in the script had been typed in the current process.
|
|
cat whoami.qsh print "I am process $$." /home/jsmith $ print $$ 2399 /home/jsmith $ whoami.qsh I am process 2405. /home/jsmith $ . whoami.qsh I am process 2399.
|
|
In Figure 12.27, SetMyEnv.qsh is a script file that
|
|
cat SetMyEnv.qsh name=Joe Smith firstname=Joe lastname=Smith dept=Accounting /home/jsmith $ print $name $dept /home/jsmith $ SetMyEnv.qsh /home/jsmith $ print $name $dept /home/jsmith $ . SetMyEnv.qsh /home/jsmith $ print $name $dept Joe Smith Accounting
|
|
The
xargs
utility (Execute Arguments) reads arguments
from standard input and
To understand how xargs works, suppose that an input stream contains four records with the values AB, CD, EF , and GH . When xargs reads these values, it combines them into one record of values separated by white spaces. This single record is passed as arguments to the command that is listed as xargs ' first non-option argument. If that command were rm , for example, Qshell would execute the following:
rm AB CD EF GH
If no command is given , echo is used by default.
Figures 12.28 and 12.29 give examples of xargs . In Figure 12.28, xargs effectively combines four records into one, passing the records as arguments to echo (the default command).
|
|
cat names.txt Joe Bill Bob Arlis /home/jsmith $ xargs <names.txt Joe Bill Bob Arlis /home/jsmith $
|
|
|
|
{% if main.adsdop %}{% include 'adsenceinline.tpl' %}{% endif %}
grep print * bu.qsh: print -u2 "bu is not a directory - backup aborted." casewild.qsh: * ) print -u2 "Parameter 1 is invalid." demo.csh:print "Number of arguments: $#" demo.csh: printf "%3d (%s)\n" $argnbr "" donde.qsh:print "I am scriptgrep print * bu.qsh: print -u2 "bu is not a directory - backup aborted." casewild.qsh: * ) print -u2 "Parameter 1 is invalid." demo.csh:print "Number of arguments: $#" demo.csh: printf "%3d (%s)\n" $argnbr "$1" donde.qsh:print "I am script $0." /home/jsmith $ find $PWD -." /home/jsmith $ find $PWD -exec grep print {} \; print "I am scriptexec grep print {} \; print "I am script $0." print "here im is" * ) print -u2 "Parameter 1 is invalid." print -u2 "bu is not a directory - backup aborted." * ) print -u2 "Parameter 1 is invalid." print "Number of arguments: $#" printf "%3d (%s)\n" $argnbr "$1" print "I am script $0." /home/jsmith $ find . -type f -print xargs grep print ./bin/donde.qsh:print "I am script $0." ./bin/one.qsh:print "here i'm is" ./temp1/casewild.qsh: * ) print -u2 "Parameter 1 is invalid." ./bu.qsh: print -u2 "bu is not a directory - backup aborted." ./casewild.qsh: * ) print -u2 "Parameter 1 is invalid." ./demo.csh:print "Number of arguments: $#" ./demo.csh: printf "%3d (%s)\n" $argnbr "$1" ./donde.qsh:print "I am script $0."grep print * bu.qsh: print -u2 "bu is not a directory - backup aborted." casewild.qsh: * ) print -u2 "Parameter 1 is invalid." demo.csh:print "Number of arguments: $#" demo.csh: printf "%3d (%s)\n" $argnbr "$1" donde.qsh:print "I am script $0." /home/jsmith $ find $PWD -." print "here im is" * ) print -u2 "Parameter 1 is invalid." print -u2 "bu is not a directory - backup aborted." * ) print -u2 "Parameter 1 is invalid." print "Number of arguments: $#" printf "%3d (%s)\n" $argnbr "" print "I am scriptexec grep print {} \; print "I am script $0." print "here im is" * ) print -u2 "Parameter 1 is invalid." print -u2 "bu is not a directory - backup aborted." * ) print -u2 "Parameter 1 is invalid." print "Number of arguments: $#" printf "%3d (%s)\n" $argnbr "$1" print "I am script $0." /home/jsmith $ find . -type f -print xargs grep print ./bin/donde.qsh:print "I am script $0." ./bin/one.qsh:print "here i'm is" ./temp1/casewild.qsh: * ) print -u2 "Parameter 1 is invalid." ./bu.qsh: print -u2 "bu is not a directory - backup aborted." ./casewild.qsh: * ) print -u2 "Parameter 1 is invalid." ./demo.csh:print "Number of arguments: $#" ./demo.csh: printf "%3d (%s)\n" $argnbr "$1" ./donde.qsh:print "I am script $0."grep print * bu.qsh: print -u2 "bu is not a directory - backup aborted." casewild.qsh: * ) print -u2 "Parameter 1 is invalid." demo.csh:print "Number of arguments: $#" demo.csh: printf "%3d (%s)\n" $argnbr "$1" donde.qsh:print "I am script $0." /home/jsmith $ find $PWD -." /home/jsmith $ find . -type f -print xargs grep print ./bin/donde.qsh:print "I am scriptexec grep print {} \; print "I am script $0." print "here im is" * ) print -u2 "Parameter 1 is invalid." print -u2 "bu is not a directory - backup aborted." * ) print -u2 "Parameter 1 is invalid." print "Number of arguments: $#" printf "%3d (%s)\n" $argnbr "$1" print "I am script $0." /home/jsmith $ find . -type f -print xargs grep print ./bin/donde.qsh:print "I am script $0." ./bin/one.qsh:print "here i'm is" ./temp1/casewild.qsh: * ) print -u2 "Parameter 1 is invalid." ./bu.qsh: print -u2 "bu is not a directory - backup aborted." ./casewild.qsh: * ) print -u2 "Parameter 1 is invalid." ./demo.csh:print "Number of arguments: $#" ./demo.csh: printf "%3d (%s)\n" $argnbr "$1" ./donde.qsh:print "I am script $0."grep print * bu.qsh: print -u2 "bu is not a directory - backup aborted." casewild.qsh: * ) print -u2 "Parameter 1 is invalid." demo.csh:print "Number of arguments: $#" demo.csh: printf "%3d (%s)\n" $argnbr "$1" donde.qsh:print "I am script $0." /home/jsmith $ find $PWD -." ./bin/one.qsh:print "here i'm is" ./temp1/casewild.qsh: * ) print -u2 "Parameter 1 is invalid." ./bu.qsh: print -u2 "bu is not a directory - backup aborted." ./casewild.qsh: * ) print -u2 "Parameter 1 is invalid." ./demo.csh:print "Number of arguments: $#" ./demo.csh: printf "%3d (%s)\n" $argnbr "" ./donde.qsh:print "I am scriptexec grep print {} \; print "I am script $0." print "here im is" * ) print -u2 "Parameter 1 is invalid." print -u2 "bu is not a directory - backup aborted." * ) print -u2 "Parameter 1 is invalid." print "Number of arguments: $#" printf "%3d (%s)\n" $argnbr "$1" print "I am script $0." /home/jsmith $ find . -type f -print xargs grep print ./bin/donde.qsh:print "I am script $0." ./bin/one.qsh:print "here i'm is" ./temp1/casewild.qsh: * ) print -u2 "Parameter 1 is invalid." ./bu.qsh: print -u2 "bu is not a directory - backup aborted." ./casewild.qsh: * ) print -u2 "Parameter 1 is invalid." ./demo.csh:print "Number of arguments: $#" ./demo.csh: printf "%3d (%s)\n" $argnbr "$1" ./donde.qsh:print "I am script $0."grep print * bu.qsh: print -u2 "bu is not a directory - backup aborted." casewild.qsh: * ) print -u2 "Parameter 1 is invalid." demo.csh:print "Number of arguments: $#" demo.csh: printf "%3d (%s)\n" $argnbr "$1" donde.qsh:print "I am script $0." /home/jsmith $ find $PWD -."exec grep print {} \; print "I am script $0." print "here im is" * ) print -u2 "Parameter 1 is invalid." print -u2 "bu is not a directory - backup aborted." * ) print -u2 "Parameter 1 is invalid." print "Number of arguments: $#" printf "%3d (%s)\n" $argnbr "$1" print "I am script $0." /home/jsmith $ find . -type f -print xargs grep print ./bin/donde.qsh:print "I am script $0." ./bin/one.qsh:print "here i'm is" ./temp1/casewild.qsh: * ) print -u2 "Parameter 1 is invalid." ./bu.qsh: print -u2 "bu is not a directory - backup aborted." ./casewild.qsh: * ) print -u2 "Parameter 1 is invalid." ./demo.csh:print "Number of arguments: $#" ./demo.csh: printf "%3d (%s)\n" $argnbr "$1" ./donde.qsh:print "I am script $0."
|
|
In Figure 12.29, the first
grep
command looks for the
word
print
in all files of the current directory. However,
it does not search subdirectories. The second
grep
does
search subdirectories; however, it does not tell which file
contains which lines, and it runs slowly. The reason for these
problems is that the
find
command launches a new instance of
grep
for each file
Theres an interesting side effect of the
xargs
utility as
it
A common solution to this is to use the null file /dev/null in
the
grep
. The following
xargs
command
find . -type f -print xargs grep print /dev/null