LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-14-2007, 09:15 AM   #1
antis
Member
 
Registered: Nov 2004
Location: sweden
Distribution: Arch
Posts: 67

Rep: Reputation: 15
bash script, understanding variables


Below is a snippet from a script I'm working on.
Basically it looks at the timestamp in a log file and puts this value into a variable and then prints it.

It's working ok while inside the loop but then, when I'm finished reading the file, the variables are both set to 0.
This is driving me crazy! Can someone please explain to me why this is happening. In this case I expect it to print the last timestamp in the file.

Like everything else the answer is probably quite obvious when you know it

Many thanks!


Code:
#! /bin/bash

LOG_FILE="/var/log/auth.log"
LAST_CHECKED_TIMESTAMP=0
TIMESTAMP_ON_LINE=0

RETURN_TAIL_LINES=`wc -l "$LOG_FILE" | awk '{print $1}'`

tail -n $RETURN_TAIL_LINES "$LOG_FILE" | while read line
do
        TIMESTAMP_ON_LINE=`echo $line | awk '{print $1, $2, $3}'`
        LAST_CHECKED_TIMESTAMP=$TIMESTAMP_ON_LINE

        printf "$LAST_CHECKED_TIMESTAMP\n$TIMESTAMP_ON_LINE"
done

printf "\n\n*******\n$LAST_CHECKED_TIMESTAMP\n$TIMESTAMP_ON_LINE\n"
 
Old 11-14-2007, 09:50 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
well, it's an age old bash thing.
the read variables only work in the while loop, annoying isn't it?

you will find that if you run it in ksh it will work.

if you have it that is.

/bin/sh won't work either if on linux as it's bash in drag.

Last edited by bigearsbilly; 11-14-2007 at 09:52 AM.
 
Old 11-14-2007, 09:54 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,503

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
When piping the output of the tail command to the while loop, the loop itself is executed in a subshell. When the loop exits and the subshell execution is terminated, all the variables assigned in the subshell are lost. To avoid this behaviour you can use "process substitution", as in
Code:
while read line; do
        TIMESTAMP_ON_LINE=`echo $line | awk '{print $1, $2, $3}'`
        LAST_CHECKED_TIMESTAMP=$TIMESTAMP_ON_LINE

        printf "$LAST_CHECKED_TIMESTAMP\n$TIMESTAMP_ON_LINE"
done < <(tail -n $RETURN_TAIL_LINES $LOG_FILE)
Have a look at the Advanced Bash Scripting Guide, chapter 22 for a complete explanation of process substitution.
 
Old 11-14-2007, 12:53 PM   #4
antis
Member
 
Registered: Nov 2004
Location: sweden
Distribution: Arch
Posts: 67

Original Poster
Rep: Reputation: 15
Thanks alot for your comments!
Process substitution indeed looks like they way to go for me.
 
Old 11-15-2007, 02:32 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
or /bin/ksh
 
Old 11-15-2007, 04:27 AM   #6
antis
Member
 
Registered: Nov 2004
Location: sweden
Distribution: Arch
Posts: 67

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by bigearsbilly View Post
or /bin/ksh
Possibly maybe perhaps
Using ksh for the script was my plan from the beginning but a few date issues made me go with bash instead. That might change before it's finished though...
 
  


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
Directory variables in a bash script madtinkerer Programming 9 12-04-2006 12:30 PM
Bash script environment variables mbjunior99 Suse/Novell 4 12-28-2005 12:40 AM
Decimal numbers in bash script variables? Massif Programming 3 11-07-2005 09:01 PM
Using Bash Script for Exporting and Returning Environmental Variables Jicksta Programming 3 12-04-2004 04:14 PM
bash script variables twantrd Programming 7 11-17-2004 02:38 AM


All times are GMT -5. The time now is 11:33 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration