Reputation: 1424
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
.
Upvotes: 0
Views: 208
Reputation: 1
#!/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
Upvotes: 0
Reputation: 57997
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.
Upvotes: 5