如果打印了某些文本,我该如何阻止脚本完成?

如果打印了某些文本,我该如何阻止脚本完成?

我正在尝试自动加入域,但希望这样做,以便您无法运行某些命令两次。如果域已加入,我尝试停止脚本。如果域已加入,则打印文本:领域:已加入此域

#!/bin/bash
yum install realmd oddjob oddjob-mkhomedir sssd samba-common-tools -y

# Type the domain you want to join. You will be asked for the user password.
read -p "Enter the domain you want to join : " domain_name
realm join -U admin $domain_name

<==> STOP HERE IF PRINTED TEXT IS= realm: Already joined to this domain

sed -i "111i %Linux-Admins@domain   ALL=(ALL)       NOPASSWD: ALL" /etc/sudoers
sed -i '$ a\+ : domain\\Linux-Admins : ALL\n+ : root : ALL\n- : ALL : ALL' /etc/security/access.conf
sed -i '/account     required      pam_unix.so/s/$/ broken_shadow/' /etc/pam.d/system-auth-ac
sed -i '/account     required      pam_permit.so/a account     required      pam_access.so' /etc/pam.d/system-auth-ac
sed -i '/account     required      pam_unix.so/s/$/ broken_shadow/' /etc/pam.d/password-auth-ac
sed -i '/account     required      pam_permit.so/a account     required      pam_access.so' /etc/pam.d/password-auth-ac
sed -i -e '/GSSAPIAuthentication/s/yes/no/' /etc/ssh/sshd_config
systemctl stop realmd.service oddjobd.service sssd.service sshd.service
systemctl start realmd.service oddjobd.service sssd.service sshd.service
systemctl status realmd.service oddjobd.service sssd.service sshd.service

答案1

你需要这样的东西:

res=`realm join -U admin $domain_name 2>&1`
if [[ $res == *"realm: Already joined to this domain"* ]]; then
        echo done
        exit 0
fi

相关内容