1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Using date command within an ash script

Discussion in 'Tomato Firmware' started by jsauve, Mar 28, 2009.

  1. jsauve

    jsauve LI Guru Member

    Hi,

    I'm having problems getting a date command to work properly in an ash script. I'm trying to run this in the Init section of my Tomato frimware:

    Code:
    echo "
    #!/bin/ash
    NOWDATE=$(date +%Y%m%d)_$(date +%H%M%S)
    touch /tmp/$NOWDATE.test
    " > /tmp/test.ash
    
    chmod 777 /tmp/test.ash
    
    /tmp/test.ash
    

    The resulting fie just winds up being ".test" after the router has booted.

    If I issue these commands manually at the console:

    Code:
    NOWDATE=$(date +%Y%m%d)_$(date +%H%M%S)
    touch /tmp/$NOWDATE.test
    ...the file gets created just fine, like "20090328_105527.test".

    Why is Init keeping the date function from working properly? :wall:

    Thanks
     
  2. fyellin

    fyellin LI Guru Member

    The problem is that the $ are being expanded in your echo command. When you have
    NOWDATE=.....
    touch /tmp/$NOWDATE.test
    that is being turned into
    NOWDATE=<current time>
    touch /tmp/.test
    Try putting a \ before each of your dollar signs.

    EDIT: 1 minute later.

    Also try using single quotes rather than double quotes. That doesn't expand $.
     
  3. jsauve

    jsauve LI Guru Member

    Cool, thanks for the tip. The backslash helped. I didn't modify my quotes.

    Code:
    echo "
    #!/bin/ash
    NOWDATE=\$(date +%Y%m%d)_\$(date +%H%M%S)
    touch /tmp/\$NOWDATE.test
    " > /tmp/test.ash
    
    chmod 777 /tmp/test.ash
    
    /tmp/test.ash
    BUT, the time is wrong. It's reading the beginning of the Unix epoch as the current time. Is the clock not yet synchronized during the Init? :confused:

    Code:
    -rw-r--r--    1 root     root            0 Dec 31  1969 19691231_180010.test
    If I run the script from the CLI, it's fine. Perhaps I should put this in the WAN startup script instead? :confused:

    thanks in advance...
     
  4. jsauve

    jsauve LI Guru Member

    Got it. I put everything in the WanUp script, and the clock seems to be initialized at that point. Thanks for the syntax help!
     
  5. fyellin

    fyellin LI Guru Member

    You've already figured out the right solution, but. . . .

    The router has no internal clock. It's got no idea what time it is until it asks a time server on the network. For more amusement, look at the first few lines of the log after the router boots.
     
  6. jsauve

    jsauve LI Guru Member

    Cool, thanks for the info!
     
  7. i1135t

    i1135t Network Guru Member

    This script does not work for me. I had an issue with Victek' mod where I enable ARP Binding, and reboot my router, my NTP does not update automatically on it's own. I was hoping this would fix that problem, but I guess not.

    Is there a command I could put in the WAN up script to run that function to update the date & time? I would like to have the ARP Binding capability enabled and still update the date and time, on its own. Thanks!
     

Share This Page