0

I have to write a bash script for university, the text says:

Write a bash script that allows root user, to get a list of all users of the machine. Selecting a user, using select, will be required to indicate a directory (indicate the absolute path). At this point in the output will have be shown a list of all files folder owned by the user, ranked in ascending order according to the size of file.

To check if the user is root i used:

if[ "$(id -u)" = 0 ]; then

To get the list of users of the machine I was thinking of using awk:

awk -F':' '{ print$1}' /etc/passwd

How can I use select with awk? Is there another way without using awk?

Thank you so much in advance

1
  • You can use cut in much the same way, but I think your problem with the select won't go away by that. The way I interret the statement is that you're supposed to present a list of users, read some input, and then respond to that. Commented Dec 6, 2014 at 3:02

2 Answers 2

2

Here is the way to use awk in select statement, you need finish the rest for your homework (for example, sort the result)

#!/usr/bin/env bash

select user in $(awk -F ":" '{print $1}' /etc/passwd )
do
  read -p "input the absolute directory: " path
  find $path -type f -user "$user" -ls 
done
Sign up to request clarification or add additional context in comments.

Comments

0

Another way to test the UID by arithmetic (smarter!?) is :

if((UID==0)); then
    ...
else
    ...
fi

Check http://wiki.bash-hackers.org/syntax/arith_expr

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.