The main issue in your code is that $? is expanded before ssh is called. This is due to quoting. All expansions in a double-quoted string are expanded before the string is used. In addition to that, the double-quoted string that you are using with ssh contains other double-quoted sections. These sections would be unquoted, just like the substring abc is unquoted in "123"abc"456".
Instead of trying to execute a complicated command on the remote host, just let the ssh command cat the passwd file, then grep that:
if ssh -n "sandeep@$ipaddress" cat /etc/passwd | grep -q -F -e "$userid"
then
echo "User exists"
else
echo "User does not exist"
fi >>"/tmp/userfind_$DATE.txt"
Also, consider reading from the user and server list using a while loop instead:
while IFS= read -r userid; do
# ...
done </home/sandeep/Project_finduser01/userslist
You may also redirect the outermost loop to your output file instead of redirecting every single echo:
while ...; do
while ...; do
# stuff
done <userlist
done <serverlist >"/tmp/userfind_$DATE.txt"
If your user list is long, you may want to only get the passwd from the remote host once, and then query that several times
while ...; do
scp "sandeep@$ipaddress:/etc/passwd" passwd.tmp
while ...; do
if grep -q -F -e "$userid" passwd.tmp; then
# exists
fi
done <userlist
done <serverlist >"/tmp/userfind_$DATE.txt"
Even more efficiently would be to read the user list into an awk array and then match the usernames from the passwd file against them. That would get rid of the innermost loop entirely.
The username is found in a particular field in the passwd file. With your approach, you would match both marc and marco if you searched for marc. To match a bit more carefully, consider using a pattern such as "^$userid:" instead of matching against the whole line (and drop the -F that I introduced above if you're still using grep to do this).
You may also avoid the parsing of the passwd file completely with
getent passwd "$userid" >/dev/null
This returns a zero exit code (success) if the user exists and non-zero otherwise.
I.e.,
if ssh -n "sandeep@$ipaddress" getent passwd "$userid" >/dev/null
then
# exists
else
# does not exist
fi
This would do one ssh call against the remote host per user though. This could be made a bit more efficient by not closing the connection between each call (the below would keep the connection open for one minute):
if ssh -n -o ControlMaster=auto -o ControlPersist=1m "sandeep@$ipaddress" getent passwd "$userid" >/dev/null
then
# exists
else
# does not exist
fi
"$?"and the other things that you quote within the command you execute withssh).cat /home/sandeepj/Project_finduser01/userslist' + userid=raj.singh + echo -e '\n' + echo -n raj.singh + ssh -t [email protected] 'grep raj.singh /etc/passwd > /dev/null; if [ 0 = 0 ]; then echo -n ' : User 'exsits > /tmp/status else echo -n ' : User not 'exsits > /tmp/status fi'"on thegrepline, and you have an extra"on the finalfiline. Look at the syntax highlighting (colors) in your question. Please edit and fix it so you show the exact script you are running.