(probably stupidly simple) if [ $var1 = $var2 ] problem...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting (probably stupidly simple) if [ $var1 = $var2 ] problem...
# 1  
Old 06-18-2008
(probably stupidly simple) if [ $var1 = $var2 ] problem...

OK, while I'm not new to the Mac or some of the the inner workings of Mac OS X I am quite new to scripting. And while I have "Learning the Bash Shell" by those lovely people at O'Reilly (Cameron Newham et. al.) I'm missing something, I just know I am.

Here's the problem:

At the beginning of my script I declare 2 variables:

Code:
#Uncomment the next line for log lines to be timestamped with the server's local time (BST, GMT, etc.)
timestamp=$(date)

#Uncomment the next line for log lines to be timestamped with UTC (Universal Coordinated Time)
#timestamp=$(date -u)

Then, later I want to log which type of timestamp is being used, so I tried every variation I could think of on:

Code:
if [ $timestamp=date ]
	then
		echo -e $timestamp "\tLogging using Server's Local Time" >> /path/to/logfile.log
	else
		if [ $timestamp=date -u ]
			then
				echo -e $timestamp "\tLogging using UTC" >> /path/to/logfile.log
			else
				echo -e $timestamp "\tI don't know what timestamp I'm logging with, but I'll carry on" >> /path/to/logfile.log
		fi
fi

My problem occurs at the [ $timestamp=date ] and [ $timestamp=date -u ] bits. It fails numerous ways and the best I've got is where it logs both "Logging using Server's Local Time" AND "Logging using UTC".

I've tried various combinations of:
Code:
[ $timestamp=date ] #btw, single brackets invariably end up in a 'too many arguments' complaint from the shell
[ $timestamp='date' ]
[[ $timestamp=$(date) ]]

etc...

I also tried this
Code:
fndate()
{
date
return $fndatevar
}

(and one for date -u)

and then using them like:
Code:
if [ $timestamp=$fndatevar ]

etc.

Still no joy

... I could go on. But at 03:00 this morning I decided to go to bed and ask. So here I am, asking.

Is it clear what I'm trying to do? Test which version of $timestamp the script is using an log the fact (even though it'll be obvious from the timestamps themselves which version is being used. It's more an exercise in what's possible and to learn how to do it than to do any rocket science).

Many thanks to those about to help me.
# 2  
Old 06-18-2008
Code:
if [ "$timestamp" = "$(date)" ]

if [ "$timestamp" = "$(date -u)" ]

Use quotes + date has to execute as a child process in order top return a value.
# 3  
Old 06-18-2008
Quote:
Originally Posted by jim mcnamara
Code:
if [ "$timestamp" = "$(date)" ]

if [ "$timestamp" = "$(date -u)" ]

Use quotes + date has to execute as a child process in order top return a value.
I'm sure I tried those variations last night. I'll have another go as to be honest my brain was getting a bit wooly around the edges by the end of it all and I might just be randomly lying!

Anyway, thank you. I appreciate the swiftness of response!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert .../($var1|$var2)/... regex to string

Hello, In one part of my shell program I need to translate many of lines in the following pattern : /(folder1|...|folderN)/(sub1|...|subN)/.../(file1|...|fileN) into strings : /folder1/sub1/.../file1 /folder1/sub1/.../... /folder1/sub1/.../fileN ... /folderN/subN/.../fileN the... (2 Replies)
Discussion started by: Ghadamyari
2 Replies

2. Shell Programming and Scripting

simple wc problem

Hi there, Im sure there is a simple explanation for this but I have a file like this with no balnk lines peter paul john I run the command # var=`grep paul file.txt` # echo $var paul # echo $var | wc -l 1 but when I grep for a value that isnt in the file, i still... (4 Replies)
Discussion started by: rethink
4 Replies

3. Shell Programming and Scripting

Please help me with a simple problem

Hi, I have a very simple script like below: for n in 10 20 30 do for a in 30 40 50 60 70 80 do for r in 2 3 4 5 6 7 do m=$((r*a)) count=1 while do echo "a = " $a ", m = " $m ", n = " $n ... (2 Replies)
Discussion started by: Dark2Bright
2 Replies

4. Shell Programming and Scripting

concatenate varaibles var1, var2 together to var1 again

HI i like to concatenate the two variables var1=$line (ie 22885068900000652 B86860003OLFXXX592123320081227) var2=$amount (ie 123456) i want to club together both the above varaible var1 & var2 and assign back to var1 right now i am doing like this but it is not working. var1=`echo... (1 Reply)
Discussion started by: kshuser
1 Replies

5. UNIX for Dummies Questions & Answers

What is this? var2=${var1#??????????}

Googling the answer to this question just doesn't work when Google won't search your symbols for you. Can someone tell me what this command will assign to var2, and where I can find more information about using braces in this way? var2=${var1#??????????} Thanks so much. (2 Replies)
Discussion started by: blondie53403
2 Replies

6. AIX

Repair boot sector after stupidly importing disk into vg?

Hey guys, I've bought a second-hand RS/6000 hardware with freshly installed AIX 5.3 on disk2 and freshly installed AIX 6 on disk1. I'm in possession of the original 5.3 install cds but the AIX 6 installation is just on the disk, I've no install media. So first after getting the system, I... (3 Replies)
Discussion started by: selzeda
3 Replies

7. Shell Programming and Scripting

Simple Scripting Problem

Hi there, I was trying to add a line of text in the middle line of a file. I have counted the lines in the file, and then I divide it into 2, after that I am stuck on how am I suppose to append the line on that file? When I tried to use this command 'second line >> filename' it appends it at... (3 Replies)
Discussion started by: felixwhoals
3 Replies

8. Shell Programming and Scripting

Echo var1,var2,var3,var4,var5,var6 in loop help

Hello, I have created numerous variables called $mask1 $mask2... $maskN. I wish to echo these variables, below is the code for the two functions, the first creates the variables and the second echos them. It is the second function which does not work. The line (i believe to be wrong) is in... (1 Reply)
Discussion started by: rorey_breaker
1 Replies

9. UNIX for Dummies Questions & Answers

Simple multiplication problem

I'am doing a tutorial where a simple calculator was given, then i noticed that you can't actually multiply this is how i have approached the problem so far. i just need if the user enters "*" to change it to "/*" ,is it possible? i know that * means the name of the last file in the directory... (8 Replies)
Discussion started by: greekozz
8 Replies

10. UNIX for Dummies Questions & Answers

*fixed if (( var1 && var2 )) ??

*EDIT -- **FIXEd I must've done something wrong, because it works now.. I punched in "if" in the forum search but nothing came up, so I'm posting it here. Quick question: Can someone correct my syntax. #!/bin/ksh if (( var >= 1 && var <= 5 )); then .................................. (1 Reply)
Discussion started by: yongho
1 Replies
Login or Register to Ask a Question