if echo 0, script send email

November 8, 2010 at 17:40:50
Specs: unix shell
For some reason i cannot get my script to work
#!/bin/sh
who | grep abcd111
if [$# == 0]
then
echo "Send email" | mail 123@mail.com
crontab -r
else
echo "hes not logged in" | mail 123@mail.com
fi
Im just learning Unix shell, so im abit of a newb, any help would be greatly appreciated

See More: if echo 0, script send email

Report •

#1
November 8, 2010 at 22:43:18
$# is the number of command line arguments. I think you want the exit code. $?.

#!/bin/sh
who | grep abcd111
if [$? == 0]
then
   echo "Send email" | mail 123@mail.com
   crontab -r
else
   echo "hes not logged in" | mail 123@mail.com
fi

Your algorithm has some limitations:

First, the crontab -r command is going to delete the crontab of the user who runs the script.

Second, command who | grep abcd111 will also return true if you happen to have a coomand named abcd111 executed by some other user. Also, it is true if you have a user named abcd1112.


Report •
Related Solutions


Ask Question