Tuesday, January 12, 2016

Write a shell script to count number of lines, number of words and number of character in a given file with menu options.

# menu.sh
# Program :         Write a shell script to count number of lines, number of words and number of character in a given file with menu options.
# Author :           sujith
# Date :              17/03/2015

echo "Enter the filename"
read file

echo "menu"
echo "1->lines"
echo "2->words"
echo "3->characters"
echo "4->exit"

echo "Enter the choice"
read choice

case $choice in
        1) set `wc -l $file`
         l=`echo $1`
         echo "no of lines in $file is:$l"
         continue;;
        2) set `wc -w $file`
         w=`echo $l`
         echo "no of words in $file is: $w"
         continue;;
        3) set `wc -c $file`
         c=`echo $l`
         echo "no of characters in $file is : $c"
         continue;;
        4)
        echo "Invalid choice"
        break;
esac


Output:
[141740@localhost ~]$ sh menu.sh
Enter the filename
str.sh
menu
1->lines
2->words
3->characters
4->exit
Enter the choice
1
no of lines in str.sh is:29

[141740@localhost ~]$

[141740@localhost ~]$ sh menu.sh
Enter the filename
str.sh
menu
1->lines
2->words
3->characters
4->exit
Enter the choice
2
no of words in str.sh is:77

[141740@localhost ~]$

[141740@localhost ~]$ sh menu.sh
Enter the filename
str.sh
menu
1->lines
2->words
3->characters
4->exit
Enter the choice
3
no of characters in str.sh is : 357

[141740@localhost ~]$









1 comment:

  1. Its a great pleasure reading your post.Its full of information I am looking for and I love to post a comment that "The content of your post is awesome" Great work. word counter

    ReplyDelete