简体   繁体   中英

cp command don't create directory in a sh\bash script, why?

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

您需要先创建目录,然后再复制到目录中,查看命令mkdir

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.

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

First create directory for given path

mkdir /home/test

then copy file from destination to source

cp /home.a.txt /home/test

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM