【问题标题】:Bash Script : While loop and if statmentBash 脚本:While 循环和 if 语句
【发布时间】: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 if-statement for-loop while-loop conditional


【解决方案1】:
#/bin/bash
fileA="groups.out"
value=1000
num='^[0-9]+$'
while IFS= read lineA
do
    #check if the line is numbers only
    if [[ $lineA =~ $num ]];then
       echo "This is a number"
       echo $lineA
       #check if $line is greater than 1000
       if [[ $lineA -gt $value ]];then
           #write it to file named numbers.out
           echo "number is greater than 1000 writing to file"
           echo $lineA >> numbers.out
       else
           echo "less than, Skipping"
       fi
    #if its not number, its group names right? so no need to check if with regex
    else
       #do what ever u want with group names here ...
       echo "string"
       echo $lineA
    fi
# This is where you feed the file to the while loop
done <  $fileA

这是您的脚本的更正版本。它应该让你去。 chmod +x scriptfile 并使用 bash scriptfile 运行它或在 crontab 中安排它。

由于您关于如何将组名与 gids 匹配的信息不足,我将其留在脚本中,但您应该能够使用脚本其他部分中提供的信息来完成它。

【讨论】:

    【解决方案2】:

    这看起来确实需要两个单独的脚本。使用 Awk 查找特定范围内的数字很简单。

    awk '!/[^0-9]/ && ($1 >= 1000)' groups.out
    

    正则表达式选择全数字输入行(或更准确地说,它排除了其中任何位置包含非数字字符的行),并且数字比较要求第一个字段为 1000 或更多。 (Awk 的默认动作是在脚本中的条件为真时打印整行,因此我们可以省略隐含的 {print} 动作。

    如果您还想将小于 1000 的数字提取到单独的文件中,则更改应该很明显。

    对于非数值,我们可以这样做

    grep '[^0-9]' groups.out |
    xargs getent |
    awk -F : '$3 >= 1000 { print $3 }'
    

    您的伪代码中的几个分支似乎是多余的。不清楚在什么情况下会发生错误,或者您在错误情况下指定的操作将如何帮助您诊断或从错误中恢复(写入 acc ss 被拒绝,磁盘已满?)所以我没有花费任何精力尝试实现这些部分。

    【讨论】:

      猜你喜欢
      • 2014-01-21
      • 2013-06-09
      • 2016-01-12
      • 2012-06-18
      • 2015-05-19
      • 1970-01-01
      • 1970-01-01
      • 2021-09-12
      • 2015-11-08
      相关资源
      最近更新 更多