Process script inputs ($1,Β $2, etc.)

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:

  • value1 is the first input
  • value2 is 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 1 means less than 1 argument
  • If no argument is given, show usage message
  • exit 1 stops 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 β†’ success
  • exit 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

  1. Forgetting #!/bin/bash at top
  2. Not making script executable: chmod +x script.sh
  3. Not checking argument count
  4. Not quoting variables
  5. 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.

Buy Me a Coffee