[arch-general] archlinux-java script in java-common package

山田ハヤオ hayao at fascode.net
Thu Sep 30 10:29:35 UTC 2021


Thank you very much for supporting me. How about this?

===Patch Start ===
--- ./archlinux-java.orig       2021-09-30 19:21:57.758914707 +0900
+++ ./archlinux-java    2021-09-30 19:22:19.588869274 +0900
@@ -8,14 +8,15 @@
 JVM_DIR=/usr/lib/jvm
 DEFAULT_NAME=default
 DEFAULT_PATH=${JVM_DIR}/${DEFAULT_NAME}
-DEFAULT_NAME_JRE=default-runtime
+DEFAULT_NAME_JRE="default-runtime"
 DEFAULT_PATH_JRE=${JVM_DIR}/${DEFAULT_NAME_JRE}


 # Utility functions

 check_root() {
-  if [ $(id -u) -ne 0 ]; then
+  #if [ $(id -u) -ne 0 ]; then
+  if (( UID != 0 )); then
     echo 'This script must be run as root'
     exit 1
   fi
@@ -24,7 +25,7 @@
 # $1: parameter count given to this script for this option
 # $2: expected parameter count for this option
 check_param_count() {
-  if [ $1 -ne $2 ]; then
+  if [[ "${1}" -ne "${2}" ]]; then
     echo 'Wrong parameter count'
     exit 2
   fi
@@ -34,8 +35,8 @@

 get_default_java() {
   path=$(readlink -e ${DEFAULT_PATH})
-  if [ "x${path}" != "x/dev/null" ]; then
-    echo ${path/${JVM_DIR}\/}
+  if [[ "${path}" != "/dev/null" ]]; then
+    echo "${path/${JVM_DIR}\/}"
   else
     echo ""
   fi
@@ -44,34 +45,34 @@
 get_installed_javas() {
   if [ -d ${JVM_DIR} ]; then
     for dir in $(find ${JVM_DIR} -mindepth 1 -maxdepth 1 -type d | sort);
do
-      if [ -x ${dir}/bin/java ]; then
-        javas+=(${dir/${JVM_DIR}\/})
+      if [ -x "${dir}/bin/java" ]; then
+        javas+=("${dir/${JVM_DIR}\/}")
       else
-        if [ -x ${dir}/jre/bin/java ]; then
-        javas+=(${dir/${JVM_DIR}\/}/jre)
+        if [ -x "${dir}/jre/bin/java" ]; then
+        javas+=("${dir/${JVM_DIR}\/}/jre")
         fi
       fi
     done
   fi
-  echo ${javas[@]}
+  echo "${javas[*]}"
 }

 # $1: Java environment name to test
 is_java_valid() {
-  test "x$1" != "x${DEFAULT_NAME}" && test -x ${JVM_DIR}/$1/bin/java
+  test "x$1" != "x${DEFAULT_NAME}" && test -x "${JVM_DIR}/${1}/bin/java"
 }

 # $1: Java environment name to set as default
 set_default_link_to() {
-  new_default=$1
-  unlink ${DEFAULT_PATH} 2>/dev/null
-  ln -sf ${new_default} ${DEFAULT_PATH}
-
-  unlink ${DEFAULT_PATH_JRE} 2>/dev/null
-  if [[ -d ${new_default}/jre ]]; then
-    ln -sf ${new_default}/jre ${DEFAULT_PATH_JRE}
+  new_default="${1}"
+  unlink "${DEFAULT_PATH}" 2>/dev/null
+  ln -sf "${new_default}" "${DEFAULT_PATH}"
+
+  unlink "${DEFAULT_PATH_JRE}" 2>/dev/null
+  if [[ -d "${new_default}/jre" ]]; then
+    ln -sf "${new_default}/jre" "${DEFAULT_PATH_JRE}"
   else
-    ln -sf ${new_default} ${DEFAULT_PATH_JRE}
+    ln -sf "${new_default}" "${DEFAULT_PATH_JRE}"
   fi
 }

@@ -83,23 +84,24 @@
 # First level functions

 do_status() {
-  installed_java=($(get_installed_javas))
+  #installed_java=($(get_installed_javas))
+  IFS=" " read -r -a installed_java < <(get_installed_javas)
   if [ ${#installed_java[@]} -eq 0 ]; then
     echo 'No compatible Java environment installed'
   else
     default_java=$(get_default_java)
     echo 'Available Java environments:'
-    for java in ${installed_java[@]}; do
+    for java in "${installed_java[@]}"; do
       # We discoverd this Java env but its JRE is actually set as default
       if [ "${java}/jre" = "${default_java}" ]; then
         echo -e "  ${java} (${java}/jre default)"
-      elif [ ${java} = "${default_java}" ]; then
+      elif [ "${java}" = "${default_java}" ]; then
         echo -e "  ${java} (default)"
       else
         echo "  ${java}"
       fi
     done
-    if [ -z ${default_java} ]; then
+    if [ -z "${default_java}" ]; then
       echo "No Java environment set as default"
     fi
   fi
@@ -111,14 +113,14 @@

 # $1: Java environment name to set as default
 do_set() {
-  if ! is_java_valid $1; then
+  if ! is_java_valid "${1}"; then
     echo "'${JVM_DIR}/$1' is not a valid Java environment path"
     exit 1
   fi
   default=$(get_default_java)
-  if [ "x$1" != "x${default}" ] || ! is_java_valid ${default}; then
+  if [[ "$1" != "${default}" ]] || ! is_java_valid "${default}"; then
     unset_default_link
-    set_default_link_to $1
+    set_default_link_to "${1}"
   fi

   #parent_dir=$(dirname $1)
@@ -135,11 +137,11 @@

 do_fix() {
   default=$(get_default_java)
-  if is_java_valid ${default}; then
+  if is_java_valid "${default}"; then
     # If its parent is also a valid Java environment
-    if is_java_valid $(dirname ${default}); then
+    if is_java_valid "$(dirname "${default}")"; then
       unset_default_link
-      set_default_link_to $(dirname ${default})
+      set_default_link_to "$(dirname "${default}")"
     fi
   else
     prev=$(readlink ${DEFAULT_PATH})
@@ -150,20 +152,21 @@
     # - first potential fixes of user choices,
     # - then OpenJDK8 as it is considered a default in Arch Linux
     # - finally, any installed environment
-    to_check=(${potential_fixes[@]} ${openjdk8[@]} $(get_installed_javas))
-    for java in ${to_check[@]}; do
-      if ! is_java_valid $(get_default_java) && is_java_valid ${java}; then
-        set_default_link_to ${java}
+    IFS=" " read -r -a to_check < <(get_installed_javas)
+    to_check=("${potential_fixes[@]}" "${openjdk8[@]}" "${to_check[@]}")
+    for java in "${to_check[@]}"; do
+      if ! is_java_valid "$(get_default_java)" && is_java_valid "${java}";
then
+        set_default_link_to "${java}"
       fi
     done
   fi
-  if ! is_java_valid $(get_default_java); then
+  if ! is_java_valid "$(get_default_java)"; then
     echo 'No valid Java environment found'
   fi
 }

 usage() {
-  echo "$(basename $0) <COMMAND>"
+  echo "$(basename "${0}") <COMMAND>"
   echo -e "\nCOMMAND:"
   echo -e '\tstatus\t\tList installed Java environments and enabled one'
   echo -e '\tget\t\tReturn the short name of the Java environment set as
default'
@@ -173,12 +176,12 @@
 }

 ## Main
-case $1 in
+case "${1}" in
   'status') do_status;;
   'get')    do_get;;
-  'set')    check_root; check_param_count $# 2; do_set $2;;
+  'set')    check_root; check_param_count "${#}" 2; do_set "${2}";;
   'unset')  check_root; do_unset;;
   'fix')    check_root; do_fix;;
   'help' | '--help' | '-h' | '') usage;;
