我有這個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..."
主叫用戶core
爲500
和root
爲0
不工作。我有以下錯誤:
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..."
我不明白爲什麼你堅持調用'sudo'執行命令*爲當前用戶*。只需直接運行命令,只使用'sudo'來模擬'root'。 –
在沒有使用sudo的情況下運行腳本並將docker-compose的sudo命令放在CoreOS下不適用於我。我有'Permission denied'錯誤。但我不想以超級用戶的身份運行每一行,所以這就是爲什麼我會調用普通用戶的大部分行。 '/ opt/bin'目錄(我複製docker-compose)只能由超級用戶使用。 – Lanti
'sudo echo> output_file'不正確。 'echo'以超級用戶身份運行,但重定向到文件由當前用戶完成。你應該使用'echo |代替sudo tee output_file>/dev/null'。 –