• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

cp command in linux

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    yes Charles, it works.

    Thank you very much.

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