为什么不能用getopts函数关闭我的电脑?

问题描述:

已验证sudo shutdown -P可以关闭我的电脑。为什么不能用getopts函数关闭我的电脑?

test(){ 
while getopts "s" arg 
do 
case $arg in 
s) 
    sudo shutdown -P 
    ;; 
esac 
done 
} 

为什么test -s无法关机我的电脑?

不要命名您自己的shell函数(或程序)test。由于test是一个POSIX命令(参见test(1),也输入为[ ....)和一个bash(& bourne shell)builtin,至少会造成混淆(并使您的脚本无法读取)。

我想通过重命名你的函数为test_shutdown它应该工作。