Visit Our UNIX and Linux User Community


Strange one (likely something simple...)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Strange one (likely something simple...)
# 1  
Old 10-30-2007
Question Strange one (likely something simple...)

Right folks... this is probably something simple that I'm missing as it's early, but can anyone tell me what's going on here:

The Script:

Code:
cougar:/usr/hxscripts>vi ~/test.ksh
"/usr/home/branch/users/tech/test.ksh" [New file]
#!/bin/ksh

LOGDIR=/usr/hxscripts/LOGS/logmeoff/    # Logging directory, must be world-writable
RETDAYS=31               # Number of days to retain old logfiles


ME=`logname`            # Username
SCRIPT="logmeoff"       # Script name

# FUNCTIONS
###########


Log () {
set -x

##Construct the log file path and name##
LOGFILE=${LOGDIR}/${1}.`date +%a%d%b%y`.logmeoff.log

##Check whether today's logfile exists and create if not##
[ -w $LOGFILE ] ||  ( touch $LOGFILE; chmod 666 $LOGFILE )

##Delete logmeoff log files older than $RETDAYS old##
find $LOGDIR -type f -name '*.logmeoff.log' -mtime +$RETDAYS -exec rm {} \;

##Write the log entry##
echo "`date +%T`|`logname`|`who am i`" >> $LOGFILE

}


Log test

And the output:

Code:
cougar:/usr/hxscripts>ksh ~/test.ksh
+ + date +%a%d%b%y
LOGFILE=/usr/hxscripts/LOGS/test.Tue30Oct07.logmeoff.log
+ [ -w /usr/hxscripts/LOGS/test.Tue30Oct07.logmeoff.log ]
+ touch /usr/hxscripts/LOGS/test.Tue30Oct07.logmeoff.log
+ chmod 666 /usr/hxscripts/LOGS/test.Tue30Oct07.logmeoff.log
+ find /usr/hxscripts/LOGS -type f -name *.logmeoff.log -mtime +31 -exec rm {} ;
+ date +%T
+ logname
+ who am i
+ echo 09:06:26|tech|tech        pts/1374    30 Oct 08:53     (g10wshud311.zg.if.atcsg.net)
+ 1>> /usr/hxscripts/LOGS/test.Tue30Oct07.logmeoff.log
cougar:/usr/hxscripts>ls -ld /usr/hxscripts/LOGS/logmeoff/
drwxrwxrwx   2 tech     users           512 17 Oct 13:25 /usr/hxscripts/LOGS/logmeoff/

As you can see, even though I'm specifying that the LOGDIR is the existing and world-changable directory /usr/hxscripts/LOGS/logmeoff/, everything goes to /usr/hxscripts/LOGS. Why is this?!

Obviously this isn't causing me massive headaches, but I'd like to know what's going on!

Many thanks,

Alex
# 2  
Old 10-30-2007
Code:
LOGDIR=/usr/hxscripts/LOGS/logmeoff/

Is the trailing slash needed as you add another when you concatenate?
# 3  
Old 10-30-2007
Quote:
Originally Posted by porter
Code:
LOGDIR=/usr/hxscripts/LOGS/logmeoff/

Is the trailing slash needed as you add another when you concatenate?
No, I guess it isn't needed.

I just prefer to put it in when using directory paths in variables which I know are going to be used in such a way. That way you either end up with 'DIRECTORY/file', or 'DIRECTORY//file'. Rather than risking 'DIRECTORYfile'.

I didn't think that either mattered.
# 4  
Old 10-30-2007
I suppose at this point I should mention I'm using ksh on AIX
# 5  
Old 10-30-2007
Try changing the code a little bit:

Code:
#! /bin/ksh

LOGDIR="/home/Administrator/LOGS/logmeoff"    # Logging directory, must be world-writable
RETDAYS="31"               # Number of days to retain old logfiles


ME="$(logname)"            # Username
SCRIPT="logmeoff"       # Script name

# FUNCTIONS
###########


