LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-01-2013, 03:47 PM   #1
doucettej3
LQ Newbie
 
Registered: Feb 2013
Posts: 2

Rep: Reputation: Disabled
Counting the amount of lines in a file and comparing the result to a variable in BASH


I am writing a script that will need to compare the number of lines in a file to an integer the user inputs. When I try run the code it gives me an error stating "Line 15 Integer expression expected" I'm not sure if wc returns a string value, so that might be the issue. This script is part of a lab I'm doing for my UNIX class so I have some specifications that need to be met for the error checking of the script.

1. You get the right number of arguments (3).
2. The file specified exists and is a normal file.
3. The first line number specified is less than or equal to the last line number specified.
4. The actual number of lines in the file is greater than the last line to be printed.

I got the first three all done and am on part four.

My Code:
Code:
#!/bin/bash

ans1= cat "$3" | wc -l

if [ $# -ne 3 ]
then
echo " Please enter three variables"
elif [ ! -f "$3" ]
then
echo " File does not exist" 
elif [ "$1" -ge "$2" ]
then
echo " First number must be less than the second"
elif [ "$ans1" -lt "$2" ]
then
echo " Not Enough Lines"
fi
The user will input 2 integers and a filename when running the script.

Any help anyone can give me is much appreciated.
 
Old 02-01-2013, 04:14 PM   #2
Samotnik
Member
 
Registered: Jun 2006
Location: Belarus
Distribution: Debian GNU/Linux testing/unstable
Posts: 471

Rep: Reputation: 40
ans1=$(cat "$3" | wc -l)

read man bash about expressions evaliation
 
Old 02-01-2013, 05:55 PM   #3
doucettej3
LQ Newbie
 
Registered: Feb 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks, it worked.
 
Old 02-03-2013, 07:41 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,411

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
Can i also encourage you to indent your code thus
Code:
if [ $# -ne 3 ]
then
    echo " Please enter three variables"
elif [ ! -f "$3" ]
Makes it much easier to read/debug, especially as it gets longer.

BTW, useful links for bash
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
Old 02-04-2013, 04:41 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,550

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
I would also add that you have not followed your brief very well. You execute your cat command on a third argument that at the point of execution you are not currently sure you got!!

Item 3:
Quote:
3. The first line number specified is less than or equal to the last line number specified.
Is not equal to the following code:
Code:
elif [ "$1" -ge "$2" ]
Item 4 does not make sense unless you have omitted earlier information:
Quote:
4. The actual number of lines in the file is greater than the last line to be printed.
None of the items you have listed corresponds to the following code:
Code:
elif [ "$ans1" -lt "$2" ]
Personally I would also suggest looking into (()) for testing arithmetic equations.
 
Old 02-04-2013, 12:20 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,834

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Code:
#!/bin/bash

ans1=$( wc -l <"$3" )

if (( $# != 3 )); then

    echo " Please enter three variables"

elif [[ ! -f "$3" ]]; then

    echo " File does not exist" 

elif (( "$1" >= "$2" )); then

    echo " First number must be less than the second"

elif (( "$ans1" < "$2" )); then

    echo " Not Enough Lines"

fi
1) Clean, consistent formatting makes code more readable and easily debuggable. Be liberal with whitespace; indent all your sub-commands evenly and separate logical sections with empty lines. Never just line everything up on the left edge. Add comments anywhere the code isn't completely obvious (and remember, what seems completely obvious to you now will not be a year or so down the line).

Many scripters also feel that it's better to place the "do/then" keywords on the same line as the "for/while/until/if" keywords, as they are not separate commands, but the closing indicator for the test block, and paired directly with the opening keyword. Putting them together on one line thus more clearly separates the outside block from the inside block.

Scripting With Style


2) When using bash or ksh, it's recommended to use [[..]] for string/file tests, and ((..)) for numerical tests. Avoid using the old [..] test unless you specifically need POSIX-style portability.

http://mywiki.wooledge.org/BashFAQ/031
http://mywiki.wooledge.org/ArithmeticExpression


3) Useless Use Of Cat. The vast majority of commands can take filenames as direct arguments.

However, in this particular case, while wc is one such program, it will also print the filename in the output. So while we still need it to read from stdin to avoid that, we don't need cat to do it. A simple file redirection will do the job nicely.


Optionally we could read the file directly, and then strip off the filename from the output with a shell parameter substitution. Or use a different command to count the lines.

Code:
ans1=$( wc -l "$3" )
ans1=${ans1%% *}

ans1=$( grep -c '.*' "$3" )
ans1=$( sed -n '$=' "$3" )
ans1=$( awk 'END{ print NR }' "$3" )
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] BASH Script. Result of command input into variable bcyork Linux - Newbie 4 12-06-2011 01:57 PM
Bash: Counting the number of character occurences in a variable basildon Linux - Newbie 3 09-22-2008 11:11 AM
bash script: how to assign an output (not the result) to a variable? Singing Banzo Programming 8 10-01-2006 07:29 PM
cannot export result from awk into a variable in a bash script Emmanuel_uk Linux - Newbie 4 03-07-2005 02:54 AM
bash - comparing a variable to several values davee Programming 3 05-05-2003 08:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration