If Else Statement, Goodnight/bye


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers If Else Statement, Goodnight/bye
# 1  
Old 03-01-2012
If Else Statement, Goodnight/bye

I'm working on a script that will send me an email everytime I log out. I've got that down, and now I'm trying to put in an if else statement. Right now, it says "Goodbye $USER" but I'd really like it to say Have a Great Day if it's before 4pm and Have a Good Night if it's after 4pm. So far, I have

Code:
#Variables
TIMED=$(date) | cut -f4 -d
TIMENOW="16:00:00"

if [ "$TIMED" < "$TIMENOW" ]; 
then
      echo Have a nice day $USER
else
      echo Have a good day $USER
fi

This isn't working. Does anyone know what would work?
Thanks

Last edited by methyl; 03-01-2012 at 09:32 PM.. Reason: please use code tags. indented code
# 2  
Old 03-01-2012
Please post what Operating System and version you are running.

Please post the output from the command date on your system (there is much variation).

Quote:
Right now, it says "Goodbye $USER"
It does not.



Quote:
but I'd really like it to say Have a Great Day if it's before 4pm and Have a Good Night if it's after 4pm
In your code samples neither phrase occurs.


Btw: What Shell do you have?

Last edited by methyl; 03-01-2012 at 09:44 PM..
# 3  
Old 03-01-2012
so what I've done..

Ok I have changed it. It now says
Code:
#!/bin/bash
TIMED=$(date +%H)

if [ "${TIMED}" < "16" }; then
echo Have a great day $USER
else
echo Have a great night $USER
fi

That seems to work well enough, however I'm getting an error that read
Code:
$HOME/bin/mylogout: line 6 16: No such file or directory

But then it says Have a great night bbowers.

So it appears to have read it, but what's the error?
Moderator's Comments:
Mod Comment Please use next time
code tags for your code and data

Last edited by vbe; 03-02-2012 at 08:42 AM..
# 4  
Old 03-02-2012
You were trying to read a file called "16" from an inward redirect "<". You also had mismatched brackets in your "if" statement.

Try a numeric compare:

Code:
#!/bin/bash
TIMED=$(date +%H)

if [ ${TIMED} -lt 16 ]; then
     echo Have a great day $USER
else
     echo Have a great night $USER
fi

This User Gave Thanks to methyl For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

2. Shell Programming and Scripting

Difference between exit, bye and quit in sftp

Hi All, I would like to know whether is there any difference in closing the sftp connection with exit, bye and quit. And would like to know the reliable command. (3 Replies)
Discussion started by: Girish19
3 Replies

3. Shell Programming and Scripting

How is use sselect statement o/p in insert statement.

Hi All, I am using Unix ksh script. I need to insert values to a table using the o/p from a slelect statement. Can anybody Help! My script looks like tihs. ---`sqlplus -s username/password@SID << EOF set heading off set feedback off set pages 0 insert into ${TB_NAME}_D... (2 Replies)
Discussion started by: nkosaraju
2 Replies

4. Shell Programming and Scripting

If statement - How to write a null statement

In my ksh script, if the conditions of a if statement are true, then do nothing; otherwise, execute some commands. How do I write the "do nothing" statement in the following example? Example: if (( "$x"="1" && "$y"="a" && "$z"="happy" )) then do nothing else command command fi... (3 Replies)
Discussion started by: april
3 Replies

5. Programming

Zero bye file not deleting

Hi All... The code i have written is if(filename) remove_files (filename); and my remove function is ========================================= void remove_files(char *rf) { if (remove(rf)==0) printf("(%d) Deleting %s\n",getpid(), rf); // JC 10/24/2003 ... (2 Replies)
Discussion started by: arunkumar_mca
2 Replies

6. Shell Programming and Scripting

if statement

can someone please tell me what is wrong with the below. i'm trying to get a script to run if the content of a variable is either small letter y or capital letter Y. if then whatever fi (4 Replies)
Discussion started by: Terrible
4 Replies
Login or Register to Ask a Question