Posted Dec 8, 2011 21:48 UTC (Thu) by quotemstr (subscriber, #45331)
[Link]
> That's just the way Windows console behaves. Arguments are not splitted if they are quoted - it's different from Linux, but it's consistent.
No, PowerShell is not being consistent with cmd here. PowerShell in fact tries to quote arguments (so they will be properly decoded and sent to [w]main), but fails in some glaring cases. Here is a case that *does* work:
PS> $foo="hello world"
PS> whoami $foo bar
ERROR: Invalid argument/option - 'hello world'.
Type "WHOAMI /?" for usage.
Here, you see that whoami (which prints its first argument, and so is useful as a testing tool) prints *just* "hello world", not "hello world bar".
Please, learn how the Windows command line argument system works before attempting to defend it.
Evolution of shells in Linux (developerWorks)
Posted Dec 8, 2011 22:09 UTC (Thu) by Cyberax (✭ supporter ✭, #52523)
[Link]
You can do it like this: 'whoami @($c -split " ")'. If you need this often then define a function.
Evolution of shells in Linux (developerWorks)
Posted Dec 8, 2011 22:26 UTC (Thu) by quotemstr (subscriber, #45331)
[Link]
> whoami @($c -split " ")
No. Splatting does _NOTHING_ to address the quoting issue I raised. It does not allow you to pass arbitrary argument words to external programs. Your technique actually makes the problem _WORSE_ because you lose the identity of individual arguments which CAN contain spaces_and which you DO need to quote. You've utterly failed to even comprehend my basic point. This conversation is over.
Evolution of shells in Linux (developerWorks)
Posted Dec 8, 2011 22:28 UTC (Thu) by Cyberax (✭ supporter ✭, #52523)
[Link]
Can you show me what EXACTLY you are trying to do? Paste a piece of code here, including the parts which receive parameters.
Evolution of shells in Linux (developerWorks)
Posted Dec 9, 2011 12:25 UTC (Fri) by nix (subscriber, #2304)
[Link]
quotemstr is trying to show you faults in the Windows command-line parsing system by using whoami as a tool to indicate the contents of the first argument.
You are treating it as if he wants to run 'whoami' but doesn't know how.
When a finger points at the moon...
Evolution of shells in Linux (developerWorks)
Posted Dec 10, 2011 3:00 UTC (Sat) by Cyberax (✭ supporter ✭, #52523)
[Link]
I understand that 'whoami' is used as an example.
I finally understood what author wants and gave an answer: "@($var -split ' ')". It does the braindead thing that was requested.
Why braindead? Because good PowerShell scripts would just use an _array_ and 'splat' it when required, instead of constructing shell arguments as raw strings.
And no, this hack with splitting string into arguments is definitely not required to work with legacy code.
Evolution of shells in Linux (developerWorks)
Posted Dec 10, 2011 3:01 UTC (Sat) by Cyberax (✭ supporter ✭, #52523)
[Link]
Try it. It does EXACTLY what you've requested - passes a string as a number of arguments, split by spaces.
Evolution of shells in Linux (developerWorks)
Posted Dec 16, 2011 3:33 UTC (Fri) by useerup (guest, #81854)
[Link]
You err because you want PowerShell to act exactly the way POSIX shells do. PowerShell is actually a bit more structured and it may require you to do things in a little different way. The advantage is that many of the error-prone constructs in bash are avoided.
For your example try this:
PS>$foo="/groups","/user"
PS>whoami $foo
And then try
PS>whoami $foo[0]
PS>whoami $foo[1]
PowerShell actually integrates quite nicely with external programs; it does so even retaining (not re-interpreting) arguments.
In your example you told PS to pass a string containing "arg1 arg2" to an external program - which it did. You assume that the entire commandline is turned into text and re-interpreted (the way of POSIX shells).
As you can see above, PowerShell understands arrays and will readily pass the array items as discrete arguments. It's not harder - just a little different and a lot more robust and avoids the risk of injection vulnerabilities.