LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 03-09-2010, 04:46 AM   #1
kickarzt
LQ Newbie
 
Registered: Feb 2010
Posts: 18

Rep: Reputation: 0
Beginner question, Bash-script - date


I get compilationserror on this script, and it doesnt says reallt whats wrong, so can anyone see whats worn in the code? Is it ok to type the first line like that??(date).
Thanks!

[START CODE]
time= 'date +%H:%M'
if [$time -lt 12]; then
txt="Good morning "
elif [$tid -lt -lt 19]; then
txt="Good afternoon "
else
txt="Good evening"
fi
echo $txt $HOST
[END CODE]
 
Old 03-09-2010, 04:50 AM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 552Reputation: 552Reputation: 552Reputation: 552Reputation: 552Reputation: 552
Those should be either `backticks` or $(braces like this) around that date statement; you are currently using 'single quotes'..

Code:
`date`
or
Code:
$(date)
Also, you have two "-lt"'s in one line there, which will give errors. And, depending what exact output is produced by that date FORMAT you are using, you probably/possibly will want to be using "double quotes" around your $VARIABLES.

Cheers!
Sasha

PS - Here's how to use the code tags: http://www.phpbb.com/community/faq.php?mode=bbcode#f2r1
 
Old 03-09-2010, 04:58 AM   #3
kainosnous
Member
 
Registered: Mar 2010
Location: Tennessee, USA
Distribution: Arch, Fedora
Posts: 59

Rep: Reputation: 18
The first problem is that you are attempting to assign $time the literal value 'date +%H:%M' I assume that is not what you want. To get the value of date into time try this:

Code:
time=$(date +%H:%M)
The next thing to note is your "if" statements. The "[" character is actually short for the "test" command and needs to be surrounded by whitespace. The closing "]" should be a separate parameter to test and should also be surrounded by whitespace. For example, try:

Code:
if [ $time -lt 12 ]; then
 
Old 03-09-2010, 05:00 AM   #4
kickarzt
LQ Newbie
 
Registered: Feb 2010
Posts: 18

Original Poster
Rep: Reputation: 0
Thanks, IŽve changed that, but still get errors on line 1, 2 and 4.
-3.2$ ./dateMessage
./dateMessage: line 1: 10:55: command not found
./dateMessage: line 2: [: missing `]'
./dateMessage: line 4: [: missing `]'

And my code looks like this:
TIME= `date '+%H:%M'`
if [$TIME -lt 12]; then
TXT="Good morning "
elif [$TIME -lt 19]; then
TXT="Goof afternoon "
else
TXT="Good evening"
fi
echo $TXT $HOST
 
Old 03-09-2010, 05:07 AM   #5
kickarzt
LQ Newbie
 
Registered: Feb 2010
Posts: 18

Original Poster
Rep: Reputation: 0
I couldnt edit my last post, Ive change line 2 and 4, with white space between, but IŽll still get the same errors..
 
Old 03-09-2010, 05:14 AM   #6
kainosnous
Member
 
Registered: Mar 2010
Location: Tennessee, USA
Distribution: Arch, Fedora
Posts: 59

Rep: Reputation: 18
Are you sure that you get the "same" error? I would expect that you would get an error about [ expecting an integer. Try this code:

Code:
TIME=`date '+%H'`
if [ $TIME -lt 12 ]; then
TXT="Good morning "
elif [ $TIME -lt 19 ]; then
TXT="Good afternoon "
else
TXT="Good evening"
fi
TIME=`date '+%H:%M'` # if you need the time later
echo $TXT $HOST
 
1 members found this post helpful.
Old 03-09-2010, 05:49 AM   #7
kickarzt
LQ Newbie
 
Registered: Feb 2010
Posts: 18

Original Poster
Rep: Reputation: 0
Thanks for not given up the hope on me;-)
I dont know why it didn`t work, but I had problems with running my script edited in gedit, I noticed, so when I wrote the script again with cat command in the terminal window it worked!
I dont know why`s that?

How do I put this script in .bashrc? Please help me with the right commands ;-)
I want it to run everytime I start a new shell.
 
Old 03-09-2010, 06:12 AM   #8
kainosnous
Member
 
