1

I am very new in sh/bash script and I have the following problem:

I have a script that create some directory and copy into it some files but seems not work !!!

In my script I have the following lines of code:

# Creazione Nuovo Pacchetto per Ubuntu
JRE_I386="jre1.6.0_35-i386"
JRE_AMD64="jre1.6.0_35-x86_64"

ApplicationName="XCloud"

# Se il primo parametro è una stringa vuota mostra il messaggio di errore ed esce:
if [ -z $1 ]; then
    echo "Usage: createpkg.sh <rev package>"
    exit

# Altrimenti setta la variabile CURRENT_VERSION con il valore del parametro di input passato:
else
    CURRENT_VERSION=$1      
fi

echo ---------------------------------------------------------------------------------------------------
echo ---------------------------------------------------------------------------------------------------
echo 
echo CREAZIONE NUOVO PACCHETTO ${ApplicationName}${CURRENT_VERSION}
echo 
echo ---------------------------------------------------------------------------------------------------
echo ---------------------------------------------------------------------------------------------------

# Verifica esistenza ambiente prima di generare la nuova versione. Se esiste torna ERRORE:
if [ -e "${ApplicationName}${CURRENT_VERSION}ubuntu1-i386" ]; then
    echo "Errore: la cartella contenente la nuova versione da creare gia' esiste"
    exit
fi
if [ -e "${ApplicationName}${CURRENT_VERSION}ubuntu1-amd64" ]; then
    echo "Errore: la cartella contenente la nuova versione da creare gia' esiste"
    exit
fi

# Copia dell' alberatura completa (recursive and force), copia PACKAGE nella destinazione:
cp -Rf PACKAGE ${ApplicationName}${CURRENT_VERSION}ubuntu1-i386
cp -Rf PACKAGE ${ApplicationName}${CURRENT_VERSION}ubuntu1-amd64
echo "Copia dell' alberatura completata"

# Copia della JRE corretta:
cp -Rf ../JRE/${JRE_I386} ${ApplicationName}${CURRENT_VERSION}ubuntu1-i386/usr/share/${ApplicationName}/jre1.6.0_35
cp -Rf ../JRE/${JRE_AMD64} ${ApplicationName}${CURRENT_VERSION}ubuntu1-amd64/usr/share/${ApplicationName}/jre1.6.0_35
echo "Copia della JRE corretta completata"
......................................................................................
......................................................................................
SOME OTHER STUFF
......................................................................................
......................................................................................

The problem occurs with the last 2 cp command because when it try to execute these lines:

cp -Rf ../JRE/${JRE_I386} ${ApplicationName}${CURRENT_VERSION}ubuntu1-i386/usr/share/${ApplicationName}/jre1.6.0_35
cp -Rf ../JRE/${JRE_AMD64} ${ApplicationName}${CURRENT_VERSION}ubuntu1-amd64/usr/share/${ApplicationName}/jre1.6.0_35

when I go to execute the script, in the shell, it say to me something that in english sound like: impossible to create directory "DIRECTORYNAME": File or directory does not exist

Infact my output in the shell is the following one:

cp: impossibile creare la directory "XCloud1ubuntu1-i386/usr/share/XCloud/jre1.6.0_35": File o directory non esistente
cp: impossibile creare la directory "XCloud1ubuntu1-amd64/usr/share/XCloud/jre1.6.0_35": File o directory non esistente

What is the problem? What am I doing wrong?

Tnx

Andrea

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • Did you try to make exactly the same `cp` in the terminal step-by-step to reconstruct the issue? Maybe there are typos for the path or you don't have the correct rights?! – EverythingRightPlace Oct 22 '13 at 15:35
  • Look at here: http://stackoverflow.com/questions/1529946/linux-copy-and-create-destination-dir-if-it-does-not-exist – Refael Oct 23 '13 at 10:54

4 Answers4

1

You need to create directories before copying into them , look at command mkdir

izissise
  • 898
  • 1
  • 7
  • 23
0

Try adding

mkdir -p ${ApplicationName}${CURRENT_VERSION}ubuntu1-i386/usr/share/${ApplicationName}/jre1.6.0_35

and

mkdir -p ${ApplicationName}${CURRENT_VERSION}ubuntu1-amd64/usr/share/${ApplicationName}/jre1.6.0_35

just before the two cp lines.

damienfrancois
  • 52,978
  • 9
  • 96
  • 110
  • mmm in this way seems don't create the problem but now I have another doubt, why with the 2 previous cp command don't create the same error also id I don't have these directorys? I am refering to this 2 command: cp -Rf PACKAGE ${ApplicationName}${CURRENT_VERSION}ubuntu1-i386 cp -Rf PACKAGE ${ApplicationName}${CURRENT_VERSION}ubuntu1-amd64 echo "Copia dell' alberatura completata" – AndreaNobili Oct 22 '13 at 15:44
  • Add the `-v` flag (verbose) to the first cp to check what is happening. Maybe the structured that is copied is not what you think, or maybe a typo remains somewhere. `cp -Rfv source destination` – damienfrancois Oct 22 '13 at 15:51
0

Look at here, how to copy files and create directory if it does not exist:

Linux: copy and create destination dir if it does not exist

You can check if the directort is exist like this:

if [ -d ~/directorypath ]; then
   echo "exist!"
else
   mkdir ~/directorypath
fi

you can do it in one code line:

if [ ! -d ~/directorypath ]; then mkdir ~/directorypath; fi
Community
  • 1
  • 1
Refael
  • 6,753
  • 9
  • 35
  • 54
0

First create directory for given path

mkdir /home/test

then copy file from destination to source

cp /home.a.txt /home/test
Ashish Singhal
  • 405
  • 5
  • 21