sh return:只能从函数或源脚本"返回"

kar*_*yan 7 linux bash shell operating-system

return语句错误:urn:只能从shell脚本 myscript.sh中的函数或源脚本"返回"

#!/bin/bash
if [ $# -ne 2 ]
then
    echo "Incorrect Usage : Arguments mismatch."
    return 2
fi

mv $1 $2

return 0
Run Code Online (Sandbox Code Playgroud)

当我试图跑

sh myscript.sh

Incorrect Usage : Arguments mismatch.
myscript.sh.sh: line 5: return: can only `return' from a function or sourced script
Run Code Online (Sandbox Code Playgroud)

如何解决这个错误?

Phi*_*teG 19

我想你的意思是

exit 2
Run Code Online (Sandbox Code Playgroud)

exit 0
Run Code Online (Sandbox Code Playgroud)

另外,再看一下test的语法.

  • 就像 @glenn jackman 对你的问题发表评论一样,你可能已经陷入了 [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)。 (2认同)