使用 awk,您可以在一个程序中完成所有计算:
awk -F: 'BEGIN {maxuid=0;} {if ($3 > maxuid) maxuid=$3;} END {print maxuid+1;}' /etc/passwd
当您还不想开始使用 awk 时,请对您的代码进行一些反馈。
biggestID=0
# Do not use cat .. but while .. do .. done < input (do not open subshell)
# Use read -r line (so line is taken literally)
cat /etc/passwd | while read line
do
# Do not calculate the uid twice (in test and assignment) but store in var
# uid=$(cut -d: -f3 <<< "${line}")
# Use space after "[" and before "]"
# test is not needed, if [ .. ] already implicit says so
# (I only use test for onelines like "test -f file || errorfunction")
if test [$(echo $line | cut -d: -f3) > $biggestID]
then
biggestID=$(echo $line | cut -d: -f3)
fi
# Next line only for debugging
echo $biggestID
done
# You can use (( biggestID = biggestID + 1 ))
# or (when adding one)
# (( ++biggestID ))
let biggestID=$biggestID+1
# Use double quotes to get the contents literally, and curly brackets
# for a nice style (nothing strang will happen if you add _happy right after the var)
# echo "${biggestID}"
echo $biggestID