Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I would really like some help with this assignment from my teacher
# Loop through all the usernames supplied as arguments.
# Make sure the UID of the account is at least 1000. Otherwise, exit with status 1
for "$@" in `awk -F: /etc/passwd`
do
if (("$@" >= 999));
then
echo "$@ UID OK!"
else
echo "$@ UID not ok"
fi
done
# Compress the user's home directory (if it exists) and move it into the ARCHIVE_DIR
# You have to exit with status 1 if the command fails for some reason.
# ...
# Delete the user and her/his home directory.
# Check to see if the userdel command succeeded.
# We don't want to tell the user that an account was deleted when it hasn't been.
# ...
# If everything was ok, display a success message, e.g: "The user account XXX has been succesfully deleted."
# ...
# Finally, we can exit with status 0 (i.e. success)
exit 0
Welcome. Verbatim posting of homework is not allowed, you should describe the task in your own words. However, the few lines of script you have posted are good to have. I would recommend that rather than trying a mixture of shell script and AWK script, you can do it all in AWK. The formula would be like this:
Code:
awk -F: '...something...' /etc/passwd
Please check Bruce Barnett's AWK Guide and pay special attention to the basic structure, the input Field Separator variable, and the other special variables such as $1 and $3. The basic structure of AWK is abbreviated if-then statements. The Field Separator is set by the variable FS or, as you have it, with the -F option. The -v option can be used to pass environment variables, such as $@, into AWK.
In addition to Bruce Barnett's guide, see also the reference manual 'man awk' for the details specific to the version you have on your system.
As a professional educator I must take exception with the suggestion to do it all in AWK. AWK is a GREAT tool, but clearly the point of the exercise is to clarify the BASH concepts and skills: which purpose would NOT be served by using AWK!
This is certainly something that BASH can do, and you are clearly not restricted to using only the built-in functions. It is important to do what BASH can do natively in BASH, and know exactly what external applications you are calling and why, and testing the results for unexpected faults or behavior.
When you show us code we can advise on faults or improvements, but no one here can LEARN for you. You are going to have to make a start and ask questions only when you get "stumped" or run into an issue.
Part of education is designing exercises that fit the goals and methods. That is one of the reasons it takes around 9:1 ratio of preparation time to lecture hours for a well-designed course the first time around, a rate many institutions refuse to pay and try short change both the educator and the students. Haste makes waste in cases like that.
Using the right tool for the job is an important rule. If the goal were to focus on the shell, or even a specific shell like Bash, then that part would have been written quite differently. The task as described was rather clearly much more appropriate for AWK or another language other than Bash. Though it could be shoehorned into Bash, Lisp, or even C++ if necessary, I would not recommend it. Using pure bash would be rather complicated and involve read and parameter substitution. Anyway, let's hear more about the actual constraints for that part of the assignment and the other parts listed above and find out how free form the answers can be or if the instructor had expected or hinted at specific approaches.
Edit: addressing only the first task there, the others are likely to need a different approach, such as ... Bash.
Last edited by Turbocapitalist; 03-11-2022 at 10:31 AM.
You compare two lists.
One is "$@" and one is from passwd.
But a loop can only take one list; syntax is
for varname in list
where varname gets a cyclic assignment from the list.
In general you need two nested loops to compare each element from list1 with each element from list2.
Sometimes specific commands can save one loop.
Perhaps your OS has a getent command?
Consult its man page, and check it out at the command line!
I would really like some help with this assignment from my teacher
Code:
# Loop through all the usernames supplied as arguments.
# Make sure the UID of the account is at least 1000. Otherwise, exit with status 1
for "$@" in `awk -F: /etc/passwd`
do
if (("$@" >= 999));
then
echo "$@ UID OK!"
else
echo "$@ UID not ok"
fi
done
# Compress the user's home directory (if it exists) and move it into the ARCHIVE_DIR
# You have to exit with status 1 if the command fails for some reason.
# ...
# Delete the user and her/his home directory.
# Check to see if the userdel command succeeded.
# We don't want to tell the user that an account was deleted when it hasn't been.
# ...
# If everything was ok, display a success message, e.g: "The user account XXX has been succesfully deleted."
# ...
# Finally, we can exit with status 0 (i.e. success)
exit 0
Ok, what is the 'help' that you're needing, exactly??? Seems like you've attempted to check the user ID, but instead have written a loop (??). The rest of your homework is totally blank, and you didn't attempt any of it.
We are happy to help you, but you need to show your own efforts. Look carefully at what you're doing, too...you're starting a loop, then exiting the loop without actually doing anything with the information provided, are you?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.