Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
然后,在k shell脚本上出现意外错误_Shell_Unix_Ksh - Fatal编程技术网

然后,在k shell脚本上出现意外错误

然后,在k shell脚本上出现意外错误,shell,unix,ksh,Shell,Unix,Ksh,我编写了一个简单的k shell程序,但是我发现了以下错误 “0403-057第11行出现语法错误:'then'不应出现。” 代码是 #!/usr/bin/ksh function check_rm { number=$# #assign number of parameters if [[ $1 -gt 0 ]]; #if RC is success then if[[ "$number" -gt 2 ]]; the

我编写了一个简单的k shell程序,但是我发现了以下错误 “0403-057第11行出现语法错误:'then'不应出现。”

代码是

 #!/usr/bin/ksh
    function check_rm 
   {
  number=$#    #assign number of parameters
if [[ $1 -gt 0 ]]; #if RC is success
    then 


        if[[ "$number" -gt 2 ]];  
        then         

        /usr/bin/mv  $4  $3
        echo " ERROR for $2 " 
    fi


    #user_name=`whoami`
    #mail_body="$3 checked in by $user_name" 
    #echo $mail_body 

else 


        if[[ "$number" -gt 2 ]];  
            then 

            /usr/bin/rm -rf $4
        fi      
        echo "GOOD for $2  "   
        echo "3f was successful"
fi                  
}

有人能帮忙吗?

您可以删除if条件后的“;”。如果条件具有非常严格的语法,我更正了您的代码:

     #!/usr/bin/ksh

    function check_rm 
    {
        #assign number of parameters
        number=$#    
        #if RC is success
        if [[ $1 -gt 0 ]] 
        then 
            if [[ ${number} -gt 2 ]]
            then         
                /usr/bin/mv  $4  $3
                echo " ERROR for $2 " 
            fi


            #user_name=`whoami`
            #mail_body="$3 checked in by $user_name" 
            #echo $mail_body 

        else 
            if [[ ${number} -gt 2 ]]
            then 
                /usr/bin/rm -rf $4
            fi      
            echo "GOOD for $2  "   
            echo "3f was successful"
        fi                  

    }

正如Wrikken在评论中指出的,问题在于
if
[[
;ksh需要这个来告诉这两个是分开的。脚本后面会出现同样的问题;您需要修复这两个问题。

-1:if条件后面的分号是好的,您自己试试。您的更改会在其他地方使代码变得更糟,例如使用
[…]
而不是
[…]
“应该”这是一个错误,我会写“may”…顺便说一句,“if[…]”是“mantest”的首选,所以我无法理解我的建议如何让代码变得最糟糕…如此有建设性的论点…非常感谢!
[
test
的别名,它在Unix系统中通常是一个独立的可执行文件,这就是为什么
mantest
会推荐它的原因。
[…]
由shell本身解释,这意味着它更快(不必调用外部可执行文件)还有一个不那么脆弱的语法。请查看shell本身的手册页(
manbash
manksh
manzsh
,…)谢谢你的建议。我自己看了看,我发现你是对的