Register
It is currently Wed Jul 23, 2014 11:42 pm

Need a script to read a few lines from a file


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Thu Apr 16, 2009 10:07 am   

Joined: Thu Apr 16, 2009 9:54 am
Posts: 5
Hey,

I need the following script.
for example

./showme test.txt 15
the first variable is the filename
the second one is a number to show 15 lines from the tail of the fail.

If the first variable doesn`t exist it should show error
if the second variable doesn`t exist it should take 10 lines of the tail.
and show them on screen.

Can someone help me.

Thanx


Top
 Profile  
 PostPosted: Fri Apr 17, 2009 3:34 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Wait a minute... Is this a trick question or a homework project? ;) You just described the "tail" command!

-Jeo


Top
 Profile YIM  
 PostPosted: Tue Apr 21, 2009 12:44 am   

Joined: Thu Apr 16, 2009 9:54 am
Posts: 5
It`s not a trick question.
And it`s not homework,
But I have an exam coming soon and there will be some scripting with tail commands.
and I never used tail before.
So I tought if I have an example then I can integrate it in other questions.
:P :P :P


Top
 Profile  
 PostPosted: Wed Apr 22, 2009 9:09 am   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Right on :)

Here's how I'd do it. I put some comments in, but let us know if you need help with anything!

Code:
#!/bin/sh

## First let's see how many variables we
## got from the command line, and exit
## with a usage message if we don't have
## what we need

## if the number of arguments ($#) is less than (-lt) 1
## echo "usage"

if [ $# -lt 1 ]; then
  echo "usage: $0 <file> <lines>"
  exit 1
fi

## Next, let's set the filename variable using our
## command line input.  "$1" is the first thing after
## the script name on the command line.  The script
## name itself is "$0", which we used above in the
## usage line

file="$1"

## Now let's set the number of lines, with the default
## that you specified (10) if nothing is provided

## if there is no (-z = zero) second argument ($2)...

if [ -z "$2" ]; then
  lines="10"
else
  lines="$2"
fi

## Now the real work begins!

tail -n${lines} ${file}

## Done!


Hope this helps!
-J


Top
 Profile YIM  
 PostPosted: Wed Apr 22, 2009 3:32 pm   

Joined: Thu Apr 16, 2009 9:54 am
Posts: 5
Ah thanx
And thank you for explaining everything.
It`s now crystal clear.

But I don`t understand a few things that you wrote.
I had to alter them because it didn`t work at first.
Maybe you can explain.

First in the first line you wrote /sh is that something else then /bash

second in the first if section you wrote exit 1 (What does the 1 do??)

and last in the last line you used {}, I had to remove does.
tail -n $lines $file And I used this.

and mayby a very stupid question.
For remarques I learned to use 1#, you used ## is there a differents.

Thank you again
I`ve learned a lot.


by the way
The code below is how it works on my computer.



Code:
#!/bin/bash

if [ $# -lt 1 ]; then
echo "usage; $0 <file> <lines>"
exit
fi

file="$1"

if [ -z "$2" ]; then
lines="10"
else
lines="$2"
fi

tail -n $lines $file



Top
 Profile  
 PostPosted: Tue May 05, 2009 7:14 am   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Hi! I'm glad it worked! Looks like I need to work on making my scripts more universal...

>> First in the first line you wrote /sh is that something else then /bash
/bin/sh on most systems is actually a link to /bin/bash on many systems. sh is the basic shell, the predecessor to bash.

>> second in the first if section you wrote exit 1 (What does the 1 do??)
The "1" is the exit status. It's not really necessary in this case unless you plan on taking some other action based on the exit status. 1 means 'error'

>> and last in the last line you used {}, I had to remove does.
>> tail -n $lines $file And I used this.
That's odd. I thought that the {brackets} were a universal thing. Typically they're used if your variable is included in the middle of some other text, to provide some separation. They're not necessary in this case though, so removing them works great.

>> and mayby a very stupid question.
No such thing!

>> For remarques I learned to use 1#, you used ## is there a differents.
No difference, just a bad habit on my part :) You only need 1#


Top
 Profile YIM  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP