The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



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

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 4 Weeks Ago
wtolentino wtolentino is offline
Registered User
  
 

Join Date: Oct 2005
Posts: 33
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
  #2 (permalink)  
Old 3 Weeks Ago
vbe's Avatar
vbe vbe is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2005
Location: Switzerland - GE
Posts: 1,577
Does this happen each time?
I dont see how or why unless you had a faulty symbolic link blahblah.txt to archive somehow somewhere...
  #3 (permalink)  
Old 3 Weeks Ago
Scrutinizer Scrutinizer is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 735
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.
  #4 (permalink)  
Old 3 Weeks Ago
methyl methyl is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 1,184
Further to Scrutinizer.

Quote:
for f in $*
Should be

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

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:07 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0