File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

cp command in linux

 
Rahul Dhaware
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
batchId2="cp '24 - 28 Nov_Task_5194_1.txt' /home/backup/"
I want to use now $batchId to execute cp command.
when i used cp on files which contains space in thire name , i enclosed file name with ''.

but when i used $batchId2 in shell script it does not wrk.

Can anyone please help me?
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you give us more to go on... like the segment of shell script that isn't working and how you're setting the variable originally?
 
Rahul Dhaware
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following is my shell script.


#!/bin/sh

fileNames="'abc 345 temp.pdf' 'pqs_234.pdf'"

destinationPath="/home/temp/"

cd /home/myfolder/

cp $fileNames $destinationPath


but if i execute cp command at console like
cp 'abc 345 temp.pdf' 'pqs_234.pdf' /home/temp/

then it wrk.

so why it's not wrk when i execute it from shell script?
Please help me.
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried executing this on the shell? Like this:If you have, you'll see that Bash (I assume that's the shell you're using) expands the spaces into separate filenames, treating the quotes as part of the words. So the cp command is actually seeing these files:
  • 'abc
  • 345
  • temp.pdf'
  • 'pqs_234.pdf'
  • which isn't what you want.

    There are a couple of changes you can make. The first is to use the xargs command, like this:That should work.

    Alternatively, you can change Bash's token identification delimiter. So rather than using quote marks and Bash interpreting the spaces as delimiters, you can separate filenames with any other delimiter character not likely to appear in each name. Common examples are a newline or a colon, or any other character you know won't be found in the filenames. I'll go with the latter:Note you will need to put IFS back to the original value after running the command as that change will likely break other parts of your script.

    Post back if that works or doesn't work for you...
    [ December 10, 2008: Message edited by: Charles Lyons ]
     
    Rahul Dhaware
    Greenhorn
    Posts: 22
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    yes Charles, it works.

    Thank you very much.

    I have solved my critical problem.
    Thanks once again
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic