So, I need to write bash file that will
1) get name of the user you want to switch to
2) check if there is such user
3) check length of entered name of user
4) and change user
My script looks like this
read $user_name
if ( ! [[ $(cat /etc/passwd | grep $user_name) ]] ) then echo "error. There is no such user" exit 1 fi
if (( $(expr length $user_name) >= 30 ))
then echo "error" exit 1 fi
sudo su $user_name
exit 0
But if I enter existing user it shows error message
Where is my mistake?
you didn't provide the specific like OS, preferred shell etc...
just guessing - YMMV:
#!/bin/ksh
read $user_name
if [ $(grep $user_name /etc/password) ]; then
:
else
echo "error. There is no such user"
exit 1
fi
if [ ${#user_name} -ge 30 ]; then
echo "error"
exit 1
fi
sudo su $user_name
exit 0
read user_name # no "$", no expansion
if grep $user_name /etc/passwd; then ... # no "[" (test) command, no "command substitution" - use grep's exit code immediately