-  *)           echo "$(basename $0): unknown option '$@'"; exit 1;;
+  *)           echo "$(basename "${0}"): unknown option '${*}'"; exit 1;;
 esac
===Patch Finish ===

2021年9月30日(木) 17:24 Matej Dujava <mdujava+aur at kocurkovo.cz>:

> On Thu, Sep 30, 2021 at 02:05:41PM +0900, 山田ハヤオ via arch-general wrote:
> >The archlinux-java script in the java-common package has a lot of code
> that
> >is inappropriate as a Bash script. (ShellCheck prints a lot of errors.)
> >I have created a patch that fixes these errors with minimal changes.
> >I put the patch on GitHub Gists because it collapses for some reason when
> I
> >send the patch by email.
> >all.patch is a patch that contains all the changes, and the other files
> >split the changes by function.
> >This is my first time sending a patch to a mailing list, so there may be
> >something wrong with the way it is sent.
> >
> >Patch
> >https://gist.github.com/Hayao0819/bacccc3eebeab73159001f60c9d35a43
>
> Hey,
> if you have problems sending patch in email you can take a look at [1].
> It might be a problem of the email client converting Tabs to spaces or
> removing whitespaces at the start or end of line. You might use
> git-send-patch.
>
> Usualy unified patch is send `patch -U` instead of the default.
>
> [1]
> https://www.kernel.org/doc/html/latest/process/submitting-patches.html#no-mime-no-links-no-compression-no-attachments-just-plain-text
>


More information about the arch-general mailing list