In this chapter, you've learned about some advanced Qshell programming features:
Use Qshell option settings with the set utility to trace and debug your shell scripts.
Use traps and signals to handle complex and unexpected error conditions. Detecting simple errors early can prevent more serious errors later.
Use the kill utility to manage running scripts or provide simple event-notification between scripts.
Working with a large number of related files can sometimes be
cumbersome and
Users familiar with iSeries save files will find similarities in the way other archive files are used to back up, move, or transfer groups of files. The iSeries save file is an archive file.
The most common types of archive files are zip files, jar files, and tar files. The archive file type refers to the utility used to create the archive file. For example, a zip utility creates zip archive files.
A compressed archive file uses less space than the sum of all of
the individual files that it contains. Some archive utilities, such
as
jar
, use data compression when creating the archive file,
while others, such as
tar
, do not. You can, however, use
compression utilities to compress data in archive files that do not
use data compression
Archive files are binary files that have defined data formats. When transferring archive files (perhaps using FTP), always transfer in binary mode. The Qshell archive utility converts the data as required by the utility during insertion or extraction of files from the archive files.
The examples in this chapter use an assortment of files, links, and directories as the source of the data. For demonstration purposes, the examples all use the v option to verbosely display the tar processing that occurs. The example data is listed in Figure 16.1
|
|
ls -l total: 36 kilobytes -rwxr-xr-x 1 JSMITH 0 107 Jul 13 15:45 HelloC.c -rwxr-xr-x 1 JSMITH 0 118 Jul 13 15:45 HelloCpp.C drwxrwsrwx 2 JSMITH 0 8192 Jul 13 15:46 data lrwxrwxrwx 1 JSMITH 0 37 Jul 13 15:44 goodoleboys.sql -> /home/jsmith/src/data/goodoleboys.sql lrwxrwxrwx 1 JSMITH 0 37 Jul 13 15:44 goodoleboys.txt -> /home/jsmith/src/data/goodoleboys.txt lrwxrwxrwx 1 JSMITH 0 31 Jul 13 15:43 qcsrc -> /qsys.lib/jsmith.lib/qcsrc.file ls -lL data qcsrc data: total: 40 kilobytes -rw-rw---- 1 JSMITH 0 3341 Jul 13 15:46 customers.txt -rw-rw---- 1 JSMITH 0 13400 Jul 13 15:46 notes.txt qcsrc: total: 228 kilobytes -rwx---rwx 1 JSMITH 0 73431 Jul 13 15:42 GOODBYE.MBR -rwx---rwx 1 JSMITH 0 19434 Jul 13 15:42 HELLO.MBR -rwx---rwx 1 JSMITH 0 73431 Jul 13 15:43 TEST.MBR
|
|
Tar, the tape archive utility, is named for its ability to read, write, and list files on a tape drive. A tar file is a binary file; the tar file format specifies an ASCII file format. The tar utility converts files inserted into the archive to ASCII. When extracting files, the tar utility converts files extracted from the archive to EBCDIC (the default CCSID of the job). Setting the QIBM_CCSID environment variable to a value other than zero causes tar to convert the ASCII data in the archived file to that CCSID.
Like tape media, a tar file contains a sequential list of file or directory entries. The same source file may be present in the tar file more than once. If you add or update file entries in a tar file, the entries are added to the end.
When the tar utility encounters a directory, it processes the directories recursively unless the directory is a symbolic link. Symbolic-link processing is controlled by the H , L , and P options.
The syntax of tar is shown here:
tar operation option [ ][ tar-file ][ block-size ][ files ]
The first option character passed to
tar
is the operation that
tar
should perform. Exactly one option for the operation is required from the list shown in Table 16.1. Specify the tar-file parameter if the
f
option is used;
|
Operation |
Description |
|---|---|
|
c |
Create a new archive file. |
|
x |
Extract files from an archive file. |
|
t |
List files in an archive file. |
|
r |
Append files to an existing archive file. |
|
u |
Update files in the archive file. The u option is rather misleading, because updated files are actually added to the end of the archive, similar to what you might expect the r option to do. |
If no files are specified for an extract or list operation, all files in the archive are
All other options for tar are optional. They are shown in Table 16.2, and affect the way that the tar operation is carried out.
|
Option |
Description |
|---|---|
|
b |
This option indicates that the
|
|
e |
Exit immediately if an error is
|
|
f |
This option indicates that the next argument is the archive file argument. Use an archive file
|
|
m |
Modification times of extracted files are not restored when extracting files from the archive file. |
|
o |
Extracted files are assigned the owner and
|
|
p |
Preserve the file mode, access, and modification times, as well as owner and group settings, of files when extracting files from the archive file. |
|
v |
Use verbose mode. Write additional information about files
|
|
w |
Wait for a confirmation from the user before taking any action. |
|
H |
Remove references to symbolic links for files specified on the command line. Archive the files referred to by the links instead of the symbolic links
|
|
L |
Remove references to all symbolic links encountered. Archive the files referred to by the links instead of the symbolic links themselves. |
|
P |
Do not remove references to symbolic links. Archive the symbolic links themselves instead of the files referred to by the symbolic links. This is the default symbolic-link behavior. |
|
X |
While processing directories recursively, do not process directories that have a different device ID (for example, symbolic links to directories in a different file system). |
This section contains several examples that
|
|
tar vc * HelloC.c HelloCpp.C archive.tar data data/customers.txt data/notes.txt goodoleboys.sql goodoleboys.txt qcsrc tar: 001-2298 For archive file tar and volume 1, 9 files were processed with 0 bytes read and 0 bytes written. ls -l archive.tar -rw-rw-rw- 1 JSMITH 0 30720 Jul 13 16:02 archive.tar tar tv -rwxr-xr-x 1 JSMITH 0 0 Jul 13 15:45 HelloC.c -rwxr-xr-x 1 JSMITH 0 0 Jul 13 15:45 HelloCpp.C -rw-rw-rw- 1 JSMITH 0 0 Jul 13 16:02 archive.tar drwxrwsrwx 2 JSMITH 0 0 Jul 13 15:46 data -rw-rw---- 1 JSMITH 0 0 Jul 13 15:46 data/customers.txt -rw-rw---- 1 JSMITH 0 0 Jul 13 15:46 data/notes.txt lrwxrwxrwx 1 JSMITH 0 0 Jul 13 15:44 goodoleboys.sql => /home/jsmith/src/data/goodoleboys.sql lrwxrwxrwx 1 JSMITH 0 0 Jul 13 15:44 goodoleboys.txt => /home/jsmith/src/data/goodoleboys.txt lrwxrwxrwx 1 JSMITH 0 0 Jul 13 15:43 qcsrc => /qsys.lib/jsmith.lib/qcsrc.file
|
|
Figure 16.3 demonstrates the
w
(Wait for Confirmation) option that prompts the user for individual file actions. Only the HelloC.c file is added to the archive file. A
|
|
tar cw H*
tar: Starting interactive file rename operation.
tar: The current file is HelloC.c with mode -rwxr-xr-x and a
modification time of Jul 13 17:01.
tar: Enter a new name, or a period (".") to quit, or press
Enter to skip this file:
.
tar: The file name is not changed.
tar: Starting interactive file rename operation.
tar: The current file is HelloCpp.C with mode -rwxr-xr-x and
a modification time of Jul 13 17:01.
tar: Enter a new name, or a period (".") to quit, or press
Enter to skip this file:
tar: The file is skipped.
tar tvf archive.tar
-rwxr-xr-x 1 JSMITH 0 0 Jul 13 17:01 HelloC.c
tar: 001-2298 For archive file tar and volume 1, 1 files were
processed with 0 bytes read and 102400 bytes written.
tar wuf archive.tar HelloC.c
tar: Starting interactive file rename operation.
tar: The current file is HelloC.c with mode -rwxr-xr-x and a
modification time of Jul 13 17:01.
tar: Enter a new name, or a period (".") to quit, or press
Enter to skip this file:
HelloC-version2.c
tar: The file name is changed to HelloC-version2.c.
tar tvf archive.tar
-rwxr-xr-x 1 JSMITH 0 0 Jul 13 17:01 HelloC.c
-rwxr-xr-x 1 JSMITH 0 0 Jul 13 17:01 HelloC-version2.c
tar: 001-2298 For archive file tar and volume 1, 2 files were
processed with 0 bytes read and 199168 bytes written.
|
|
Use the
L
option to create an archive file containing the current directory (the dot). With this option, the files or directories referred to by symbolic links (in Figure 16.4, qcsrc, goodoleboys.sql, and goodoleboys.txt) are copied directly into the archive instead of the links. When extracting those objects, only the files
|
|
tar cLf archive-file.tar . tar: 001-2298 For archive file tar and volume 1, 14 files were processed with 0 bytes read and 0 bytes written. tar tvf archive-file.tar drwxrwsrwx 2 JSMITH 0 0 Jul 13 16:26 . -rw-rw-rw- 1 JSMITH 0 0 Jul 12 16:39 ./goodoleboys.sql -rw-rw-rw- 1 JSMITH 0 0 Jun 29 13:03 ./goodoleboys.txt drwx---rwx 2 JSMITH 0 0 Jul 13 15:43 ./qcsrc -rwx---rwx 1 JSMITH 0 0 Jul 13 15:42 ./qcsrc/GOODBYE.MBR -rwx---rwx 1 JSMITH 0 0 Jul 13 15:42 ./qcsrc/HELLO.MBR -rwx---rwx 1 JSMITH 0 0 Jul 13 15:43 ./qcsrc/TEST.MBR -rwxr-xr-x 1 JSMITH 0 0 Jul 13 15:45 ./HelloCpp.C -rwxr-xr-x 1 JSMITH 0 0 Jul 13 15:45 ./HelloC.c drwxrwsrwx 2 JSMITH 0 0 Jul 13 15:46 ./data -rw-rw---- 1 JSMITH 0 0 Jul 13 15:46 ./data/customers.txt -rw-rw---- 1 JSMITH 0 0 Jul 13 15:46 ./data/notes.txt -rw-rw-rw- 1 JSMITH 0 0 Jul 13 16:28 ./archive.tar -rw-rw-rw- 1 JSMITH 0 0 Jul 13 16:26 ./test.tar tar: 001-2298 For archive file tar and volume 1, 14 files were processed with 0 bytes read and 266240 bytes written.
|
|
Figure 16.5 demonstrates a combination of the
X
and
L
options. While the
L
option causes tar to follow all symbolic links, the
X
option
|
|
tar cvXL . . ./goodoleboys.sql ./goodoleboys.txt ./qcsrc ./HelloCpp.C ./HelloC.c ./data ./data/customers.txt ./data/notes.txt ./archive.tar ./test.tar tar: 001-2298 For archive file tar and volume 1, 11 files were processed with 0 bytes read and 0 bytes written. tar tvf archive.tar drwxrwsrwx 2 JSMITH 0 0 Jul 13 16:26 . -rw-rw-rw- 1 JSMITH 0 0 Jul 12 16:39 ./goodoleboys.sql -rw-rw-rw- 1 JSMITH 0 0 Jun 29 13:03 ./goodoleboys.txt drwx---rwx 2 JSMITH 0 0 Jul 13 15:43 ./qcsrc -rwxr-xr-x 1 JSMITH 0 0 Jul 13 15:45 ./HelloCpp.C -rwxr-xr-x 1 JSMITH 0 0 Jul 13 15:45 ./HelloC.c drwxrwsrwx 2 JSMITH 0 0 Jul 13 15:46 ./data -rw-rw---- 1 JSMITH 0 0 Jul 13 15:46 ./data/customers.txt -rw-rw---- 1 JSMITH 0 0 Jul 13 15:46 ./data/notes.txt -rw-rw-rw- 1 JSMITH 0 0 Jul 13 16:32 ./archive.tar -rw-rw-rw- 1 JSMITH 0 0 Jul 13 16:26 ./test.tar tar: 001-2298 For archive file tar and volume 1, 11 files were processed with 0 bytes read and 92160 bytes written.
|
|
If no file
|
|
{% if main.adsdop %}{% include 'adsenceinline.tpl' %}{% endif %}
ls -1 tar cvf test.tar HelloC.c HelloCpp.C archive.tar data data/customers.txt data/notes.txt goodoleboys.sql goodoleboys.txt qcsrc tar: 001-2298 For archive file tar and volume 1, 9 files were processed with 0 bytes read and 0 bytes written. tar tvf test.tar -rwxr-xr-x 1 JSMITH 0 0 Jul 13 15:45 HelloC.c -rwxr-xr-x 1 JSMITH 0 0 Jul 13 15:45 HelloCpp.C -rw-rw-rw- 1 JSMITH 0 0 Jul 13 16:24 archive.tar drwxrwsrwx 2 JSMITH 0 0 Jul 13 15:46 data -rw-rw---- 1 JSMITH 0 0 Jul 13 15:46 data/customers.txt -rw-rw---- 1 JSMITH 0 0 Jul 13 15:46 data/notes.txt lrwxrwxrwx 1 JSMITH 0 0 Jul 13 15:44 goodoleboys.sql => /home/jsmith/src/data/goodoleboys.sql lrwxrwxrwx 1 JSMITH 0 0 Jul 13 15:44 goodoleboys.txt => /home/jsmith/src/data/goodoleboys.txt lrwxrwxrwx 1 JSMITH 0 0 Jul 13 15:43 qcsrc => /qsys.lib/jsmith.lib/qcsrc.file tar: 001-2298 For archive file tar and volume 1, 9 files were processed with 0 bytes read and 61440 bytes written.
|
|
Figure 16.7 demonstrates restoring individual files from an archive. The archive contents are first shown using the
t
(List) option. The
x
(Extract) option
|
|
tar tvf archive.tar drwxrwsrwx 2 JSMITH 0 0 Jul 13 16:45 . lrwxrwxrwx 1 JSMITH 0 0 Jul 13 15:44 ./goodoleboys.sql => /home/jsmith/src/data/goodoleboys.sql lrwxrwxrwx 1 JSMITH 0 0 Jul 13 15:44 ./goodoleboys.txt => /home/jsmith/src/data/goodoleboys.txt lrwxrwxrwx 1 JSMITH 0 0 Jul 13 15:43 ./qcsrc => /qsys.lib/jsmith.lib/qcsrc.file -rwxr-xr-x 1 JSMITH 0 0 Jul 13 15:45 ./HelloCpp.C -rwxr-xr-x 1 JSMITH 0 0 Jul 13 15:45 ./HelloC.c drwxrwsrwx 2 JSMITH 0 0 Jul 13 15:46 ./data -rw-rw---- 1 JSMITH 0 0 Jul 13 15:46 ./data/customers.txt -rw-rw---- 1 JSMITH 0 0 Jul 13 15:46 ./data/notes.txt -rw-rw-rw- 1 JSMITH 0 0 Jul 13 16:45 ./archive.tar -rw-rw-rw- 1 JSMITH 0 0 Jul 13 16:26 ./test.tar tar: 001-2298 For archive file tar and volume 1, 11 files were processed with 0 bytes read and 92160 bytes written. tar xvf archive.tar './Hello*' ./data ./HelloCpp.C ./HelloC.c ./data ./data/customers.txt ./data/notes.txt tar: 001-2298 For archive file tar and volume 1, 11 files were processed with 0 bytes read and 92160 bytes written . tar xvf archive.tar . ./goodoleboys.sql ./goodoleboys.txt ./qcsrc ./HelloCpp.C ./HelloC.c ./data ./data/customers.txt ./data/notes.txt ./archive.tar ./test.tar tar: 001-2298 For archive file tar and volume 1, 11 files were processed with 0 bytes read and 92160 bytes written.
|
|
The u (Update) option updates files in an archive. Updated files are inserted into the archive again. The listings in Figure 16.8 ( tar tvf ) show the contents of the tar file before and after the update. After the update, note that the files HelloC.c and HelloCpp.C are present in the archive twice with different file timestamps. When the HelloC.c file is extracted, the tar utility processes the archive sequentially (like a tape drive). The HelloC.c file is encountered twice and extracted each time. The result is that the latest version of the HelloC.c file with timestamp "Jul 13 16:58" is extracted.
|
|
tar tvf archive.tar -rwxr-xr-x 1 JSMITH 0 0 Jul 13 15:45 HelloC.c -rwxr-xr-x 1 JSMITH 0 0 Jul 13 15:45 HelloCpp.C drwxrwsrwx 2 JSMITH 0 0 Jul 13 15:46 data -rw-rw---- 1 JSMITH 0 0 Jul 13 15:46 data/customers.txt -rw-rw---- 1 JSMITH 0 0 Jul 13 15:46 data/notes.txt lrwxrwxrwx 1 JSMITH 0 0 Jul 13 16:54 goodoleboys.sql => /home/jsmith/src/data/goodoleboys.sql lrwxrwxrwx 1 JSMITH 0 0 Jul 13 16:54 goodoleboys.txt => /home/jsmith/src/data/goodoleboys.txt lrwxrwxrwx 1 JSMITH 0 0 Jul 13 16:54 qcsrc => /qsys.lib/jsmith.lib/qcsrc.file tar: 001-2298 For archive file tar and volume 1, 10 files were processed with 0 bytes read and 215040 bytes written. ls -l Hello* -rwxr-xr-x 1 JSMITH 0 92160 Jul 13 16:58 HelloC.c -rwxr-xr-x 1 JSMITH 0 118 Jul 13 16:58 HelloCpp.C tar uvf archive.tar Hello* tar: 001-2315 The archive is being read to position to the end of the archive. done. HelloC.c HelloCpp.C tar: 001-2298 For archive file tar and volume 1, 10 files were processed with 0 bytes read and 114176 bytes written. tar tvf archive.tar -rwxr-xr-x 1 JSMITH 0 0 Jul 13 15:45 HelloC.c -rwxr-xr-x 1 JSMITH 0 0 Jul 13 15:45 HelloCpp.C drwxrwsrwx 2 JSMITH 0 0 Jul 13 15:46 data -rw-rw---- 1 JSMITH 0 0 Jul 13 15:46 data/customers.txt -rw-rw---- 1 JSMITH 0 0 Jul 13 15:46 data/notes.txt lrwxrwxrwx 1 JSMITH 0 0 Jul 13 16:54 goodoleboys.sql => /home/jsmith/src/data/goodoleboys.sql lrwxrwxrwx 1 JSMITH 0 0 Jul 13 16:54 goodoleboys.txt => /home/jsmith/src/data/goodoleboys.txt lrwxrwxrwx 1 JSMITH 0 0 Jul 13 16:54 qcsrc => /qsys.lib/jsmith.lib/qcsrc.file -rwxr-xr-x 1 JSMITH 0 0 Jul 13 16:58 HelloC.c -rwxr-xr-x 1 JSMITH 0 0 Jul 13 16:58 HelloCpp.C tar: 001-2298 For archive file tar and volume 1, 10 files were processed with 0 bytes read and 215040 bytes written. tar xvf archive.tar HelloC.c HelloC.c HelloC.c tar: 001-2298 For archive file tar and volume 1, 12 files were processed with 0 bytes read and 307200 bytes written.
|
|