Sample Header Ad - 728x90

Check if user exists in /etc/passwd, if they exist, create new one with prefix

0 votes
1 answer
3148 views
Kinda stuck here, did not find any solution. Check if user exists in /etc/passwd, if they exist, create new one with prefix new_the_existed_one
#!/bin/bash

myuser="/home/yakyak/Desktop/Exercises/newusers.txt"

sed '/^[ \t]*$/d' $myuser | while read -r line
do
        name="$line"
        # adduser --disabled-login --gecos "" $name
        # check if user exist
        isthere=$(cut -d: -f1 /etc/passwd | grep "$name")

        # if user was added then make insert record into log file
        if [[ "$isthere" == "$name" ]]
        then
            echo "User already exist, creating new user.."
            adduser --disabled-login 
            # adduser --disabled-login --gecos " " $name
        fi   
done
Asked by YakyAK (23 rep)
Jan 27, 2022, 01:13 AM
Last activity: Jul 3, 2025, 03:27 PM