Problem with Input Parameters using Shell Script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Problem with Input Parameters using Shell Script
# 1  
Old 11-04-2009
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  
Old 11-06-2009
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  
Old 11-06-2009
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  
Old 11-06-2009
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.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem in taking input Shell script

I was writing a shell script, where i need to run a command through script and then take input from user and later terminate it. i am not sure how to take input in a proper format immediately after a command. example: below command line is used to import some data in to database (LDAP)... (3 Replies)
Discussion started by: Mridul17
3 Replies

2. Shell Programming and Scripting

Problem with input parameters

Hi I wrote a script which lists the content of a given directory. I have just one problem. If you give 2 or more parameters, then it gives a strange error. After that error, it gives also an error from my script, but normally it shouldn't give that error. Here's my script.You can test it. ... (3 Replies)
Discussion started by: hss
3 Replies

3. Shell Programming and Scripting

Help parsing job script input parameters

I have a job script that runs with input parms from the command line. job.sh -p parm1_parm2_parm3_parm4_file_1.dat The parms are separated by _ The last parm is a file name and can have an _ in the name. I currently use the following commands to extract the parms parm1=`eval echo... (3 Replies)
Discussion started by: jclanc8
3 Replies

4. Shell Programming and Scripting

call another shell script and pass parameters to that shell script

Hi, I basically have 2 shell scripts. One is a shell script will get the variable value from the user. The variable is nothing but the IP of the remote system. Another shell script is a script that does the job of connecting to the remote system using ssh. This uses a expect utility in turn. ... (2 Replies)
Discussion started by: sunrexstar
2 Replies

5. Shell Programming and Scripting

Problem with Input parameters

Hi, I am facing a weird problem with input parameters. Please find more details about my problem below: a) I am executing a korn shellscript with 11 input parameters from "Appworx" tool (it's a scheduling tool) and immediately displaying those 11 parameter values in unixscript and noticed... (4 Replies)
Discussion started by: npk2210
4 Replies

6. Shell Programming and Scripting

problem with Regular expression as input in shell script

Hi, I have script which will take a string as input and search in a file. But when I want to search a pattern which has special characters script is ignoring it. For example: I want to search a pattern "\.tumblr\.com". shell script is removing \ (backslah) and trying to search... (7 Replies)
Discussion started by: Anjan1
7 Replies

7. UNIX for Dummies Questions & Answers

Capturing Input Parameters on Shell Script

i have this basic line of code that doesn't work. i simply want to get the input parameter strings but when the script is run it appears that the first parameter is assigning the value to the second parameter. #!/bin/sh pdir=`pwd` p1=$1 p2=$2 echo "directory: $pdir\n" echo "parameter... (2 Replies)
Discussion started by: wtolentino
2 Replies

8. Shell Programming and Scripting

(Problem) Script with Parameters file

Hello, this is my first post, and.... obviusly, i have a problem, i have this script: #!/bin/sh . /tmp/variables ... ... DESTINATION=`hostname`_DESTINATION ... and the /tmp/variables has: #!/bin/sh aix53_DESTINATION="/temporal/`hostname`.mksysb.`date +\%d\%m\%y`" ** The problem is... (2 Replies)
Discussion started by: pelado
2 Replies

9. Shell Programming and Scripting

Problem with script not able to take parameters passed to it

when I pass any 2 parameters it always says: Number of parameters passed: 1 and the job_name as x Can some body help? (7 Replies)
Discussion started by: dsravan
7 Replies

10. Shell Programming and Scripting

DB2 stored procedure (with input parameters) from script

I have a db2 stored procedure on my database which require 3 parameters, I have the following db2 command in a sql script CONNECT TO SAMPLE; CALL BACKUP(INPUT_1, INPUT_2, INPUT3); Usually, I would just invoke this sql script from my shell script like below db2 -tf... (1 Reply)
Discussion started by: mpang_
1 Replies
Login or Register to Ask a Question