在命令行上将用户和组传递给 shell 脚本

在命令行上将用户和组传递给 shell 脚本

我需要运行一个只能由特定用户和组运行的脚本。命令的格式如下

$PROGRAM_HOME/bin/installService.sh [USER [GROUP]]

我尝试运行脚本

$PROGRAM_HOME/bin/installService.sh root root

但我收到以下错误

** ERROR: Only root user can install artifactory as a service

当我使用 sudo 运行它时,我收到另一个错误

** ERROR: Could not change values in /etc/init.d/artifactory

这是确切的错误

** INFO: Please edit the files in /etc/opt/jfrog/artifactory to set the correct environment
Especially /etc/opt/jfrog/artifactory/default that defines ARTIFACTORY_HOME, JAVA_HOME and JAVA_OPTIONS
cp: /etc/init.d/artifactory: No such file or directory
chmod: /etc/init.d/artifactory: No such file or directory
sed: illegal option -- -
usage: sed script [-Ealn] [-i extension] [file ...]
       sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]

** ERROR: Could not change values in /etc/init.d/artifactory


Whats the correct syntax to run it as a root?

我调试时遵循的一些步骤

我使用的是 4.11.1 版本,这是我特别感兴趣的版本

我正在按照文档中的步骤进行操作https://www.jfrog.com/confluence/display/RTF/Installing+on+Linux+Solaris+or+Mac+OS

我使用的是 Mac OSX EI Capitan 10.11.5

出现错误后,我确实修复了 /opt/jfrog/artifactory/default 中的 ARTIFACTORY_HOME 位置

我以 root 身份使用 sudo -s 运行安装脚本,但出现完全相同的错误。

我还尝试了另一个版本,看看这个版本的脚本是否存在问题,但得到了同样的错误

我猜测这部分脚本失败了

installService() {
    serviceName=$(basename $artServiceFile)
    serviceFiles=$artBinDir/../misc/service
    if [ -e "$artServiceFile" ]; then
        cp -f $artServiceFile $serviceFiles/$serviceName.init.backup
    fi
    cp -f $serviceFiles/artifactory $artServiceFile
    chmod a+x $artServiceFile

在调试模式下运行脚本的输出

root# bash -x ./installService.sh
+ checkRoot
+ curUser=
+ '[' -x /usr/xpg4/bin/id ']'
++ id -nu
+ curUser=root
+ '[' root '!=' root ']'
+ '[' ./installService.sh = . ']'
+ '[' ./installService.sh = source ']'
+++ dirname ./installService.sh
++ cd .
++ pwd
+ artBinDir=/opt/artifactory-pro-4.11.1/bin
+++ dirname /opt/artifactory-pro-4.11.1/bin
++ cd /opt/artifactory-pro-4.11.1
++ pwd
+ artExtractDir=/opt/artifactory-pro-4.11.1
+ ARTIFACTORY_HOME=/opt/artifactory-pro-4.11.1
+ '[' -n '' ']'
+ artEtcDir=/etc/opt/jfrog/artifactory
+ TOMCAT_HOME=/opt/artifactory-pro-4.11.1/tomcat
+ artLogDir=/opt/artifactory-pro-4.11.1/logs
+ artRunDir=/opt/artifactory-pro-4.11.1/run
+ '[' -n '' ']'
+ artServiceFile=/etc/init.d/artifactory
+ artDefaultFile=/etc/opt/jfrog/artifactory/default
+ getArtUser ''
+ '[' -n '' ']'
+ '[' -z '' ']'
+ ARTIFACTORY_USER=artifactory
+ getArtGroup ''
+ '[' -n '' ']'
+ echo

+ echo 'Installing artifactory as a Unix service that will run as user artifactory'
Installing artifactory as a Unix service that will run as user artifactory
+ echo 'Installing artifactory with home /opt/artifactory-pro-4.11.1'
Installing artifactory with home /opt/artifactory-pro-4.11.1
+ createArtUser
+ echo -n 'Creating user artifactory...'
Creating user artifactory...++ getent passwd artifactory
++ awk -F: '{print $1}'
./installService.sh: line 40: getent: command not found
+ artifactoryUsername=
+ '[' '' = artifactory ']'
+ echo -n creating...
creating...++ which bash
+ useradd -m -s /usr/local/bin/bash artifactory
./installService.sh: line 45: useradd: command not found
+ '[' '!' 127 ']'
+ echo ' DONE'
 DONE
+ createArtGroup
+ '[' '' == '' ']'
+ return 0
+ createArtEtc
+ echo

+ echo -n 'Checking configuration link and files in /etc/opt/jfrog/artifactory...'
Checking configuration link and files in /etc/opt/jfrog/artifactory...+ '[' -L /opt/artifactory-pro-4.11.1/etc ']'
+ echo -n 'already exists, no change...'
already exists, no change...+ echo -e ' DONE'
 DONE
+ createArtDefault
+ echo -n 'Creating environment file /etc/opt/jfrog/artifactory/default...'
Creating environment file /etc/opt/jfrog/artifactory/default...+ '[' -e /etc/opt/jfrog/artifactory/default ']'
+ echo -n 'already exists, no change...'
already exists, no change...+ echo -e ' DONE'
 DONE
+ echo -e '\033[33m** INFO: Please edit the files in /etc/opt/jfrog/artifactory to set the correct environment\033[0m'
** INFO: Please edit the files in /etc/opt/jfrog/artifactory to set the correct environment
+ echo -e '\033[33mEspecially /etc/opt/jfrog/artifactory/default that defines ARTIFACTORY_HOME, JAVA_HOME and JAVA_OPTIONS\033[0m'
Especially /etc/opt/jfrog/artifactory/default that defines ARTIFACTORY_HOME, JAVA_HOME and JAVA_OPTIONS
+ createArtRun
+ '[' '!' -d /opt/artifactory-pro-4.11.1/run ']'
+ installService
++ basename /etc/init.d/artifactory
+ serviceName=artifactory
+ serviceFiles=/opt/artifactory-pro-4.11.1/bin/../misc/service
+ '[' -e /etc/init.d/artifactory ']'
+ cp -f /opt/artifactory-pro-4.11.1/bin/../misc/service/artifactory /etc/init.d/artifactory
cp: /etc/init.d/artifactory: No such file or directory
+ chmod a+x /etc/init.d/artifactory
chmod: /etc/init.d/artifactory: No such file or directory
+ sed --in-place -e '
     /processname:/ s%artifactory%artifactory%g;
     /Provides:/ s%artifactory%artifactory%g;
     s%# pidfile: .*%# pidfile: /opt/artifactory-pro-4.11.1/run/artifactory.pid%g;
     s%/etc/opt/jfrog/artifactory/default%/etc/opt/jfrog/artifactory/default%g;
     ' /etc/init.d/artifactory
sed: illegal option -- -
usage: sed script [-Ealn] [-i extension] [file ...]
       sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]
