JAVA_HOME не набор в сценарии, когда выполнено с помощью sudo

Вы думали бы, что будет утилита для этого, но я не мог найти его. Однако эта острота Perl должна добиться цели:

perl -pe 's/\e\[?.*?[\@-~]//g'

Пример:

$ command-that-produces-colored-output | perl -pe 's/\e\[?.*?[\@-~]//g' > outfile

Или, если Вы хотите сценарий, можно сохранить как stripcolorcodes:

#! /usr/bin/perl

use strict;
use warnings;

while (<>) {
  s/\e\[?.*?[\@-~]//g; # Strip ANSI escape codes
  print;
}

Если Вы хотите разделить только цветовые коды и оставить какие-либо другие коды ANSI (как перемещение курсора) одними, использовать

s/\e\[[\d;]*m//g;

вместо замены я использовал выше (который удаляет все управляющие коды ANSI).

19
26.06.2019, 17:38
2 ответа

По соображениям безопасности, sudo может очистить переменные среды, который является, почему это, вероятно, не забирает $JAVA_HOME. Загляните Ваш /etc/sudoers файл для env_reset.

От man sudoers:

env_reset   If set, sudo will reset the environment to only contain the following variables: HOME, LOGNAME, PATH, SHELL, TERM, and USER (in addi-
           tion to the SUDO_* variables).  Of these, only TERM is copied unaltered from the old environment.  The other variables are set to
           default values (possibly modified by the value of the set_logname option).  If sudo was compiled with the SECURE_PATH option, its value
           will be used for the PATH environment variable.  Other variables may be preserved with the env_keep option.

env_keep    Environment variables to be preserved in the user's environment when the env_reset option is in effect.  This allows fine-grained con-
           trol over the environment sudo-spawned processes will receive.  The argument may be a double-quoted, space-separated list or a single
           value without double-quotes.  The list can be replaced, added to, deleted from, or disabled by using the =, +=, -=, and ! operators
           respectively.  This list has no default members.

Так, если Вы хотите, чтобы это сохранило JAVA_HOME, добавьте его к env_keep:

Defaults   env_keep += "JAVA_HOME"

С другой стороны, набор JAVA_HOME в корне ~/.bash_profile.

28
27.01.2020, 19:44
  • 1
    Да, это было этим. Понятия не имевший мог сделать это.Спасибо! –  Josh 19.01.2011, 18:23

Выполненные sudo с-E (сохраняют среду) опция (см. файл человека), или помещают JAVA_HOME в install.sh сценарий.

20
27.01.2020, 19:44

Теги

Похожие вопросы