![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Capturing Input Parameters on Shell Script | wtolentino | UNIX for Dummies Questions & Answers | 2 | 4 Weeks Ago 05:33 PM |
| Problem with script not able to take parameters passed to it | dsravan | Shell Programming and Scripting | 3 | 02-28-2008 05:09 PM |
| help me in sending parameters from sqlplus script to unix shell script | Hara | Shell Programming and Scripting | 2 | 01-29-2008 03:31 PM |
| Problem with script not able to take parameters passed to it | dsravan | Shell Programming and Scripting | 7 | 10-09-2007 10:28 PM |
| DB2 stored procedure (with input parameters) from script | mpang_ | Shell Programming and Scripting | 1 | 12-13-2006 08:12 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Problem with Input Parameters using Shell Script
i have this basic code that accepts for two input one for the source file and the other is for the target directory. basically it is a simple copy command. the problem is at the end of the execution the second parameter string was passed to first parameter and it displays a message like: Code:
cp: archive: A file or directory in the path name does not exist. this is what it looks like when code is run Code:
$ sh archive_file_list.sh "/u02/app/ccalloc/dev/out/*.txt" "/export/home/ccalftd v/data/archive" current directory /u02/app/ccalloc/dev/out copied EATVDAILY02132008.txt to /export/home/ccalftdv/data/archive copied EATVDAILY03292007.txt to /export/home/ccalftdv/data/archive copied EATVDAILY04082008.txt to /export/home/ccalftdv/data/archive copied EATVDAILY05012008.txt to /export/home/ccalftdv/data/archive copied EATVDAILY05282008.txt to /export/home/ccalftdv/data/archive copied EATVDAILY06052007.txt to /export/home/ccalftdv/data/archive copied EATVDAILY06062007.txt to /export/home/ccalftdv/data/archive copied EATVDAILY06192009.txt to /export/home/ccalftdv/data/archive copied EATVDAILY06222009.txt to /export/home/ccalftdv/data/archive copied EATVDAILY06242009.txt to /export/home/ccalftdv/data/archive copied EATVDAILY07132009.txt to /export/home/ccalftdv/data/archive copied EATVDAILY07142009.txt to /export/home/ccalftdv/data/archive copied EATVDAILY07152009.txt to /export/home/ccalftdv/data/archive copied EATVDAILY07162009.txt to /export/home/ccalftdv/data/archive copied EATVDAILY10212009.txt to /export/home/ccalftdv/data/archive copied PCARDDAILY07102009.txt to /export/home/ccalftdv/data/archive copied citicc_pre_pack.txt to /export/home/ccalftdv/data/archive copied archive to /export/home/ccalftdv/data/archive cp: archive: A file or directory in the path name does not exist. below is the actual code Code:
pdir=`pwd`
p1=$1
p2=$2
# check for null parameter
if [ $# -lt 1 ] && [ $# -lt 2 ]; then
echo "current directory $pdir"
echo "required parameters for source and target directory is missing"
echo "e.g. archive_file_list.sh /directory_source/filesource.dat /directory_target"
echo " -------------------------------- -----------------"
echo " ^ ^"
echo
else
#check for directory entry only
if [ -d $p1 ]; then
pdir=$p1
echo current directory $pdir
cd $pdir
ls -latr
echo
#check for directory entry and file
elif [ -f $p1 ]; then
pdir=`dirname $p1`
echo current directory $pdir
cd $pdir
for f in $*
do
pfile=`basename $f`
#ls -altr $pfile
echo copied $pfile to "$p2"
cp $pfile $p2
done
else
echo $p1 not found
fi
fi
# put a white space
echo
thanks, warren |
|
||||
|
You use: Code:
for f in $*
do
pfile=`basename $f`
#ls -altr $pfile
echo copied $pfile to "$p2"
cp $pfile $p2
done
Before your command $* consists of two fields: Code:
$1="/u02/app/ccalloc/dev/out/*.txt" -and- $2="/export/home/ccalftd" By using referencing $* in the loop, $1 gets expanded to all those txt files, followed by $2 which gets expanded to /export/home/ccalftd as the last value. so after you apply basename, first your script performs : Code:
cp EATVDAILY02132008.txt ... until cp citicc_pre_pack.txt .. and then it tries to perform the last copy: Code:
cp archive to /export/home/ccalftdv/data/archive and of course /u02/app/ccalloc/dev/out/archive does not exist. S. |
|
||||
|
Further to Scrutinizer. Quote:
Code:
for f in ${p1}
Note: The "for" statement will break for large numbers of files if the expanded "for" line exceeds the maximum length of a command allowed in your shell. It will also break if a filename contains space characters. A while loop is preferred for long lists. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|