?
Solved

Could you please explain the unix shell code

Posted on 2005-04-14
7
Medium Priority
?
488 Views
Last Modified: 2013-12-26
Hi all,

Please explain the code in detail. I have 2 sample scripts, but I didn't get clear idea about this. I am new to this shell scripting area.

1.

#!/bin/ksh
PATH=$PATH:/usr/local/bin:/users/pgp-6.5.2
WORKDIR=/users/xfercrd
export PATH WORKDIR
find $WORKDIR/archive -mtime +45 -exec rm {} \;
cp $WORKDIR/cuc.ftp $WORKDIR/cuc.ftp.save
cp $WORKDIR/ftpupload $WORKDIR/ftpupload.save
cp $WORKDIR/vaxdir $WORKDIR/vaxdir.save
$WORKDIR/bin/getdirlist
$WORKDIR/getdir > $WORKDIR/vaxdir
$WORKDIR/bin/makeqfile
$WORKDIR/cuc.ftp
PGPPASS="hardware rocket cafe"
PGPPATH=/users/cucpgp
export PGPPASS PGPPATH
$WORKDIR/bin/procftp
PGPPASS=""
export PGPPASS
cp $WORKDIR/?CUE* $WORKDIR/archive
rm $WORKDIR/S*

and 2 program is

#!/bin/ksh
# File : lbl

# Set Variables
PATH=$PATH:/usr/local/bin
WORKDIR=/users/xferexplbl
export PATH WORKDIR
dirpath='$leg'
export dirpath

# Remove Archive Files Older Than 60 Days
find $WORKDIR/archive -mtime +60 -exec rm {} \;

# Copy Old FTP Runs To A Backup
cp $WORKDIR/cuc.ftp $WORKDIR/cuc.ftp.save
cp $WORKDIR/vaxdir $WORKDIR/vaxdir.save

# FTP To Retrieve A List Of File Names To Be Picked Up
ftp  -v corp.cuc.com << EOFDIR | tee  $WORKDIR/vaxdir
cd exprpt
dir EXPSCRLBL*
quit
EOFDIR

# Create FTP Command File
$WORKDIR/bin/mkftpfile

# Execute FTP Command File
$WORKDIR/cuc.ftp

# Create Upload File, FTP Upload Command File
# And Email And Log Files
$WORKDIR/bin/mkrecord.pl

# Execute FTP Upload Command File (Copy To Production)
$WORKDIR/ftpupload

# Compress Files And Move To Archive Directory
compress $WORKDIR/EXP*
mv $WORKDIR/EXP* $WORKDIR/archive

---------------------

Why $PATH is specified in the below statement?
# Set Variables
PATH=$PATH:/usr/local/bin


Thanks
r_kar
0
Comment
Question by:r_kar
7 Comments
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 13788331
> Why $PATH is specified in the below statement?
> # Set Variables
> PATH=$PATH:/usr/local/bin

This has the effect of appending /usr/local/bin to the existing PATH.
0
 
LVL 38

Accepted Solution

by:
yuzh earned 1200 total points
ID: 13788356
A Shell script is similar to batch file in windows (DOS), it contains a collection of Unix commands for doing the job.

Have a look at the following links to learn shell scripting:
      http://uwsg.ucs.indiana.edu/usail/concepts/shell-scripting.html
      you can get more tutorial links from http:Q_20890663.html

When you read a shell script, if you don't understand what it does, you can do a
"man" to learn what it does, eg:

cp $WORKDIR/ftpupload $WORKDIR/ftpupload.save

it copy file /users/xfercrd/ftpupload /users/xfercrd/ftpupload.save

type in:
man cp
to learn more details about the script.

A line starting with # is a comment, the 2nd script has very good commets about what the
script does, just read the comments in the script.

I'll give you some explainations for the first one.

#!/bin/ksh

the first line specify what shell systax to be used for the script, it use ksh (K shell in this case).

# Set Variables
PATH=$PATH:/usr/local/bin:/users/pgp-6.5.2
# Add :/usr/local/bin and /users/pgp-6.5.2 to your running path on top of the PATH
# defined in the user's login script

WORKDIR=/users/xfercrd
export PATH WORKDIR

# delete file older than 45 days under /users/xfercrd.
# man find
# to learn more details
find $WORKDIR/archive -mtime +45 -exec rm {} \;

# backup files -- simple copy
cp $WORKDIR/cuc.ftp $WORKDIR/cuc.ftp.save
cp $WORKDIR/ftpupload $WORKDIR/ftpupload.save
cp $WORKDIR/vaxdir $WORKDIR/vaxdir.save

#run programs/scripts
$WORKDIR/bin/getdirlist
$WORKDIR/getdir > $WORKDIR/vaxdir
$WORKDIR/bin/makeqfile
$WORKDIR/cuc.ftp

# set variables for the scrippt
PGPPASS="hardware rocket cafe"
PGPPATH=/users/cucpgp
export PGPPASS PGPPATH

# run procftp
$WORKDIR/bin/procftp
PGPPASS=""
export PGPPASS
cp $WORKDIR/?CUE* $WORKDIR/archive
# delete files with filename start with a S under /users/xfercrd
rm $WORKDIR/S*



0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 13788420
> Please explain the code in detail
anything of the form:
   VARIABLE=VALUE
in your script sets a variable named VARIABLE to the value VALUE
the usage of $VARIABLE  evaluates to the variables value (VALUE in my example)
export in your script makes the variable available to aother called programs
anything else are UNIX programs being called
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:r_kar
ID: 13788524
Hi yuzh,

Thank you for your valuable information.

Regards,
r_kar
0
 

Author Comment

by:r_kar
ID: 13788685
Hi yuzh,

Could you please explain this statement? It copies what type of files to archive directory?
cp $WORKDIR/?CUE* $WORKDIR/archive

and what the above statement does?

cp $WORKDIR/?CU??.??? $WORKDIR/archive

I am going to award you the points. Please answer to this
Thanks
0
 
LVL 38

Expert Comment

by:yuzh
ID: 13788747
? and * are wildcard characters. The wildcard ? matches any one character. The wildcard * matches any grouping of zero or more characters.

?CU??.??? mactch filename start with ANY char following by CU following by 2 chars and a
DOT . and three chars, eg:

mach

1CUab.abc
SCUmm.xyz, etc
cp $WORKDIR/?CU??.??? $WORKDIR/archive
will copy the files to dir $WORKDIR/archive, in your case

from: /users/xfercrd to /users/xfercrd/archive

Please have a look at the following page to learn more about WILDCARD chars:
http://www.sfu.ca/acs/howtos/b/b-8.htm
0
 

Author Comment

by:r_kar
ID: 13789107
Hi yuzh,

Thanks for your quick reply and excellent work.

Regards,
r_kar
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month13 days, 18 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question