3. Create Simple Shell Scripts
πRed Hat Certified System Administrator (RHCSA β EX200)
In the RHCSA exam, you must know how to write simple Bash shell scripts that accept and process input values from the command line.
When you run a script, you can pass information to it. These values are called positional parameters. They allow a script to work dynamically instead of being hard-coded.
Understanding this topic is very important for the exam.
1. What Are Script Inputs?
When you execute a script, you can pass arguments after the script name.
Example:
./script.sh value1 value2
Here:
value1is the first inputvalue2is the second input
Inside the script, these are accessed using:
$1β First argument$2β Second argument$3β Third argument- and so on
These are called positional parameters.
2. How Positional Parameters Work
If you run:
./userinfo.sh admin /home/admin
Then inside the script:
$1= admin$2= /home/admin
Example script:
#!/bin/bash
echo "Username: $1"
echo "Home Directory: $2"
Output:
Username: admin
Home Directory: /home/admin
This is commonly used in IT environments when writing automation scripts such as:
- Creating users
- Backing up directories
- Managing services
- Changing permissions
3. Important Special Variables
Besides $1, $2, etc., there are other special variables you must know for the exam.
$0 β Script Name
$0 contains the name of the script itself.
Example:
echo "Script name is: $0"
If you run:
./test.sh
Output:
Script name is: ./test.sh
This is useful for:
- Logging
- Displaying usage messages
$# β Number of Arguments
$# shows how many arguments were passed to the script.
Example:
echo "Number of arguments: $#"
If you run:
./test.sh user1 user2
Output:
Number of arguments: 2
This is very important for validation in the exam.
$@ β All Arguments (Separately)
$@ represents all arguments individually.
Example:
echo "All arguments: $@"
If you run:
./test.sh user1 user2
Output:
All arguments: user1 user2
Used mostly in loops.
$* β All Arguments (Single String)
$* also represents all arguments, but treats them as one single string.
For RHCSA, just remember:
$@is safer in loops$*combines everything into one string
4. Checking If Arguments Are Provided (Very Important for Exam)
In the exam, you may need to verify that the user provides required inputs.
Example:
#!/bin/bashif [ $# -lt 1 ]
then
echo "Usage: $0 username"
exit 1
fiecho "Creating user: $1"
Explanation:
-lt 1means less than 1 argument- If no argument is given, show usage message
exit 1stops the script with error
This is common in system administration tasks like:
- User creation scripts
- Service restart scripts
- File backup scripts
5. Example: Create User Script (IT Example)
Example script:
#!/bin/bashif [ $# -ne 1 ]
then
echo "Usage: $0 username"
exit 1
fiuseradd $1
echo "User $1 created successfully"
Run:
./createuser.sh testuser
This uses:
$1β username$#β checks argument count$0β prints script name in usage message
This type of script is very common in Linux server administration.
6. Looping Through Arguments (Important Concept)
Sometimes you need to process multiple inputs.
Example:
#!/bin/bashfor user in "$@"
do
echo "Processing user: $user"
done
If you run:
./script.sh user1 user2 user3
It processes each user one by one.
This is useful for:
- Adding multiple users
- Changing permissions on multiple files
- Restarting multiple services
7. Shifting Arguments (shift Command)
The shift command removes the first argument and moves others left.
Example:
Before shift:
- $1 = user1
- $2 = user2
After shift:
- $1 = user2
Example:
#!/bin/bashwhile [ $# -gt 0 ]
do
echo "Processing: $1"
shift
done
This processes arguments one by one until none remain.
You should understand this for exam scripting tasks.
8. Quoting Arguments (Very Important)
Always use quotes when working with variables:
Correct:
echo "$1"
Wrong:
echo $1
Why?
If input contains spaces:
./script.sh "IT Support Team"
Without quotes, it may break into multiple words.
For exam safety:
Always use "$1" and "$@".
9. Exit Status and Argument Errors
In scripts:
exit 0β successexit 1β error
Example:
if [ $# -eq 0 ]
then
echo "No input provided"
exit 1
fi
RHCSA exam expects proper error handling.
10. Common Exam Mistakes to Avoid
- Forgetting
#!/bin/bashat top - Not making script executable: chmod +x script.sh
- Not checking argument count
- Not quoting variables
- Using wrong test operators (
-eq,-ne,-lt, etc.)
11. Test Operators You Must Know
Inside [ ]:
-eqβ equal-neβ not equal-ltβ less than-gtβ greater than-leβ less or equal-geβ greater or equal
Example:
if [ $# -ne 2 ]
then
echo "Exactly 2 arguments required"
exit 1
fi
12. Real IT Usage in Enterprise Environments
Script inputs are commonly used for:
- Creating users with different usernames
- Assigning different home directories
- Backing up different folders
- Restarting specific services
- Managing file permissions
- Passing log file names
- Automating system configuration tasks
Without positional parameters, scripts would not be flexible.
13. Summary β What You Must Know for RHCSA
For the exam, you must understand:
β What $1, $2, $3 are
β What $0 means
β What $# does
β Difference between $@ and $*
β How to validate argument count
β How to use shift
β Importance of quoting variables
β How to exit with proper status code
If you can:
- Accept input
- Validate input
- Process input correctly
- Handle errors properly
Then you are fully prepared for this section of the RHCSA exam.
