为什么这个脚本会出错?
#!/bin/bash
# This script checks whether
# the current user is root or
# not.
if [ $UID -ne 0 ] then
echo "Non-root user."
else
echo "Root user."
Fi
输出
myuser@kali:~$ ./user.sh
./user.sh: line 7: syntax error near unexpected token `else'
./user.sh: line 7: `else'
myuser@kali:~$
答案1
您忘记在和;
之间添加:if
then
if [ "$UID" -ne 0 ]; then
echo "Non-root user."
else
echo "Root user."
fi
条件结构也以, notif
结尾。fi
Fi
;
基本上是换行符的简写。如果您愿意,可以then
在下一行中使用来避免;
:
if [ "$UID" -ne 0 ]
then
echo "Non-root user."
else
echo "Root user."
fi