Flylib.com

Books Software

 
 
 

The Source (Dot) Utility


The Source (Dot) Utility

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 variables that will be used throughout the remainder of the Qshell session. Qshell uses the source utility to execute the .profile, etc/profile, and $ENV startup files.

The closest counterparts in traditional iSeries programming are RPGs /COPY and COBOLs COPY directives or C/C++ #include directives, which include source code at compilation, as if the contents of the copybooks or header files had been keyed directly into the source member.

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.

start figure


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.

end figure

Figure 12.26: Failing to run the whoami.qsh script under the source utility produces nerroneous results in this example.

In Figure 12.27, SetMyEnv.qsh is a script file that assigns values to certain variables. The first print command shows that the name and dept variables have no values in the current process. Running the script without the dot operator does not affect the current process. However, after running the script with the dot operator, the variables are defined.

start figure


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

end figure

Figure 12.27: Sourcing a script is a good way to assign values to frequently used variables.



The Xargs Utility

The xargs utility (Execute Arguments) reads arguments from standard input and passes them to a command. If no command is given, xargs uses the echo command.

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

start figure


cat names.txt


Joe


Bill


Bob


Arlis


/home/jsmith $


xargs <names.txt


Joe Bill Bob Arlis


/home/jsmith $

end figure

Figure 12.28: Xargs combines multiple records into one and passes them to a utility.

start figure

{% 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 script

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

."
/home/jsmith $ find $PWD -exec grep print {} \; print "I am script

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

."
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 script

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

."
/home/jsmith $ find . -type f -print xargs grep print ./bin/donde.qsh:print "I am script

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

."
./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 script

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

."

end figure

Figure 12.29: The third command uses xargs to solve the problems with the previous two commands.

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 name it finds. The third command solves both of these problems. The names of the files found are sent to xargs , which passes them en masse to one instance of the grep command, as if the user had typed one grep and listed all of the file names as arguments.

Theres an interesting side effect of the xargs utility as it relates to grep . As shown above, when grep is searching more than one file, it shows the name of the file containing the match. However, when using xargs , theres no guarantee that the last call to grep will have more than one file. For example, the find utility might find 29 file names, and xargs determines that 28 of them can fit on the first grep command line. The second grep command line generated by xargs will contain only one file. If a match occurs in that single file, grep will not show the file name.

A common solution to this is to use the null file /dev/null in the grep . The following xargs command guarantees there are always two or more files in the generated grep command:

find . -type f -print  xargs grep print /dev/null