nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

Shell script to count number of lines in file specified by the second command-line

This is a discussion on Shell script to count number of lines in file specified by the second command-line within the Shell scripting forums, part of the Development/Scripting category; please help me, I have problem with this script, I don't know what the error with this Syntax 'grep ^i ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 16-03-2009, 03:46 PM
Junior Member
User
 
Join Date: Feb 2009
OS: fedora
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
seaman77 is on a distinguished road
Default Shell script to count number of lines in file specified by the second command-line

please help me,
I have problem with this script, I don't know what the error with this Syntax 'grep ^i $2 | WC -l'

Write a shell script that checks to see if the first command-line option is –i or –e. If it is –i, the program count the number of lines in the file specified by the second command-line option that begin with the letter i. If the first option is –e, the program count the number of lines in the file specified by the second command-line option that begins with letter e. If the first command-line option is not –i or –e, the program prints a brief error message to the screen.

this is my script

#!/bin/bash
#tarek_20307
#exercise 3:
case $1 in
-i)
count= 'grep ^i $2 | WC -l'
echo "The Number of LINES in The $2 That begin with the letter i IS $count"
;;
-e)
count='grep ^e $2 | wc -l'
echo "The Number of LINES in The < $2 > That begin with the letter e IS< $count >"
;;
*)
echo "error: this option is incorrect "
;;
esac

Please help me to fix the error
Reply With Quote
  #2 (permalink)  
Old 16-03-2009, 07:46 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,709
Thanks: 11
Thanked 244 Times in 183 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

You need to use backticks (`) and not a single quote ('). Also wc should be in lower case. Here is fixed script
Code:
#!/bin/bash
#tarek_20307
#exercise 3:
case $1 in
-i)
count=`grep ^i $2 | wc -l`
echo "The Number of LINES in The $2 That begin with the letter i IS $count"
;;
-e)
count=`grep ^e $2 | wc -l`
echo "The Number of LINES in The < $2 > That begin with the letter e IS< $count >"
;;
*)
echo "error: this option is incorrect "
;;
esac
Run it as
Code:
./script.name -i filename.txt
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
Reply

Tags
backticks , case , grep , shell , wc command


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
SED Script to remove a few lines out of a file cjredding Shell scripting 31 18-07-2009 07:39 PM
Shell script tp Log files last line kasimani Shell scripting 3 24-03-2009 03:25 PM
grep command count number of CPU sidebrake Shell scripting 3 09-09-2008 11:26 PM
Script to display number of lines and words from a file newbewie Shell scripting 2 07-12-2007 10:37 PM


All times are GMT +5.5. The time now is 03:48 AM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38