We help IT Professionals succeed at work.

passing param with space in text to shell script

feng042497
feng042497 asked
on
Hi, Gurus:
    I tried to use single quota to pass a param with space to a shell script but had some problem, which described below.

The shell script takes 4 params.  Inside the script, it's just simple like following.

# ---------------------------------------------
#!/bin/ksh

#Make the file copy using scp
#
# Arg 1: source directory including server name, i.e. eportdev01:/local/dev
#        if no server name is specified, it means using current server
# Arg 2: orginal file name
# Arg 3: destination directory including server name
# Arg 4: destination file name

if ( scp  #!/bin/ksh

#Make the file copy using scp
#
# Arg 1: source directory including server name, i.e. eportdev01:/local/dev
#        if no server name is specified, it means using current server
# Arg 2: orginal file name
# Arg 3: destination directory including server name
# Arg 4: destination file name

if ( scp "'"$1/"$2""'" $3/$4 > /dev/null 2>&1 )
then
        echo "success"
else
        echo "failed"
fi
# -----------------------------------------------


I tested this script(called myCopy) using following command.

>./myCopy /home/dfeng 'a b.txt' server1:/local a_b.txt
failed

I debugged it and found "'"$1/"$2""'" can do the right replacement and the value is '/home/dfeng/a b.txt' including the single quotes.  Interestingly, if I manully type the command like following.

>scp '/home/dfeng/a b.txt' server1:/local/a_b.txt

It just works like a champ.
Any ideas???  Your input and help will be greatly appreciated.

Thanks,
Dennis
Comment
Watch Question

Author

Commented:
Sorry, I cut and paste that script wrongly.  Somehow, it overrided itself.  Here is the correct one.

# ---------------------------------------------
#!/bin/ksh

#Make the file copy using scp
#
# Arg 1: source directory including server name, i.e. eportdev01:/local/dev
#        if no server name is specified, it means using current server
# Arg 2: orginal file name
# Arg 3: destination directory including server name
# Arg 4: destination file name

if ( scp "'"$1/"$2""'" $3/$4 > /dev/null 2>&1 )
then
       echo "success"
else
       echo "failed"
fi
# -----------------------------------------------

CERTIFIED EXPERT

Commented:
Hi feng ,

  The problem is the folowing statement:

  if ( scp "'"$1/"$2""'" $3/$4 > /dev/null 2>&1 )

  When you run the scp command, the remote machine require the usr who runs the script to supply his password on the remote system.

   if you have to use secure copy client, you should not redirect all the screen message to /dev/null, and allow the user to supply their password to the remote machine. That's why the scripte failed.

   The following script will work for you:

    # ---------------------------------------------
   #!/bin/ksh

   #Make the file copy using scp
   #
   # Arg 1: source directory including server name, i.e. eportdev01:/local/dev
   #        if no server name is specified, it means using current server
   # Arg 2: orginal file name
   # Arg 3: destination directory including server name
   # Arg 4: destination file name

#   if ( scp "'"$1/"$2""'" $3/$4 > /dev/null 2>&1 )
    scp ${1}/${2} ${3}/${4}

   if [[ "$?" -ne 0 ]] ; then
         echo "failed"
   else
         echo "success"
   fi
   # -----------------------------------------------

   

Author

Commented:
yuzh: thanks for your answer.  Sorry for not replying for long time.  Too busy to take time writing something and explain what needed.  Basically the password stuff is not the problem.  We set up the system to not prompt password checking for certain user.  The major problem is how to copy files whose name contains "space" or some other special symbols like "&;|()", etc.

If I have a file called "a b.txt", and I can successfully copy it manually using following command.

>cp ./'a b.txt' ./ab.txt

however, if I use a script to do this, for example the script called "myCopyScript".  Even I intentionally create single quota around file name in the script, it seems the command will drop it just like I didn't put them.
the script is like this.

#!/bin/ksh

cp "'"${1}/${2}"'" ${3}/${4} > /dev/null 2>&1

if [[ "$?" -ne 0 ]]
then
        echo "failed"
else
        echo "success"
fi

and I call the script like this.

>myCopyScript . 'a b.txt' . ab.txt

it will print out "failed" and actually didn't copy the file.  Just don't know why.  Any thoughts?

Dennis

Commented:
Same comment as in question 20249411, use this syntax:

scp "${1}/${2}" ${3}/${4}
CERTIFIED EXPERT

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation for this question in the Cleanup topic area as follows:
- PAQ & remove points

Please leave any comments here within the next 7 days

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

tfewster (I don't work here, I'm just an Expert :-)

per recommendation

SpideyMod
Community Support Moderator @Experts Exchange

Explore More ContentExplore courses, solutions, and other research materials related to this topic.