PDA

View Full Version : Problem with a python script and the date command at a *nix terminal



jamadagni
14th April 2007, 08:46
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:



$ ./datefmt "foo\nymd hm"
+"foo%n%F %R"
$ date +"foo%n%F %R"
foo
2007-03-29 20:03
$ date $(./datefmt "foo\nymd hm")
date: extra operand `%R"'
Try `date --help' for more information.


What is the problem? I am using Konsole from KDE 3.5.6 on Kubuntu Edgy.

wysota
14th April 2007, 09:35
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.

jamadagni
14th April 2007, 09:47
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.

wysota
14th April 2007, 10:01
Try this way:
date "$(./datefmt "foo\nymd hm")"

jamadagni
14th April 2007, 12:59
Now it works. Thanks. What exactly was the problem?

wysota
14th April 2007, 14:18
The argument was treated as two arguments instead of one.

jamadagni
15th April 2007, 02:16
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?

wysota
15th April 2007, 02:39
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)