shell-script-while-loop-examples

$ cat log*
#! /bin/sh
echo "The current users are:"
who | awk '{print $1}' | sort > temp1
cp temp1 temp2
more temp1
while true
do
    who | awk '{print $1}' | sort > temp2
    cmp -s temp1 temp2
    case "$?" in
    0)
        echo "No user has logged in/out in the last 5 seconds."
        ;;
    1)
        user=`comm -23 temp1 temp2`
        file=`grep $user temp1 temp2 | cut -c 1-5`
        if [ $file = "temp1" ]
            echo "User "$user" has logged out."
        if [ $file = "temp2" ]
            echo "User "$user" has logged in."
        ;;
    esac
    rm temp1
    mv temp2 temp1
    sleep 5
done

$ cat lag.sh
#!/bin/ksh
echo "The current users are:"
who | awk '{print $1}' | sort > temp1
cp temp1 temp2
more temp1
while true
do
who | awk '{print $1}' | sort > temp2
cmp -s temp1 temp2
case "$?" in
1)
user="adcwcgq"
file=`grep $user temp1 temp2 | cut -c 1-5`
echo $file
file1=`echo $file | cut -c 1-5`
echo $file1
if [ $file1 = "temp1" ]
then echo "User "$user" has logged out." | mail -s "Logged out" info@aiobox.net
cat /home/absprod1/test_audit | mail -s "Audit report" info@aiobox.net
fi
esac

$cat montun.sh
#!/bin/sh
while [ 1 ]; do
STATUSTUN=`ping -c 2 -W 3000 192.168.0.1 |grep loss |awk '{ print $7; }'`
AA="0.0%"
if [ $STATUSTUN = "100.0%" ];then
echo "Down!!" >> /var/log/messages
sleep 10
else
echo "Up!!"
fi
sleep 20
done
#!/bin/csh
set yname="foo"
while ( $yname != "" )
	echo -n "Enter your name : "
	set yname = $<
	if ( $yname != "" ) then
		echo "Hi, $yname"
	endif
end
Social tagging: > > > >

Comments are closed.