LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Problem with loading date into variable (https://www.linuxquestions.org/questions/linux-newbie-8/problem-with-loading-date-into-variable-878027/)

boumphreyfr 04-30-2011 01:00 PM

Problem with loading date into variable
 
I have the following code:
Code:

#!/bin/bash
        mydate= date
        echo $mydate
        echo $mydate
        echo $mydate
        date
exit

The expected result is to see the date four times.
Instead I get:
Code:

Sat Apr 30 13:41:08 EDT 2011


Sat Apr 30 13:41:08 EDT 2011


Can anyone tell me the reason for this and give me a work around
Thanks Frank

Nylex 04-30-2011 01:02 PM

First of all, you need to remove the space after the =. Secondly, you need to tell Bash to run the command and store the output in the variable. What you're attempting to do is store the text "date" in the variable. What you want to do is the following:

mydate=$(date)

You might want to read more about Bash scripting. A good guide can be found here.

Nylex 05-04-2011 01:18 AM

Are you still having problems with this?

boumphreyfr 05-07-2011 10:50 PM

Sorry for the delay in replying.
Thanks for your Answer. The reference you sent is invaluable. Can you recommend a good 'dead tree' book? I'm marking it solved.
Problem is I'm very new to Bash scripting, and I'm trying to convert my old C & PHP programs into Bash. I know what I need to do, but I'm having problems with the mechanics.

Frank

Nylex 05-08-2011 12:29 PM

Cool. Unfortunately, I (personally) am unable to recommend a book on Bash as I only really learnt from the guide I provided a link to. Maybe check out some of the books in the bibliography of said guide, or perhaps Learning the bash Shell. There are probably other good books, too.

Out of interest, though, why are you converting C and PHP programs to Bash?

catkin 05-08-2011 01:27 PM

Quote:

Originally Posted by boumphreyfr (Post 4349698)
Can you recommend a good 'dead tree' book?

AFAIK there isn't one; we have moved into the online age and documentation is mostly on the web. There's a list of shell scripting links here and further intros here and here. Bash keeps on getting better (or more bloated, depending on your PoV) so beware that older documentation does not cover newer features. Once you are up to speed (and maybe before) you may find GNU's bash reference useful.

chrism01 05-09-2011 12:48 AM

See also
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

catkin 05-09-2011 02:15 AM

Another list of tutorials here, with reviews.

AnanthaP 05-09-2011 06:38 AM

Dead tree books. These ar every necessary in India (where I come from). Sometimes you commute upto two hours a daya - daily. So leaves little time for using the computer (and most people cant afford laptops).

"The UNIX programming environment" by Kernigan and Pyke.

OK

catkin 05-09-2011 07:48 AM

Quote:

Originally Posted by AnanthaP (Post 4350837)
Dead tree books. These ar every necessary in India (where I come from). Sometimes you commute upto two hours a daya - daily. So leaves little time for using the computer (and most people cant afford laptops).

"The UNIX programming environment" by Kernigan and Pyke.

OK

If the demand is there then some enterprising person (no shortage of those in India!) will produce them. Alternatively you could download the online pages and print them onto dead trees yourself ...

boumphreyfr 05-09-2011 01:27 PM

Nylex wrote:
Quote:

Out of interest, though, why are you converting C and PHP programs to Bash?
In order to convert formatted text files, e.g. guttenberg.org, into well formatted XML, Kindle or HTML on the fly. Currently they have to be down loaded converted with an app and uploaded to the server.

Thanks to the others for the many online references.

Like the commuter from India, I do a lot of traveling where online is not available.

Frank


All times are GMT -5. The time now is 03:07 PM.