You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何入门基于.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.

Step 1: Creating the 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:

  1. Create and open the file with a text editor (nano is beginner-friendly):
nano /tmp/user_UserID.info
  1. 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.

Step 2: Writing the Shell Script

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:

  1. Make it executable:
chmod +x create_users.sh
  1. 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 /home content
Step 3: Learning Tips & Core Concepts

For your friend to build on this, focus on these key shell scripting and Linux admin basics:

  • File processing: Master while read loops and IFS for parsing structured text
  • User/group commands: Use man useradd, man groupadd, and man chpasswd to 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) and chmod (access rights) for securing user directories

Practice hacks:

  • Add set -x at 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 14:57:54