+ errorArtHome 'Could not change values in /etc/init.d/artifactory'
+ echo

+ echo -e '\033[31m** ERROR: Could not change values in /etc/init.d/artifactory\033[0m'
** ERROR: Could not change values in /etc/init.d/artifactory
+ echo

+ exit 1

答案1

不幸的是,这些事情有时会发生。也许脚本太旧了,或者是为不同的操作系统编写的,或者你缺少它所需的软件包,或者它很糟糕。有办法解决它。

你是从 shell 脚本的角度来问这个问题的,所以我就这么回答吧。此外,我不知道你的操作系统是什么(或版本),你从哪里得到这个软件(和它的版本),你是如何走到这一步的,以及你到底想做什么。

您是否遵循了说明关于/etc/opt/jfrog/artifactory像它警告的那样定义设置?

你使用的是最新版本这个软件/脚本?

您的脚本正在尝试修改/访问(或创建?)一个初始化脚本失败时:

/etc/init.d/artifactory

调试/修复问题

如果不知道脚本执行的所有步骤,就很难知道为什么会收到这些消息。

首先,获得一个 root shell,只是为了好玩。sudo -ssudo su -或者su -应该可以让你到达那里。

那么 -/etc/init.d存在吗?/etc/init.d/artifactory存在吗?

您可以打开脚本并通读它,寻找问题。或者您可以按如下方式运行它并逐步调试输出:

bash -x <script> <args>

事实上,有时只需运行脚本bash就可以提供帮助,因为一些 shell 脚本包含#!/bin/sh在顶部,dash至少在 Ubuntu 系统上可以调用。Dash 可能会导致一些 bash 脚本失败。

bash <script> <args>

当您发现问题(与脚本或系统有关)时,请修复它并重新运行脚本。如果事情没有解决,请返回并使用有用的信息更新您的问题。

相关内容