Hello there ULF,
Good day! Just want to share my code and as well as my problem on why I'm not getting the output that I want. My original code was:
#!/usr/bin/sh
echo
echo -n "Please input an IP-Pool: "
read ip
echo
echo "Please wait....."
/home/nsadm/scripts/IP-PoolSession-Checker/ip_checker1.sh $ip > temp_output
sleep 15
/home/nsadm/scripts/IP-PoolSession-Checker/ip_checker2.sh $ip | grep -v NOTE >> temp_output
sleep 1
cp temp_output final_output
pd1=`cat temp_output | awk 'NR==10 {print $3}'`
bd1=`cat temp_output | awk 'NR==11 {print $3}'`
pu1=`cat temp_output | awk 'NR==12 {print $3}'`
bu1=`cat temp_output | awk 'NR==13 {print $3}'`
pd2=`cat temp_output | awk 'NR==18 {print $3}'`
bd2=`cat temp_output | awk 'NR==19 {print $3}'`
pu2=`cat temp_output | awk 'NR==20 {print $3}'`
bu2=`cat temp_output | awk 'NR==21 {print $3}'`
pd=$(expr $pd2 - $pd1)
bd=$(expr $bd2 - $bd1)
pu=$(expr $pu2 - $pu1)
bu=$(expr $bu2 - $bu1)
echo >> final_output
echo "Delta Summary:" >> final_output
echo "==============" >> final_output
echo "Delta Pkts-Dwn: $pd" >> final_output
echo "Delta Bytes-Dwn: $bd" >> final_output
echo "Delta Pkts-Up: $pu" >> final_output
echo "Delta Bytes-Up: $bu" >> final_output
cat final_output
rm temp_output
rm final_output
echo
Now since this script can be used by other users, there will be a possibility of having simultaneous running-process for this script which will lead to a wrongly data to be parsed, so in order for this one not to happen, I want to check if the process is running it will prompt the user that it's not yet safe to run the script otherwise begin using it. So I've decided to put something like this into my code:
#!/usr/bin/sh
psef=`ps -ef | grep IP-PoolSession-Summary.sh | grep -v grep | wc -l`
if [ $psef -eq 1 ];
then
echo
echo -e "\033[41;37mSorry! Another user is currently running the script. Please try again later.\033[0m"
echo
exit
else
echo -n "Please input an IP-Pool: "
read ip
echo
echo "Please wait....."
/home/nsadm/scripts/IP-PoolSession-Checker/ip_checker1.sh $ip > temp_output
sleep 15
/home/nsadm/scripts/IP-PoolSession-Checker/ip_checker2.sh $ip | grep -v NOTE >> temp_output
sleep 1
cp temp_output final_output
pd1=`cat temp_output | awk 'NR==10 {print $3}'`
bd1=`cat temp_output | awk 'NR==11 {print $3}'`
pu1=`cat temp_output | awk 'NR==12 {print $3}'`
bu1=`cat temp_output | awk 'NR==13 {print $3}'`
pd2=`cat temp_output | awk 'NR==18 {print $3}'`
bd2=`cat temp_output | awk 'NR==19 {print $3}'`
pu2=`cat temp_output | awk 'NR==20 {print $3}'`
bu2=`cat temp_output | awk 'NR==21 {print $3}'`
pd=$(expr $pd2 - $pd1)
bd=$(expr $bd2 - $bd1)
pu=$(expr $pu2 - $pu1)
bu=$(expr $bu2 - $bu1)
echo >> final_output
echo "Delta Summary:" >> final_output
echo "==============" >> final_output
echo "Delta Pkts-Dwn: $pd" >> final_output
echo "Delta Bytes-Dwn: $bd" >> final_output
echo "Delta Pkts-Up: $pu" >> final_output
echo "Delta Bytes-Up: $bu" >> final_output
cat final_output
rm temp_output
rm final_output
echo
fi
The problem with this code is that it's not prompting the user and still continues and proceed with the original code (which is after the else function) even if I already ran one before running it again.
Then I've modified the if/else by doing something like this:
#!/usr/bin/sh
psef=`ps -ef | grep IP-PoolSession-Summary.sh | grep -v grep | wc -l`
if [ $psef -eq 1 ]; then
echo
echo -e "\033[41;37mSorry! Another user is currently running the script. Please try again later.\033[0m"
echo
exit
elif [ $psef -eq 0 ]; then
echo
echo -n "Please input an IP-Pool: "
read ip
echo
echo "Please wait....."
/home/nsadm/scripts/IP-PoolSession-Checker/ip_checker1.sh $ip > temp_output
sleep 15
/home/nsadm/scripts/IP-PoolSession-Checker/ip_checker2.sh $ip | grep -v NOTE >> temp_output
sleep 1
cp temp_output final_output
pd1=`cat temp_output | awk 'NR==10 {print $3}'`
bd1=`cat temp_output | awk 'NR==11 {print $3}'`
pu1=`cat temp_output | awk 'NR==12 {print $3}'`
bu1=`cat temp_output | awk 'NR==13 {print $3}'`
pd2=`cat temp_output | awk 'NR==18 {print $3}'`
bd2=`cat temp_output | awk 'NR==19 {print $3}'`
pu2=`cat temp_output | awk 'NR==20 {print $3}'`
bu2=`cat temp_output | awk 'NR==21 {print $3}'`
pd=$(expr $pd2 - $pd1)
bd=$(expr $bd2 - $bd1)
pu=$(expr $pu2 - $pu1)
bu=$(expr $bu2 - $bu1)
echo >> final_output
echo "Delta Summary:" >> final_output
echo "==============" >> final_output
echo "Delta Pkts-Dwn: $pd" >> final_output
echo "Delta Bytes-Dwn: $bd" >> final_output
echo "Delta Pkts-Up: $pu" >> final_output
echo "Delta Bytes-Up: $bu" >> final_output
cat final_output
rm temp_output
rm final_output
echo
fi
As you can see, I've just changed the if/else to if/elif, but again the output is that after executing the script, it automatically exits. No prompt appeared even.
Can you please help me with this problem? I'm working on a SUSE Linux.
Thanks and Best Regards,
rymnd_12345