Tuesday, 11 November 2014

script for interview perpose

Write a bash​ shell​ or perl script which does the following:

 - accepts a syslog file path, comma seperated list of service names and another argument which is a string (​this is an optional argument)
 - prints the count of logs of each service name on a separate line
 - If the optional string argument is provided, then searches for it in the logs of the provided service names and prints its count ​on a separate line.
 - prints usage if it is incorrect

 This was interview question for me .  Look how i have given the solution after 3 attempt :)



 Writer: Anuj Borah
Email: anujborah1@gmail.com



#!/bin/bash
clear
 echo -e "__________ Welcome____________\n"
if [ $# -eq 0 ]; then
echo "Please provide the ./script-name, file name and service-name (ex- ./script syslog CRON,CRON... critical )  "
fi
if [ $# -eq 2 ]; then
test -f $1
if [ $? -eq 0 ]
then
a=$1
oIFS="$IFS"; IFS=, ; set -- $2 ; IFS="$oIFS"
for i in "$@"; do
echo $i has the following count:
grep -o $i $a | wc -l
grep $i $a >> /tmp/1
done
fi
elif [ $# -eq 3 ]; then
echo -e "$1\n$2\n$3" >> /tmp/2
a=$1
oIFS="$IFS"; IFS=, ; set -- $2 ; IFS="$oIFS"
for i in "$@"; do
echo $i has the following count:
grep -o $i $a | wc -l
grep $i $a >> /tmp/1
done

b=`awk 'NR==3' /tmp/2`
echo  -e "\nThe over all count for $b is as follows:"
grep $b /tmp/1 | wc -l || echo "No such word" && echo "All Done"
rm /tmp/1 > /dev/null
rm /tmp/2 > /dev/null
else
echo "No such file"
fi



See the output



No comments:

Post a Comment

Popular Posts