Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have this script:

exportFile(){
    file=$1
    skippedBytes=$2
    exportCommandString=$3

    #This works:
    #dd if=$file bs=1 skip=$skippedBytes | ssh sshConnection 'cat - >> /remote/dir/myTarget.txt.output' > export.output 2>&1

    #This does not work:
    dd if=$file bs=1 skip=$skippedBytes | $($exportCommandString $file) > export.output 2>&1

    cat export.output
}

sshExportCommand(){
    userAtServer=$1
    targetDir=$2
    filename=$3
    echo 'ssh '$userAtServer' '\''cat - >> '$targetDir'/'$filename'.output'\'
    #Same result with this:
    #echo "ssh $userAtServer 'cat - >> $targetDir/$filename.output'" 
}

exportFile mySource.txt 9 "sshExportCommand sshConnection /remote/dir myTarget.txt"

When invoking it, I obtain the following output:

85+0 records in
85+0 records out
85 bytes (85 B) copied, 0.00080048 s, 106 kB/s
Read bytes: 85
ovh_ssh: cat - >> /remote/dir/myTarget.txt.output: No such file or directory

What does prevent this from working ? Is this command substitution ? Anything else I missed ?

share|improve this question
    
Can you maybe strip your script down to the bare essentials for what your question is? It will greatly increase the chance for a useful answer –  Bernhard Mar 10 '13 at 19:43
    
Done, hope it is enough. Thanks –  lauhub Mar 10 '13 at 20:57

1 Answer 1

up vote 2 down vote accepted

As you written in the comment code line, the following is working:

byteCount=$( exec 3>&1 ; dd if=$file bs=1 skip=$skippedBytes | tee -a >(wc -c >&3) -a $file.output | ssh sshConnection 'cat - >> /remote/dir/myTarget.txt.output' > export.output 2>&1 ; 3>&1 )

So adapt this knowledge and use this refactoring:

exportFile(){
   file=$1
   skippedBytes=$2
   userAtServer=$3
   targetDir=$4
   targetFile=$5
   rm export.output

   byteCount=$( exec 3>&1 ; dd if=$file bs=1 skip=$skippedBytes | tee -a >(wc -c >&3) -a $file.output | ssh ${userAtServer} "cat - >>$targetDir/${targetFile}" > export.output 2>&1 ; 3>&1 )

   echo "Read bytes: $byteCount"
   cat export.output
 }
 exportFile mySource.txt 9 sshConnection /remote/dir myTarget.txt
share|improve this answer
    
It would work, but I need export command string to be configurable and multiple. Something like this: exportFile mySource.txt 9 "sshExportCommand sshConnection /remote/dir myTarget.txt" "sshExportCommand otherSshConnection /otherRemote/dir myTarget2.txt" –  lauhub Mar 10 '13 at 21:09
    
@lauhub Sorry - that use case is not described in your question. I'm sure that you not really need it this way. Change your point of view. Duplicate transmissions can be initiated by duplicate exportFile calls. If you realy need it with one function, you may use a wrapper function dividing the arguments to a sequence of exportFile invocations. –  H.-Dirk Schmitt Mar 10 '13 at 21:17
    
Ok, thanks, I am gonna try this. Actually I found some workaround but was curious why this way did not work –  lauhub Mar 10 '13 at 21:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.