Create Simple Administration Scripts

ShellBeginner
Practice Now

Introduction

Administrators often turn repeated checks into small shell scripts. RHCSA-style scripting is not about writing large programs. It is about using Bash safely enough to automate common tasks, validate input, loop over targets, return useful exit codes, and leave output that another administrator can understand.

In this lab, you will build a directory summary script one piece at a time. The script will accept directory paths, check whether each path is usable, collect basic file information, write readable output, and save the same output to a log file.

Create a Runnable Script

In this step, you will inspect the sample directories and create the first runnable version of an administration script. A script should start with a shebang so the system knows which interpreter to use.

Move into the project directory.

cd /home/labex/project

Inspect the sample tree. The find command lists files and directories, and sort makes the order stable.

find /home/labex/project/script-practice -maxdepth 2 -print | sort

Create the first version of admin-summary.sh. The line #!/bin/bash tells Linux to run the file with Bash. The set -u option makes the script fail if it uses an unset variable, which catches many typing mistakes.

cat > /home/labex/project/admin-summary.sh <<'EOF'
#!/bin/bash
set -u

echo "RHCSA admin summary"
echo "RUN_AS=$(id -un)"
echo "HOST=$(hostname -s)"
EOF

Make the script executable. Without execute permission, the file is only text.

chmod 755 /home/labex/project/admin-summary.sh

Run the script.

/home/labex/project/admin-summary.sh

You should see the script title, the current user, and the short hostname. The exact hostname can vary.

Accept Directory Arguments

In this step, you will add positional parameters. A script uses $# to count arguments and "$@" to represent all arguments exactly as they were passed.

Replace the script with a version that requires at least one directory argument.

cat > /home/labex/project/admin-summary.sh <<'EOF'
#!/bin/bash
set -u

if [ "$#" -eq 0 ]; then
  echo "Usage: $0 DIRECTORY [DIRECTORY...]"
  exit 2
fi

echo "RHCSA admin summary"
echo "RUN_AS=$(id -un)"
echo "HOST=$(hostname -s)"

for target in "$@"; do
  echo "TARGET=$target"
done
EOF

Keep the script executable.

chmod 755 /home/labex/project/admin-summary.sh

Run it with no arguments. The command should print usage text and return exit status 2. The || operator runs the command on the right only when the script on the left fails, which lets you display the nonzero status intentionally.

/home/labex/project/admin-summary.sh || echo "exit status: $?"

Now run it with two directory arguments.

/home/labex/project/admin-summary.sh /home/labex/project/script-practice/app /home/labex/project/script-practice/logs

The output should include one TARGET= line for each argument.

Test Each Target and Return a Useful Status

In this step, you will add a conditional test for each argument. The test [ -d "$target" ] succeeds only when the path exists and is a directory.

Replace the script with a version that reports OK for directories and MISSING for anything else.

cat > /home/labex/project/admin-summary.sh <<'EOF'
#!/bin/bash
set -u

if [ "$#" -eq 0 ]; then
  echo "Usage: $0 DIRECTORY [DIRECTORY...]"
  exit 2
fi

echo "RHCSA admin summary"
echo "RUN_AS=$(id -un)"
echo "HOST=$(hostname -s)"

exit_status=0

for target in "$@"; do
  if [ -d "$target" ]; then
    echo "OK $target"
  else
    echo "MISSING $target"
    exit_status=1
  fi
done

exit "$exit_status"
EOF

Run the script with a valid directory. The final exit status should be 0.

/home/labex/project/admin-summary.sh /home/labex/project/script-practice/app
echo "exit status: $?"

Run it again with a missing directory. This time the script should print MISSING and return exit status 1.

/home/labex/project/admin-summary.sh /home/labex/project/script-practice/missing || echo "exit status: $?"

Collect Practical Directory Facts

In this step, you will add command substitution. Command substitution runs a command inside $() and stores its output in a variable.

Replace the script with a version that reports file count, apparent size, and owner for each valid directory.

cat > /home/labex/project/admin-summary.sh <<'EOF'
#!/bin/bash
set -u

if [ "$#" -eq 0 ]; then
  echo "Usage: $0 DIRECTORY [DIRECTORY...]"
  exit 2
fi

echo "RHCSA admin summary"
echo "RUN_AS=$(id -un)"
echo "HOST=$(hostname -s)"

exit_status=0

for target in "$@"; do
  if [ -d "$target" ]; then
    file_count="$(find "$target" -type f | wc -l)"
    size="$(du -sh "$target" | awk '{print $1}')"
    owner="$(stat -c '%U:%G' "$target")"
    echo "OK $target FILES=$file_count SIZE=$size OWNER=$owner"
  else
    echo "MISSING $target"
    exit_status=1
  fi
done

exit "$exit_status"
EOF

Run the script against the three prepared directories.

/home/labex/project/admin-summary.sh /home/labex/project/script-practice/app /home/labex/project/script-practice/logs /home/labex/project/script-practice/cache

The app directory should show FILES=3, logs should show FILES=2, and cache should show FILES=1.

Save Output to a Log File

In this step, you will add a small helper function and a log file. A function prevents repeated code, and tee -a displays output while appending the same line to a file.

Replace the script with the final version.

cat > /home/labex/project/admin-summary.sh <<'EOF'
#!/bin/bash
set -u

LOG="/home/labex/project/admin-summary.log"
: > "$LOG"

emit() {
  echo "$*" | tee -a "$LOG"
}

if [ "$#" -eq 0 ]; then
  emit "Usage: $0 DIRECTORY [DIRECTORY...]"
  exit 2
fi

emit "RHCSA admin summary"
emit "RUN_AS=$(id -un)"
emit "HOST=$(hostname -s)"

exit_status=0

for target in "$@"; do
  if [ -d "$target" ]; then
    file_count="$(find "$target" -type f | wc -l)"
    size="$(du -sh "$target" | awk '{print $1}')"
    owner="$(stat -c '%U:%G' "$target")"
    emit "OK $target FILES=$file_count SIZE=$size OWNER=$owner"
  else
    emit "MISSING $target"
    exit_status=1
  fi
done

exit "$exit_status"
EOF

Run the final script with two valid directories and one missing directory.

/home/labex/project/admin-summary.sh /home/labex/project/script-practice/app /home/labex/project/script-practice/logs /home/labex/project/script-practice/missing || echo "exit status: $?"

Inspect the saved log.

sed -n '1,8p' /home/labex/project/admin-summary.log

The terminal output and log file should contain the same summary lines. Because one target is missing, the exit status should be 1.

Summary

You created a practical Bash administration script. You used a shebang, executable permissions, positional parameters, argument counts, loops, directory tests, command substitution, explicit exit statuses, a helper function, and a log file. These are the scripting patterns most useful for small RHCSA-style administration tasks.