Check if uid numbers in /etc/passwd are in a certain range and not already used, with nested ifs

huangapple go评论79阅读模式
英文:

Check if uid numbers in /etc/passwd are in a certain range and not already used, with nested ifs

问题

我正在尝试检查已经使用并在1000到60000范围内的uid,使用了嵌套的if条件,但只有范围条件起作用。
此外,它打印了$hpass的内容。
有什么改进代码的想法吗?

提前感谢任何建议!

以下是我的代码:

  1. #!/bin/bash
  2. dbpasswd=$(cat /etc/passwd | cut -d ":" -f3 | sort -n)
  3. hpass=$(printf '%d\n' "$dbpasswd") #将所有数字放在单独的行上
  4. while true; do
  5. read -p "输入一个介于1000到59999之间的数字" num
  6. echo ""
  7. if [[ "$num" -gt 1000 && "$num" -lt 60000 ]]; then
  8. if [[ "$num" -eq "$hpass" ]]; then
  9. echo "$num 已经在使用中"
  10. else
  11. echo "$num 在范围内且尚未使用!"
  12. break
  13. fi
  14. else
  15. echo "$num 超出范围"
  16. fi
  17. done
英文:

I am trying to check the uid's that are already used and in within a range from 1000 to 60000, with nested if conditions but only the range condition works.
Also it prints the $hpass content.
Any ideas how to improve the code?

Thanks in advance for any suggestions!

Here is my code:

  1. #!/bin/bash
  2. dbpasswd=$(cat /etc/passwd | cut -d ":" -f3 | sort -n)
  3. hpass=$(printf '%d\n' "$dbpasswd") #to put all numbers on separeate line
  4. while true; do
  5. read -p "Enter a number between 1000 and 59999" num
  6. echo ""
  7. if [[ "$num" -gt 1000 && "$num" -lt 60000 ]]; then
  8. if [[ "$num" -eq "$hpass" ]]; then
  9. echo "$num is already in use"
  10. else
  11. echo "$num is in range and not yet used!"
  12. break
  13. fi
  14. else
  15. echo "$num is out of range"
  16. fi
  17. done

答案1

得分: 2

加载/etc/passwd文件中的UID列表到一个数组中:

  1. uids=()
  2. while read -r uid
  3. do
  4. uids[$uid]="$uid"
  5. done < <(cut -d":" -f3 /etc/passwd)

修改OP的当前代码以测试$num是否是数组中的元素:

  1. while true; do
  2. read -p "输入一个介于1000和59999之间的数字: " num
  3. echo ""
  4. if [[ "$num" -gt 1000 && "$num" -lt 60000 ]]; then
  5. if [[ -n "${uids[num]}" ]]; then
  6. echo "$num 已被使用"
  7. else
  8. echo "$num 在范围内且尚未被使用!"
  9. break
  10. fi
  11. else
  12. echo "$num 超出范围"
  13. fi
  14. done
英文:

Load the list of /etc/passwd uids into an array:

  1. uids=()
  2. while read -r uid
  3. do
  4. uids[$uid]=&quot;$uid&quot;
  5. done &lt; &lt;(cut -d&quot;:&quot; -f3 /etc/passwd)

Modifying OP's current code to test if $num is an element in the array:

  1. while true; do
  2. read -p &quot;Enter a number between 1000 and 59999: &quot; num
  3. echo &quot;&quot;
  4. if [[ &quot;$num&quot; -gt 1000 &amp;&amp; &quot;$num&quot; -lt 60000 ]]; then
  5. if [[ -n &quot;${uids[num]}&quot; ]]; then
  6. echo &quot;$num is already in use&quot;
  7. else
  8. echo &quot;$num is in range and not yet used!&quot;
  9. break
  10. fi
  11. else
  12. echo &quot;$num is out of range&quot;
  13. fi
  14. done

huangapple
  • 本文由 发表于 2023年6月25日 23:29:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551148.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定