Next Generation Emulation banner
1 - 4 of 4 Posts

·
No$gba Philosopher
Joined
·
548 Posts
Discussion Starter · #1 · (Edited)
I'm hoping this is the alternative place to ask for Linux programming help here. Unless not being sure, please move this thread to the right section, in case there is a subforum dedicated to Linux programming.

I'm trying to write a script in Linux, which uses the bash shell, but I can't seem to create the solution to it.

The homework problem is that I need to find the most recent (newest) account created in Linux. The only way I can think of is to use the user ID code, and pick the largest number between 1000 and 2000.

I can't figure out how to get the user ID from reading across many lines in /etc/passwd.

So far, it's like this:

Code:
while read line
do
        acct='echo $line | cut -f1 -d:'
        if [ "$line" > 1000 && "$line" < 2000 ]; then
               grep $line | cut -d ':' -f 3
        fi
done
Am I doing this correctly? Or I am doing it the wrong way...?

Thanks in advance.
 

·
Premium Member
Joined
·
17,148 Posts
I don´t know much about linux nor scripting on it but i do understand somehow the code you put there.

as far as i can recognize the $line holds your value and by using the "IF" you´re checking if the "$line" contains a number from 1001 to 1999 is that correct??? btw you´re using "" on your variable... does it means is a string??? because comparing string to integers can be a problem in some languages while in others not such as php etc...

since you´re looping through it you are checking the value constantly which is great but...

1. if you´re looping and you want to get the highest value you could get from your variable from 1001 to 1999 then you will have to store the value into a temp variable and always check if the new value is higher than the actual one on your temp variable.

2. in case you just want to get the first value from 1001 to 1999 then am missing a command to exit the loop. otherwise the value is going to be overwriten by the new one. and if you´re printing the value then its going display a new value each time.

3. if you want to check all the values of the $line that are from 1001 to 1999 then its fine the way it is i think.

again am not a linux expert nor have any linux scripting knowledge but as a coder i´ve seen several logical problems you could face with that code in case you want to do something similar as showed in my first 2 points.

hope it helps. :thumb:
 

·
No$gba Philosopher
Joined
·
548 Posts
Discussion Starter · #3 ·
Hm...It did gave me some ideas I wanted to try it out on, but they have to wait until I can figure out how to get the user ID from reading across many lines in /etc/passwd.

That's the main problem right there, just don't know if my first post actually gives the picture out.
 

·
No$gba Philosopher
Joined
·
548 Posts
Discussion Starter · #4 ·
Found 4/5 of the solution to the above problem I was having.

It's a new topic.

I learned how to do it, there are a few newly created accounts just created, with their user ID set to 1001, 1002, and 1003 , and I'm just one missing problem:

u2.sh:
Code:
#! /bin/bash

while read line
do
     userdel -f "$line"
done

u1.sh:
Code:
grep -n "1[0-9][0-9][0-9]" /etc/passwd | cut -f2 -d:
Command:
Code:
$ ./u2.sh < ./u1.sh
The only problem is that the output is: Those newly created account doesn't exist.

What should I do after that? Thanks in advance, especially @ruantec, thanks to you for the ideas.
 
1 - 4 of 4 Posts
This is an older thread, you may not receive a response, and could be reviving an old thread. Please consider creating a new thread.
Top