2015-12-17 59 views
-2

我有這個bash腳本。我用下面的命令來運行它:sudo ./install.sh如何在Linux bash腳本中調用用戶/組ID

#!/bin/bash 

MYSQL_PASS="password" 

USER="500" 
SUPERUSER="0" 

sudo -u ${USER} echo -e "Installing docker-compose from GitHub Latest release..." \ 
\ 
&& sudo -u ${SUPERUSER} mkdir -p /opt/bin \ 
&& sudo -u ${SUPERUSER} curl -L https://github.com/docker/compose/releases/download/1.5.2/docker-compose-`uname -s`-`uname -m` > /opt/bin/docker-compose \ 
&& sudo -u ${SUPERUSER} chmod +x /opt/bin/docker-compose \ 
&& sudo -u ${USER} echo -e "docker-compose installed, verifying..." \ 
&& sudo -u ${USER} docker-compose -v \ 
\ 
&& sudo -u ${USER} echo -e "Creating folder structure..." \ 
&& sudo -u ${USER} mkdir -p ~/mysql ~/sqlbackup ~/work/lemp ~/www \ 
\ 
&& sudo -u ${USER} echo -e "Cloning git repo into \"~/work/lemp\"..." \ 
&& sudo -u ${USER} git clone https://github.com/DJviolin/LEMP.git ~/work/lemp \ 
\ 
&& sudo -u ${USER} echo -e "Showing working directory..." \ 
&& sudo -u ${USER} ls -al ~/work/lemp \ 
\ 
&& sudo -u ${USER} echo -e "Starting docker images and containers generation..." \ 
&& sudo -u ${USER} echo -e "\ 
# Set MySQL Root Password\n\ 
MYSQL_ROOT_PASSWORD=${MYSQL_PASS}" > ~/work/lemp/mariadb/mariadb.env \ 
&& sudo -u ${USER} cat ~/work/lemp/mariadb/mariadb.env \ 
&& sudo -u ${USER} docker-compose build ~/work/lemp \ 
\ 
&& sudo -u ${USER} echo -e "LEMP stack has built...\nRun the service with ./service-start.sh command." \ 
&& sudo -u ${USER} echo -e "All done! Exiting..." 

主叫用戶core500root0不工作。我有以下錯誤:

sudo: unknown user: 500 
sudo: unable to initialize policy plugin 

我爲什麼要使用的用戶或組羣號碼,因爲我想讓它多平臺(會是很好,如果它可以500或1000之間進行選擇,因爲我讀的原因一些linux發行版的默認用戶是1000,但在我的情況下是500)。

我們可以檢測到用戶開始運行該腳本sudo?因爲這可能是多用戶Linux上的一種方式。

編輯:

這是無需的超級用戶權限的腳本,可根據用戶的主文件夾製作一個新的路徑變量:

#!/bin/bash 

set -e 

echo -e "Installing docker-compose from GitHub Latest release..." 

mkdir -p ~/bin 
curl -L https://github.com/docker/compose/releases/download/1.5.2/docker-compose-`uname -s`-`uname -m` > ~/bin/docker-compose 
chmod +x ~/bin/docker-compose 
export PATH="~/bin:$PATH" 
echo -e "docker-compose installed, verifying..." 
docker-compose -v 

echo -e "Creating folder structure..." 
mkdir -p ~/mysql ~/sqlbackup ~/work/lemp ~/www 

echo -e "Cloning git repo into \"~/work/lemp\"..." 
git clone https://github.com/DJviolin/LEMP.git ~/work/lemp 

echo -e "Showing working directory..." 
ls -al ~/work/lemp 

echo -e "Starting docker images and containers generation..." 
echo -e "\ 
# Set MySQL Root Password\n\ 
MYSQL_ROOT_PASSWORD=`openssl rand -base64 37 | sed -e 's/^\(.\{37\}\).*/\1/g'`" > ~/work/lemp/mariadb/mariadb.env 
cat ~/work/lemp/mariadb/mariadb.env 

echo -e "LEMP stack has built...\nRun the service with ./service-start.sh command." \ 
echo -e "All done! Exiting..." 
+3

我不明白爲什麼你堅持調用'sudo'執行命令*爲當前用戶*。只需直接運行命令,只使用'sudo'來模擬'root'。 –

+0

在沒有使用sudo的情況下運行腳本並將docker-compose的sudo命令放在CoreOS下不適用於我。我有'Permission denied'錯誤。但我不想以超級用戶的身份運行每一行,所以這就是爲什麼我會調用普通用戶的大部分行。 '/ opt/bin'目錄(我複製docker-compose)只能由超級用戶使用。 – Lanti

+2

'sudo echo> output_file'不正確。 'echo'以超級用戶身份運行,但重定向到文件由當前用戶完成。你應該使用'echo |代替sudo tee output_file>/dev/null'。 –

回答

6

sudoman pages

-u user, --user=user
Run the command as a user other than the default target user (usually root). The user may be either a user name or a numeric user ID (UID) prefixed with the ‘#’ character (e.g. #0 for UID 0). When running commands as a UID, many shells require that the ‘#’ be escaped with a backslash (‘\’). Some security policies may restrict UIDs to those listed in the password database. The sudoers policy allows UIDs that are not in the password database as long as the targetpw option is not set. Other security policies may not support this.

因爲#也用於開始註釋,所以您要麼用反斜槓或引用它來轉義它。例如sudo -u "#${USER}"sudo -u \#${USER}