Redirecting Input & Output: Interactive Shell Scripting & User Management
This chapter covers the fundamentals of making shell scripts dynamic and interactive. Instead of hardcoding values, you will learn how to accept real-time user input using the read command, leverage that input to perform system administration tasks (such as dynamically creating system users), manage script execution permissions, and verify system-level changes.
1. The Need for Dynamic Scripts: Hardcoded vs. Interactive Input
In basic shell scripting, values are often hardcoded directly into variables. While this works for static tasks, real-world DevOps automation requires scripts to adapt to varying environments and user inputs.
Without interactivity, if you need to create a new user on a Linux system, you would have to manually edit the script's code every single time. By transitioning to interactive input, you write the logic once, and the script prompts the operator for the necessary data at runtime.
2. The Core Mental Model: Interactive Input via read
The read command acts as an entry gate for data in a shell script. It pauses script execution, waits for the user to type a value via standard input (stdin), and assigns that value to a designated variable once the user presses Enter.
Real-Life Analogy
Think of the
readcommand as a receptionist at an office building. Instead of guessing who you are, the receptionist stops you, asks for your name (the prompt), waits for you to speak, and then writes your name down on a visitor badge (the variable) before letting you proceed.
3. Step-by-Step Evolution: Building the Interactive Script
Let us look at how we build an interactive script from scratch. We start with a simple input-output script and evolve it into a functional system administration utility.
Phase 1: The Naive Input Script
This script uses the read command with the -p (prompt) flag to ask for a username and then echoes it back to the terminal.
#!/bin/bash
# Prompting the user and storing the input in the 'username' variable
read -p "Enter username: " username
# Displaying the captured input
echo "You entered $username"
Phase 2: The Production-Ready User Creation Script
Now, we expand the script to perform a real administrative action: creating a new system user with a home directory using the useradd command.
#!/bin/bash
# 1. Prompt the user for the username
read -p "Enter username: " username
echo "You entered: $username"
# 2. Create the user with a home directory (-m flag)
echo "Creating new user..."
sudo useradd -m "$username"
# 3. Confirm successful execution
echo "New user added successfully!"
4. Executing the Script: Permissions and Execution Methods
Before running a script, you must understand how to execute it and how file permissions affect execution.
Setting Executable Permissions
By default, newly created files do not have execution permissions. You must grant these permissions using the chmod command:
chmod 755 create_user.sh
7(Owner): Read, write, and execute.5(Group): Read and execute.5(Others): Read and execute.
Comparison of Script Execution Methods
5. Automating System Administration: Adding Users Dynamically
The script utilizes the useradd command combined with the -m flag:
sudo useradd -m "$username"
sudo: Elevates privileges to superuser status, which is required because standard users cannot modify system user databases.-m: Instructs the system to automatically create a home directory for the new user under/home/<username>.
6. Verifying System Changes: Inspecting /etc/passwd
Once the script executes and claims "New user added", you must verify that the user was actually created in the Linux system database.
In Linux, all local user accounts are stored in the /etc/passwd file. You can inspect this file using the cat or tail commands.
cat /etc/passwd
To view only the most recently added users, use the tail command:
tail -n 5 /etc/passwd
Understanding the Output Structure
When you inspect /etc/passwd, you will see a line corresponding to your new user (e.g., jetha):
jetha:x:1001:1001::/home/jetha:/bin/bash
This colon-separated line contains critical system information:
jetha: The username.x: Placeholder for the encrypted password (stored securely in/etc/shadow).1001: User ID (UID).1001: Group ID (GID).::: User Info/Gecos field (empty in this case)./home/jetha: The user's home directory (created automatically by the-mflag)./bin/bash: The default login shell assigned to the user.
7. Complete Flow Walkthrough
Below is the step-by-step execution path of the interactive user creation process:
Summary and Key Takeaways
- Interactive Input: The
read -pcommand is the standard way to accept user input interactively in Bash scripts, assigning the input directly to a variable. - Script Execution: Scripts can be run using
bash script.sh(requires read permissions) or./script.sh(requires explicit execute permissions viachmod 755). - User Management: The
useradd -m <username>command creates a new system user along with their corresponding home directory. - Privilege Escalation: Modifying system users requires root privileges, which is why
sudomust prepend theuseraddcommand. - System Verification: The
/etc/passwdfile serves as the single source of truth for local user accounts on a Linux machine. - Automation Power: Combining interactive input with system commands allows DevOps engineers to build safe, reusable, and dynamic utility scripts.