Results 1 to 8 of 8

Thread: Problem with a python script and the date command at a *nix terminal

  1. #1
    Join Date
    Jan 2006
    Location
    11 N 78 E
    Posts
    110
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Problem with a python script and the date command at a *nix terminal

    I will attach a Python script I wrote to make my using the date command easier. But it does not appear to work perfectly. Please see the following session transcript:

    Qt Code:
    1. $ ./datefmt "foo\nymd hm"
    2. +"foo%n%F %R"
    3. $ date +"foo%n%F %R"
    4. foo
    5. 2007-03-29 20:03
    6. $ date $(./datefmt "foo\nymd hm")
    7. date: extra operand `%R"'
    8. Try `date --help' for more information.
    To copy to clipboard, switch view to plain text mode 

    What is the problem? I am using Konsole from KDE 3.5.6 on Kubuntu Edgy.
    Penguin #395953 using Qt for open-source development on X11 using C++ and
    Python via PyQt

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with a python script and the date command at a *nix terminal

    I think you forgot to attach the script. Anyway the problem seems to be that a "%R" formatting token is passed to date and it doesn't know how to handle it.

  3. #3
    Join Date
    Jan 2006
    Location
    11 N 78 E
    Posts
    110
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with a python script and the date command at a *nix terminal

    Sorry, now I have attached the script. man:date tells me that %R is recognized as a valid input for the format and date +%R gives me meaningful output.
    Attached Files Attached Files
    Penguin #395953 using Qt for open-source development on X11 using C++ and
    Python via PyQt

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with a python script and the date command at a *nix terminal

    Try this way:
    date "$(./datefmt "foo\nymd hm")"

  5. #5
    Join Date
    Jan 2006
    Location
    11 N 78 E
    Posts
    110
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with a python script and the date command at a *nix terminal

    Now it works. Thanks. What exactly was the problem?
    Penguin #395953 using Qt for open-source development on X11 using C++ and
    Python via PyQt

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with a python script and the date command at a *nix terminal

    The argument was treated as two arguments instead of one.

  7. #7
    Join Date
    Jan 2006
    Location
    11 N 78 E
    Posts
    110
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with a python script and the date command at a *nix terminal

    Quote Originally Posted by wysota View Post
    The argument was treated as two arguments instead of one.
    Why? I have added quotes in my script, as you see. And if I try out just ./datefmt I do get the quotes in the output. If I manually input the output of datefmt into date I get the desired behaviour. Why is it that if I pass it directly using $() I get unexpeted behaviour?
    Penguin #395953 using Qt for open-source development on X11 using C++ and
    Python via PyQt

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with a python script and the date command at a *nix terminal

    Quote Originally Posted by jamadagni View Post
    Why?
    I have no idea.

    I have added quotes in my script, as you see.
    I guess the quotes were treated as part of the argument and not as a "whitespace escaper" (sorry, can't find a better name for it at this hour). This may have something to do with how the $() construction works. I have never seen it before, so I can't say.

    Why is it that if I pass it directly using $() I get unexpeted behaviour?
    Treat it as you would have passed:

    ./datefmt.py +\"foo%n%F %R\" (quotes get escaped)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.