LinuxQuestions.org
Visit Jeremy's Blog.
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-15-2012, 11:14 PM   #1
ywang016
LQ Newbie
 
Registered: Feb 2012
Posts: 1

Rep: Reputation: Disabled
help on bash script programming related to read file and get last number


hi guys I have problems in appending the last number of file into the file.

I wrote the code to read but I dont know how to retrieve the last number which is in the last line
a=0;
while read line
do
value=`expr $a+ 1`;
echo $a;
done < "test"
echo final line number is $a;
 
Old 02-16-2012, 12:41 AM   #2
hallucinative
LQ Newbie
 
Registered: Feb 2012
Location: Western Australia
Distribution: Ubuntu Server
Posts: 18

Rep: Reputation: 5
I'm not the best with scripting, but there's a command I commonly abuse for log files "cat server.log | tail -n 1" shows the last line of a log, using very little IO\CPU time... I hope this has some how helped..
 
Old 02-16-2012, 12:48 AM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Quote:
Originally Posted by hallucinative View Post
I'm not the best with scripting, but there's a command I commonly abuse for log files "cat server.log | tail -n 1" shows the last line of a log, using very little IO\CPU time... I hope this has some how helped..
FYI - no need to use "cat" for that, "tail -n 1 server.log" would accomplish the same thing.
 
Old 02-16-2012, 12:49 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
You have a few errors there; try this
Code:
a=0;
while read a
do
value=`expr $a + 1`;
echo $a $value;
done < "t.t"
echo final line number is $a;
uses
Code:
# file t.t
1
2
3
4
note the first line of code is redundant in this case.
See http://www.cyberciti.biz/faq/bash-while-loop/.
you might also want to use
Code:
set -xv
at the top of your code for debugging.

See also
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-16-2012, 12:51 AM   #5
hallucinative
LQ Newbie
 
Registered: Feb 2012
Location: Western Australia
Distribution: Ubuntu Server
Posts: 18

Rep: Reputation: 5
Quote:
Originally Posted by suicidaleggroll View Post
FYI - no need to use "cat" for that, "tail -n 1 server.log" would accomplish the same thing.
Fair enough! I tip my hat to you good sir.. Abuse of pipes just happens to be one of my flaws XD
 
Old 02-16-2012, 12:51 AM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Quote:
Originally Posted by ywang016 View Post
hi guys I have problems in appending the last number of file into the file.

I wrote the code to read but I dont know how to retrieve the last number which is in the last line
a=0;
while read line
do
value=`expr $a+ 1`;
echo $a;
done < "test"
echo final line number is $a;
I'm not quite understanding your problem. Your first sentence sounds like you're trying to cat the last number in the file back into the file (repeating the last line?), while the second sentence sounds like you just want to retrieve the last number in the file.

I'm not sure I understand what your "value" variable is supposed to contain, since it's filled but never used in your script. What about your script is failing?
 
  


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] script to read number from config file mcollis Programming 6 09-13-2011 08:52 AM
how to read text file using bash script kkpal Linux - Newbie 2 03-03-2008 12:40 PM


All times are GMT -5. The time now is 10:44 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