• 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
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

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
     
    The human mind is a dangerous plaything. This tiny ad is pretty safe:
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
    reply
      Bookmark Topic Watch Topic
    • New Topic