LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 03-11-2022, 07:20 AM   #1
rangedweed
LQ Newbie
 
Registered: Mar 2022
Posts: 1

Rep: Reputation: 0
Help Bash script


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
 
Old 03-11-2022, 07:30 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,335
Blog Entries: 3

Rep: Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731
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.
 
1 members found this post helpful.
Old 03-11-2022, 08:08 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,982

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
additionally I would suggest you to use shellcheck to analyze your script.
 
1 members found this post helpful.
Old 03-11-2022, 08:57 AM   #4
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS, Manjaro
Posts: 5,714

Rep: Reputation: 2734Reputation: 2734Reputation: 2734Reputation: 2734Reputation: 2734Reputation: 2734Reputation: 2734Reputation: 2734Reputation: 2734Reputation: 2734Reputation: 2734
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.
 
1 members found this post helpful.
Old 03-11-2022, 10:26 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,335
Blog Entries: 3

Rep: Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731Reputation: 3731
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.
 
Old 03-11-2022, 10:41 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,817

Rep: Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211
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!
 
Old 03-11-2022, 01:00 PM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,712

Rep: Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972
Quote:
Originally Posted by rangedweed View Post
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?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Running bash script from another bash script bulletproof.rs Programming 5 12-10-2017 04:22 AM
[SOLVED] BASH Script - What am I doing wrong in this test? - BASH Script BW-userx Programming 34 04-08-2017 01:36 PM
Variables and Mkvextract in a bash script and a good resource for bash help? gohmifune Linux - General 9 04-13-2011 08:37 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
Bash script to create bash script jag7720 Programming 10 09-10-2007 07:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration