UnknownR

Powershell 内 function 的定义与调用问题

  •  
  •   UnknownR · Apr 26, 2019 · 3759 views
    This topic created in 2598 days ago, the information mentioned may be changed or developed.

    问题:

    脚本开头定义了 function,其他地方的调用没问题,但是在某个地方调用就报了 term "<function name="">" is not recognized as a cmdlet, function...的问题</function>

    请问这种情况是会有哪些原因引起的呢?

    Code

    
    function B {}
    
    A $var_01
    
    A $var_02
    
    function C {
    
        $code = {
        	try{
                A $args[0] ([String]::Format("number", $var))
            }
        }
        
        B $code
    }
    
    Supplement 1  ·  Apr 26, 2019
    code 应该是下面这个,帖子里格式出错被吃掉了一部分。

    错误是在 try 里面,报的是 term "A" is not recognized as cmdlet, function..., 但是 A 在其他地方调用的时候都没出问题,实在没有头绪

    ```
    function A {}

    function B {}

    A $var_01

    A $var_02

    function C {

    $code = {
    try{
    A $args[0] ([String]::Format("number", $var))
    }
    }

    B $code
    }
    ```
    3 replies    2019-06-05 18:21:28 +08:00
    geelaw
        1
    geelaw  
       Apr 26, 2019
    你附加的代码的错误在于 try 没有 catch 或 finally
    UnknownR
        2
    UnknownR  
    OP
       Apr 26, 2019
    @geelaw 其实是有的。。。只是这边省略了
    ps1aniuge
        3
    ps1aniuge  
       Jun 5, 2019
    看了你写的脚本,我想起一首歌:生活像一团麻~~~
    你写的函数像一团麻。

    不应该这么写,套来套去。屎代码 ;)

    稍微大点的屎代码,把自己都绕糊涂了。
    看看阿里 java 军规之类的,代码规范吧。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2597 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 10:43 · PVG 18:43 · LAX 03:43 · JFK 06:43
    ♥ Do have faith in what you're doing.