LiveFire Labs - Online UNIX Training - Company Logo


Online UNIX Training with Hands-on Internet Lab


"Eliminate the expense and inconvenience of classroom training without eliminating the classroom experience."

 


Home
Internet Lab
Console Access
Sample Course

Student Login


LiveFire Labs' UNIX Tip, Trick, or Shell Script of the Week - View


Questions? Call
1.888.843.1637 or send us email





January 19, 2004 - Subshells & Subshell Grouping - Part III

Our discussion of subshells and subshell grouping will conclude with an example using the tar (tape archive) command.  For this example, tar is used on both sides of a pipe to copy a directory tree to another disk directory (a disk to disk copy without an intermediary file).  The general format for this operation is:

tar read from source directory | tar write to destination directory

Since the tar write (extract) will occur in a different directory than the tar read, the write operation needs to be performed within a subshell.  

[root@hawk] # pwd
/export/home/jdoe/project1_files
[root@hawk] #
[root@hawk] # find .
.
./file1
./files2
./files3
./tmp
./tmp/file4
./tmp/file5
./tmp/file6
[root@hawk] #
[root@hawk] # mkdir /export/home/jdoe/project1_backup
[root@hawk] #
[root@hawk] # tar cvfp - . | ( cd /export/home/jdoe/project1_backup; tar xvfp - )
a ./ 0K
a ./file1 1K
a ./files2 1K
a ./files3 1K
a ./tmp/ 0K
a ./tmp/file4 1K
a ./tmp/file5 1K
a ./tmp/file6 1K
tar: blocksize = 16
x ., 0 bytes, 0 tape blocks
x ./file1, 53 bytes, 1 tape blocks
x ./files2, 53 bytes, 1 tape blocks
x ./files3, 53 bytes, 1 tape blocks
x ./tmp, 0 bytes, 0 tape blocks
x ./tmp/file4, 52 bytes, 1 tape blocks
x ./tmp/file5, 52 bytes, 1 tape blocks
x ./tmp/file6, 52 bytes, 1 tape blocks
[root@hawk] #
[root@hawk] # cd /export/home/jdoe/project1_backup
[root@hawk] #
[root@hawk] # find .
.
./file1
./files2
./files3
./tmp
./tmp/file4
./tmp/file5
./tmp/file6
[root@hawk] #

As you can see from the output, the files in the source directory tree were copied to the destination directory.

In review, there are both advantages and disadvantages to using subshells, and your ability to determine when and when not to use them will improve as your shell scripting skills mature.  They are effective for controlling the shell's environment, enabling you to avoid any undesired side effects in the current environment.  This was previously illustrated by looking at the value of a variable in the login shell, and then in a subshell (view tip).  Defining a variable within a subshell effectively makes it a local variable (local to only the subshell).

Another example of avoiding undesirable changes to the login shell was demonstrated by changing the working directory within a subshell (view tip).  The directory can be changed as many times as needed without having an impact on the login shell's working directory.

One of the major disadvantages of utilizing subshells is the fact that more system resources are used to run them.  It is more efficient to run all programs in the current shell.




Learn more...
  

If you are new to the UNIX or Linux operating system and would like to learn more, you may want to consider registering for LiveFire Labs' UNIX and Linux Operating System Fundamentals online training course.

If you already have a solid grasp of the fundamentals but would like to learn more about the Korn shell and basic and advanced shell scripting, taking our Korn Shell Scripting course will be beneficial to you.

Our innovative hands-on training model allows you to learn UNIX by completing hands-on exercises on real servers in our Internet Lab.


More Tips...

· 
Popular UNIX Tips from the Past

spacer Box Border
 

Receive the UNIX Tip, Trick, or Shell Script of the Week by Email


First Name:


Email Address:






   1.888.843.1637

Home - Contact us - Company info - Privacy Statement   

 
©2002-2004 LiveFire Labs.  All rights reserved.
Linux® is a registered trademark of Linus Torvalds, author and developer of this public domain operating system.
UNIX® is a registered trademark of The Open Group in the United States and other countries.