如何入门基于.info文件的Shell脚本开发?用户管理作业求助
Hey there! Let's break this down step by step for your friend—this shell script assignment is a great way to learn core Linux user management, so let's dive in.
user_UserID.info File First, let's build the structured data file your script will use. It’s just a plain text file with comma-separated user details (one user per line). Here’s how to make it:
- Create and open the file with a text editor (nano is beginner-friendly):
nano /tmp/user_UserID.info
- Paste in user entries following the format
Username,Password,PrimaryGroup,Comment,HomeDir. For example, 3 test users:
alice,SecurePass123!,alicegrp,Alice Smith,/home/alice bob,StrongPass456!,bobgrp,Bob Johnson,/home/bob charlie,MyPass789!,charliegrp,Charlie Brown,/home/charlie
Save and exit (press Ctrl+O, hit Enter, then Ctrl+X in nano).
What’s this file for? It’s a reusable data source—instead of hardcoding user details directly in your script, you read from this file to create accounts dynamically. This makes the script easy to update if you need to add more users later.
Now let’s build the script that handles all the heavy lifting: creating groups, users, setting passwords, and generating the required output. Name it create_users.sh:
#!/bin/bash # Define file paths INFO_FILE="/tmp/user_UserID.info" OUTPUT_FILE="userdetails_UserID.txt" # Check if the info file exists first if [ ! -f "$INFO_FILE" ]; then echo "Error: $INFO_FILE not found! Please create it first." exit 1 fi # Clear the output file (or create it if it doesn't exist) > "$OUTPUT_FILE" # Read each line from the info file, splitting fields by commas while IFS=',' read -r username password group comment homedir; do # Skip empty lines to avoid errors if [ -z "$username" ]; then continue fi # Create primary group if it doesn't exist if ! getent group "$group" > /dev/null; then echo "Creating group: $group" groupadd "$group" fi # Create home directory with secure permissions if [ ! -d "$homedir" ]; then echo "Creating home directory: $homedir" mkdir -p "$homedir" chown "$username:$group" "$homedir" chmod 700 "$homedir" # Only the user can read/write/execute here fi # Create user if they don't already exist if ! id -u "$username" > /dev/null 2>&1; then echo "Creating user: $username" useradd -g "$group" -c "$comment" -d "$homedir" "$username" # Set password non-interactively echo "$username:$password" | chpasswd else echo "User $username already exists—skipping." fi # Append user details to the output file echo "=== User Details for $username ===" >> "$OUTPUT_FILE" id "$username" >> "$OUTPUT_FILE" echo "Home Directory: $homedir" >> "$OUTPUT_FILE" echo "Primary Group: $group" >> "$OUTPUT_FILE" echo "" >> "$OUTPUT_FILE" done < "$INFO_FILE" # Display /home directory content as required echo "=== Current content of /home directory ===" ls -l /home/ echo "=== New user details saved to $OUTPUT_FILE ==="
How to run the script:
- Make it executable:
chmod +x create_users.sh
- Run with
sudo(user management requires root privileges):
sudo ./create_users.sh
What the script does:
- Checks for the info file to avoid crashes
- Processes each user line by line, splitting fields with
IFS=',' - Creates missing groups with
groupadd - Sets up home directories with secure permissions (700)
- Creates users with
useradd, linking them to their group and home dir - Sets passwords non-interactively with
chpasswd - Writes user details to the output file and shows
/homecontent
For your friend to build on this, focus on these key shell scripting and Linux admin basics:
- File processing: Master
while readloops andIFSfor parsing structured text - User/group commands: Use
man useradd,man groupadd, andman chpasswdto explore all options—man pages are your best friend! - Conditionals: Learn to check for file/directory existence and user/group presence with
if [ ... ] - Permissions: Understand
chown(owner) andchmod(access rights) for securing user directories
Practice hacks:
- Add
set -xat the top of the script to debug (it prints every command as it runs) - Test with one user first before adding multiple entries
- Always run user management scripts with
sudo—regular users can’t create accounts
内容的提问来源于stack exchange,提问作者User_1191

