【发布时间】:2017-01-15 21:59:00
【问题描述】:
我正在编写一个脚本来过滤大于 1000 和小于等于 1000 的 GID。目的是从文件中过滤掉本地组和非本地组(来自 AD 的组)..
有一个名为 groups.out 的文件,其中包含组名和 GID。它可以是任何顺序。下面是包含本地组、非本地组和 GID 的示例文件。
1098052
1098051
domain users
fuse
gdm
haldaemon
这是我要应用的逻辑
Read line by line from the file,
if the line is a number then check
if number greater than or equal to 1000 then check
if greater than or equal to 1000, append it to the file
else if number less than 1000 then dump it
else if erorr occurs append error to file and break the loop and exit
if the line is a string then check the gid of the string/group
if number greater than or equal to 1000 then append to file
else if gid less than 1000 then dump it
else if error occurs append error to file and break the loop and exit
want to repeat it in the loop line by line and if anywhere the error occurs loop should break and exit the entire script
成功执行循环后,它应该打印成功,或者如果发生任何错误,它应该退出并将错误附加到文件中。 下面是我的未煮过的代码,缺少许多部分。 gt 或 eq 错误也存在许多错误。所以你可以忽略它
fileA="groups.out"
value=1000
re='[a-z]'
num='[0-9]'
while IFS= read lineA
do
group=$(getent group "$lineA" | awk -F: '{print $3}')
# ------Don't know how to check if a number or string -----
if [ "$group" -gt "$value" ]; then
echo "$lineA" >> ldapgroups.out 2>> error.out
elif [ "$group" -lt "$value" ]; then
echo "$lineA" >> /dev/null 2>> error.out
else
echo " FAILED"
exit 1
fi
【问题讨论】:
-
这里有一个可能的解决方案来检查 bash stackoverflow.com/a/806923/524743 中的数字或字符串是否是数字或字符串
标签: bash if-statement for-loop while-loop conditional