0

I am trying to find the UID in a certain range and only showing those UIDs with their name. The range is determined by two arguments I enter in my command line. However, I don't receive the right answer in the code I am using now. Below, the first command shows running the code I am using now. In the second command, you will see the answer that I should have.

$ ./UserID 10 100
root:0
$ ./UserID 10 100
operator: 11
games: 12
ftp: 14
dbus: 81
apache: 48
tss:59
avahi: 70
грс:32
rpcuser: 29
gdm: 42
sshd: 74
tcpdump: 72
#! /bin/bash

min=$1
max=$2

awk -F: '($3>=min) && ($3<=max ) {printf "%s:%s\n",$1,$3}' /etc/passwd
Kusalananda
  • 320,670
  • 36
  • 633
  • 936
Freddy
  • 1
  • 1
  • 1
    You haven't actually used the `min` or `max` variables anywhere after defining them. – muru Nov 15 '22 at 09:24
  • 1
    Your problem description "*I don't recieve the right answer*" is not clear. Please [edit] your question and copy&paste some example input and the actual output you get and show the expected output. – Bodo Nov 15 '22 at 09:33
  • 1
    Does the stackoverflow question ["How do I use shell variables in an awk script?"](https://stackoverflow.com/questions/19075671/how-do-i-use-shell-variables-in-an-awk-script) solve your problem? – Gordon Davisson Nov 15 '22 at 09:53
  • `awk` cannot see shell variables -- they use different name spaces. Pass the shell values into awk like: `awk -v min="${min}" -v max="${max}" -F: ....`. Your image clips off the command -- I assume you are reading /etc/passwd. Don't post images -- please add the data or code as a code block. – Paul_Pedant Nov 15 '22 at 09:55

1 Answers1

2

Like this:

#!/bin/bash

min=$1
max=$2

awk -F: -v min="$min" -v max="$max" '($3>=min) && ($3<=max) {printf "%s:%s\n",$1,$3}' /etc/passwd
Kusalananda
  • 320,670
  • 36
  • 633
  • 936
Gilles Quénot
  • 31,569
  • 7
  • 64
  • 82