Registered: Mar 2010
Location: Tennessee, USA
Distribution: Arch, Fedora
Posts: 59

Rep: Reputation: 18
Some text editors are not plain text editors and they add some markup. However, I didn't think that gedit did. What problems did you have running the script? Personally, I couldn't live without vi as my editor. If you touch type and use the terminal a lot, I would recommend learning it as it is worth the steep learning curve.

To put the script in .bashrc, just open it in a plain text editor and add it at the end. I would recommend making a backup of .bashrc, though. You might look into .bash_profile as it is specifically for login shells.
 
Old 03-09-2010, 06:19 AM   #9
kickarzt
LQ Newbie
 
Registered: Feb 2010
Posts: 18

Original Poster
Rep: Reputation: 0
Ok then, I like to write direct in the terminal, and skip the extern editors, just problems, IŽll try out vi or another intern editor instead. Thanks a lot for your help!
 
Old 03-09-2010, 06:31 AM   #10
kainosnous
Member
 
Registered: Mar 2010
Location: Tennessee, USA
Distribution: Arch, Fedora
Posts: 59

Rep: Reputation: 18
I'm glad we could help.
 
Old 03-09-2010, 06:35 AM   #11
kickarzt
LQ Newbie
 
Registered: Feb 2010
Posts: 18

Original Poster
Rep: Reputation: 0
One more question, IŽve edit .bashrc with emacs; do I have to type the path like #!/bin/bash in .bashrc again?
I have a script there already with #!/bin/bash.
 
Old 03-09-2010, 07:31 AM   #12
kainosnous
Member
 
Registered: Mar 2010
Location: Tennessee, USA
Distribution: Arch, Fedora
Posts: 59

Rep: Reputation: 18
The #!/bin/bash is a special type of comment that only appears as the first line of a script. It is used to tell wich program should process the script. So, that line at the top of .bashrc is like typing /bin/bash .bashrc. Any other line starting with a # will be considered a comment.
 
Old 03-09-2010, 11:37 AM   #13
primerib
Member
 
Registered: Mar 2010
Posts: 48

Rep: Reputation: 20
Quote:
Originally Posted by kickarzt View Post
One more question, IŽve edit .bashrc with emacs; do I have to type the path like #!/bin/bash in .bashrc again?
I have a script there already with #!/bin/bash.
By default it will use /bin/sh, which is a symlink to whatever shell your distro uses. On newer Debian this is dash for example (iirc). #!/bin/<whatever> is not required if /bin/sh is symlinked to the shell you want to use.
 
Old 03-10-2010, 04:18 AM   #14
kickarzt
LQ Newbie
 
Registered: Feb 2010
Posts: 18

Original Poster
Rep: Reputation: 0
IŽwill post in this thread once more ;-)

If I want the scripts in .bashrc to me avalible whereever I stand, not just when I start a new shell, I can put a path in .bashrc?
can I do that? Should it be at the end of the text in .bashrc then?
 
Old 03-10-2010, 11:35 AM   #15
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 552Reputation: 552Reputation: 552Reputation: 552Reputation: 552Reputation: 552
Your above post isn't quite clear, but maybe I can answer it anyway:

1) Yes, you can add stuff to your $PATH from within .bashrc but you should not need to do this; the PATH is set in /etc/profile and perhaps is added to in your ~/.profile file. I think this is not what you want though.

2) If you want the contents of your .bashrc to be in effect (usable) at any given time, simply `source` the file, like so:
Code:
. ~/.bashrc
Do not forget the 'period' at the start of the above command -- that period is the "source" command.

If this does not answer your question, please re-phrase the question so I might better understand what you mean.

Sasha
 
  


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
Get file modification date/time in Bash script cmfarley19 Programming 16 04-15-2015 07:25 PM
how to increment the date in a file name via bash script tensigh Programming 10 07-23-2010 04:49 AM
bash script to delete files / folders based on date and freespace nekawa Linux - Newbie 5 06-08-2009 10:00 PM
Date calculations in BASH script Crashman Programming 4 07-03-2004 11:15 AM
Date in a BASH script... soulsniper Linux - Software 7 11-22-2003 07:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:22 PM.

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