Log () {
set -x

##Construct the log file path and name##
LOGFILE="$LOGDIR"/"$1"."$(date +%a%d%b%y)".logmeoff.log

##Check whether today's logfile exists and create if not##
[ -w "$LOGFILE" ] ||  { touch "$LOGFILE"; chmod 666 "$LOGFILE"; }

##Delete logmeoff log files older than $RETDAYS old##
find "$LOGDIR" -type f -name '*.logmeoff.log' -mtime +"$RETDAYS" -exec rm {} \;

##Write the log entry##
printf "%s\n" "$(date +%T)|$ME|$(who am i)" >> "$LOGFILE"

}

Log test

# 6  
Old 10-30-2007
oopss....

I've been a little stupid on this one.

Thanks for your input Porter and Radoulov. Radoulov - I see what you're getting at there with all the quoting variables. Whilst it might be a good habit I don't think it would have made much difference in this instance.

There was a part of the script that I had cut out - it's a standard thing we chuck in our scripts which assigns a load of useful variables - including LOGDIR. It was overwriting it with /usr/hxscripts/LOGS - it just didn't occur to me that this might happen. Oops.

Sorry to waste your time, folks!

Thanks again.

-Alex
# 7  
Old 10-30-2007
Quote:
Originally Posted by alexop
[...]
Radoulov - I see what you're getting at there with all the quoting variables. Whilst it might be a good habit I don't think it would have made much difference in this instance.
[...]

Yes, it's just a good habit ...

Previous Thread | Next Thread
Test Your Knowledge in Computers #917
Difficulty: Easy
Unix time is also known as Epoch time.
True or False?

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

A Simple Clock, Well Maybe Not That Simple...

The attachment says it all really... It is a DEMO at a glance digital readout using the "date" command to make it useful... For a Mocbook Pro 13", OSX 10.7.5, but may well work on Linux variants too. Enjoy... #!/bin/bash # # Clock.sh # A bash DEMO to create a 6 x 7 character set... (4 Replies)
Discussion started by: wisecracker
4 Replies

2. Red Hat

Syslog.conf: looking for a simple answer on a simple question

Cheers! In /etc/syslog.conf, if an error type is not specified, is it logged anywhere (most preferable is it logged to /var/log/messages) or not? To be more precise I am interested in error and critical level messages. At default these errors are not specified in syslog.conf, and I need to... (6 Replies)
Discussion started by: dr1zzt3r
6 Replies

3. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

4. Shell Programming and Scripting

Simple to you not simple to me pattern matchin help

hey all, im new and my first question is: say i have a word "blahblah" how do i get and replace the last letter of the word with say k, so replace the h with a k. However you cant just replace the h it has to change the LAST LETTER of the word. Cheers In advance. :b: (0 Replies)
Discussion started by: aleks001
0 Replies

5. Shell Programming and Scripting

Strange Logic

I am trying to read a file and skip few records based upon the following two columns. Pipe delimiter used between the two columns. Column1|Column2 Property|CutOff Target|11111 Min|9999 Max|10000 Comment|This is a test Property|Weight Target|222 Min|3434 Max|77777 UOM|mm ... (5 Replies)
Discussion started by: ganesh123
5 Replies

6. Programming

Simple C question... Hopefully it's simple

Hello. I'm a complete newbie to C programming. I have a C program that wasn't written by me where I need to write some wrappers around it to automate and make it easier for a client to use. The problem is that the program accepts standard input to control the program... I'm hoping to find a simple... (6 Replies)
Discussion started by: Xeed
6 Replies

7. Solaris

Something strange...

Hi all, Thanks for any replies and for reading in advance. We have upgraded one of our database instances to 10g on a Solaris 8 box, anyhow the other day it started trying to ping loads of weird IP addresses that we don't use, since our systems all run on pretty similar IP's. It all behind... (0 Replies)
Discussion started by: B14speedfreak
0 Replies

8. UNIX for Dummies Questions & Answers

Ok simple question for simple knowledge...

Ok what is BSD exactly? I know its a type of open source but what is it exactly? (1 Reply)
Discussion started by: Corrail
1 Replies

9. UNIX for Dummies Questions & Answers

strange

Hi All I am doing a locate <file_name> on my Redhat 7 System. I am unable to get the output. All the keep getting is: locate: this is not a vlaid slocate database: /var/lib/locate/slocate.db What des this mean? Is my system compromised? Thanks in advance. KS (13 Replies)
Discussion started by: skotapal
13 Replies

Featured Tech Videos