0

im trying to create a script that check if user already registered before than add number after username for example username1 and will keep looping after i got unique username.

here what i try

username="tes2"
password="tes2"
exists=$(grep -c "^$username:" /etc/passwd)
case=$exists
i=1

while true
do
    case "$case" in
        *)
            echo 'generate new user'
            i=$(( $i + 1 ))
            username="$username""$i"
            exists=$(grep -c "^$username:" /etc/passwd)
            case=$exists;;
        0) 
            pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
            useradd -m -p "$pass" "$username"
            echo 'success add $username' 
            break;;
    esac
done

echo "Quit"

when i run this script, it keep generate new user.

1
  • 6
    Try shellcheck, it automatically finds problems in scripts like "Line 10: This pattern always overrides a later one on line 16." Commented Jun 24, 2020 at 18:10

2 Answers 2

5

https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_03.html

The commands in the COMMAND-LIST for the first match are executed.

https://www.gnu.org/software/bash/manual/bash.html#Conditional-Constructs

case will selectively execute the command-list corresponding to the first pattern that matches word.

* always matches so it's always executed. If you want the 0 case to execute when it matches, put it before the * case.

Sign up to request clarification or add additional context in comments.

1 Comment

0
#!/bin/bash


check_passwd() {
    if [ ! -f ./passwd_copy.txt ]; then
    echo "brak pliku passwd_copy.txt, aby utworzyc uruchom skrypt z poleceniem -INIT";
    exit 1
    fi
}

case $1 in
    "-AUTHOR")
        echo "Autor skryptu: $autor";
        echo "Grupa szkoleniowa $grupa";
        echo "Nazwa skryptu: $0";
    ;;
    "-INIT")
        if [ $(cp /etc/passwd ./passwd_copy.txt) ] ; then
        echo "kopia nie ok";
        else
        echo "kopia ok";
        fi
    ;;
    "-FIND")
        check_passwd
        if [ -z "$2" ]; then
        echo "brak ciagu do wyszukania"
        exit 1
        fi
        grep "$2" passwd_copy.txt > wynik_$(date +%d-%m-%Y).txt 
    ;;
    "-COLUMNS")
        check_passwd
        if [ -z "$2" ]; then
        echo "Brak numeru kolumny"
        exit 1
        fi
        awk -v col="$2" -F: '{print $col}' passwd_copy.txt 1>$2
    ;;
    "-RIGHTS")
        check_passwd
        if [ -z "$2" ] || [ -z "$3" ]; then
        echo "brak katalogu lub praw"
        exit 1
        fi
        for file in "$2"/*; do
        if [ -f "$file" ]; then
        chmod "$3" "$file"
        echo "zmieniono prawa dla pliku $file"
        fi
        done
    ;;
    *)
        echo "Możliwe do wpisania parametry to -AUTHOR -INIT -FIND -COLUMNS -RIGHTS";
    esac

1 Comment

Thank you for posting this answer. While this code may answer the question, might you please edit your post to add an explanation as to why/how it works? This can help future readers learn and apply your answer. You are also more likely to get positive feedback (upvotes) when you include an explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.