0

when cp -r jdk1.8.0_331 /usr/java in command line, it results in such a folder:

# ls /usr/java/jdk1.8.0_331/
bin  COPYRIGHT  include  javafx-src.zip  jmc.txt  jre  legal  lib  LICENSE  man  README.html  release  src.zip  THIRDPARTYLICENSEREADME-JAVAFX.txt  THIRDPARTYLICENSEREADME.txt

However, once I put it in a shell script and run in remote host, ssh root@app-1 < java_install.sh, it results in such a folder:

# ls /usr/java/
bin  COPYRIGHT  include  javafx-src.zip  jmc.txt  jre  legal  lib  LICENSE  man  README.html  release  src.zip  THIRDPARTYLICENSEREADME-JAVAFX.txt  THIRDPARTYLICENSEREADME.txt

One layer off. Why? And how can I copy a folder into another folder as a child folder in remote shell script?

This is the whole script:

#!/bin/bash
tar -zxvf jdk-8u331-linux-x64.tar.gz
cp -r jdk1.8.0_331 /usr/java

echo '
export JAVA_HOME=/usr/java/jdk1.8.0_331
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib:$CLASSPATH
export JAVA_PATH=${JAVA_HOME}/bin:${JRE_HOME}/bin
export PATH=$PATH:${JAVA_PATH}
' >> /etc/profile

source /etc/profile
java -version

And I have just found that, when running script twice, the second time, it moves jdk1.8.0_331 to /usr/java/jdk1.8.0_331.

1 Answer 1

0

Make sure the parent directory /usr/java exists before copying into it.

2
  • it exists. What I don't understand is why it results in one layer off (file path folder part) when running script first time.
    – Tiina
    Commented Jul 1, 2022 at 9:39
  • (1) When you execute cp -r jdk1.8.0_331 /usr/java , what should Linux do in case "/usr/java" is a normal text file ? (2) What should happen when "/usr/java" is Empty Directory ? (3) In case Directory "/usr/java" is not Empty, what should happen ? (4) Lastly , what should occur when "/usr/java" is not existing ? (5) When you think about these 4 cases & realize the Issue , you will know why it is 1-layer off & why you should ensure "/usr/java" exists before copying, @Tiina
    – Prem
    Commented Jul 1, 2022 at 11:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .