TransWikia.com

How do I stop a script from finishing if certain text is printed?

Super User Asked by Vlad Gogonea on December 9, 2021

I am trying to automate domain join but want to make it so you cannot run some commands twice. I am trying to stop the script if the domain is already joined. Printed text if domain is already joined: realm: Already joined to this domain

#!/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 : ALLn+ : root : ALLn- : 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

One Answer

You need something like this:

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

Answered by fratester on December 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP