0

Hello i have got problem, on shell program(.sh), with this code I should copy the file JPG from the directory sorgente to destinazione but the code does not work, can you help me!!

#!/bin/bash

sorgente=$1
destinazione=$2


cd sorgente

for i in *.jpg
do

  #controllo che la directory sia leggibile
   if test -r $i 
   then

     #controllo che il file $i non sia presente nella destinazione
       if test -r $2 
         then 
            #in questo caso è presente nella destinazione
          mv $i "$destinazione/duplicati"  

         else
            mv $i "$destinazione"
       fi

  else
       echo "il file $i non è leggibile da questo utente"

   fi

done
3
  • 3
    What is the error you're getting or what about it is "not working". Also, why are you testing if you have read privs on the destination directory, they shouldn't matter for anything you're doing to it. Also, you should quote your variables to prevent word splitting, and finally you're doing cd sorgente without $ and you probably want cd "$sorgente" Commented Sep 4, 2016 at 12:49
  • 1
    test -r $2 tests if the directory is readable, that's not the same as testing if it already contains a file named $i. Commented Sep 4, 2016 at 12:49
  • 1
    All other things aside, GNU mv has a --backup switch that can generate a foo.jpg.1 in the target directory if foo.jpg already exists there. Commented Sep 4, 2016 at 12:51

1 Answer 1

0

I have solve with this code:

sorgente=$1
destinazione=$2

echo "\n Sorgente:  $sorgente \n" 
echo "\n Destinazione:  $destinazione \n"


cd "$sorgente"

for i in *.JPG
do

  #controllo che la directory sia leggibile
   if test -r "$i" 
   then

     #controllo che il file $i non sia presente nella destinazione
       if test -r "$2" 
         then 
            #in questo caso è presente nella destinazione
            # mv "$i" "$destinazione/duplicati"  

           # else
            mv "$i" "$destinazione"
       fi

  else
       echo "il file $i non è leggibile da questo utente"

   fi

done

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.