LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Simple Script : But didnt work as i expect (https://www.linuxquestions.org/questions/linux-newbie-8/simple-script-but-didnt-work-as-i-expect-941259/)

kingston 04-23-2012 05:12 AM

Simple Script : But didnt work as i expect
 
Hi All

I am trying to find the users who are all using emacs editor in my linux server and i wrote the following script.

Quote:

#!/bin/bash
#This script is used to find the usage of emacs
#Whether it is being used or not and
#Who and When using the same.

user=`ps -ef |grep emacs |awk '{print $1}'|grep -v root`
process=`ps -ef |grep emacs |awk '{print $8}'|grep -v gre`

echo $user > one.txt
echo $process >> one.txt

`mail -s "$process invoked by $user" kingston@win.com < one.txt
The output is as follows, with the subject "emacs"
Quote:

/bin/bash
/bin/bash
emacs invoked by mursir
kinved

mursir kinved
emacs /bin/bash /bin/bash emacs
What i expect is a mail with the subject "emacs invoked by mursir kinved", with the following content
mursir kinved
emacs emacs

When i fired the individual commands, i have got the following outputs.

Quote:

[root@einbalx0014 ~]# user=`ps -ef |grep emacs |awk '{print $1}'|grep -v root`
[root@einbalx0014 ~]# echo $user
mursir kinved
[root@einbalx0014 ~]# process=`ps -ef |grep emacs |awk '{print $8}'|grep -v gre`
[root@einbalx0014 ~]# echo $process
emacs emacs
Can someone help me to attain the expected result?

Ser Olmy 04-23-2012 07:32 AM

I removed the backtick in front of the mail command, and received a mail with the names of all users running emacs on one line, and a line below saying "emacs emacs emacs" etc.

kingston 04-24-2012 02:24 AM

@ser_omly. Thanks for your reply. But it is not working for me still at the same time, i write this script in a different way and working fine.
Quote:


#This script is used to find the usage of emacs editor
rm -rf 1
rm -rf 2
touch 1
touch 2
for i in `ps -ef |grep emacs |awk '{print $1}'|grep -v root`
do
echo $i >> 1
done
for j in `ps -ef |grep emacs |awk '{print $8}'|grep -v gre`
do
echo $j >> 2
done
touch one.txt
presize=`ls -l one.txt |awk '{print $5}'`

paste 1 2 |pr -t -e20 > one.txt

if [[ -s one.txt ]] ## This Part will check the file one.txt
## is a empty one or not. If it is a empty one
## there is no need to alert the System Admin
then
cursize=`ls -l one.txt |awk '{print $5}'`
if [[ $cursize -gt $presize ]]
then
mail -s "emacs editor invoked" kingston.vedanayagam@hp.com < one.txt
else
touch one.txt
fi
else
#echo "no one is using emacs as of now"
touch one.txt
fi


All times are GMT -5. The time now is 02:32 AM.