Bash Job Control, Linux Utilities and Example Scripts
Bash Job Control, Utilities and Example Scripts
The commands you listed are a mix of job-control utilities (fg, jobs, suspend) and standard Linux utilities (df, more).
Here is an explanation of the syntax and purpose for each:
1. Job Control Commands 🛠️
Job control commands are shell built-ins (mostly in Bash, KornShell, etc.) used to manage processes that are currently running in the background or suspended in the current terminal session.
(a) fg (Foreground)
- Syntax:
fg [%job_id] - Purpose: The
fgcommand brings a background job (a process running in the background) or a suspended job (a process currently stopped) back into the foreground. Once in the foreground, the job regains control of the terminal, allowing you to interact with it directly (for example, to provide input). - If no
%job_idis specified,fgoperates on the job most recently put into the background or suspended.
(b) jobs
- Syntax:
jobs [OPTIONS] - Purpose: The
jobscommand lists all currently running or stopped jobs in the current shell session. It displays the job number ([1], [2], etc.), the PID (process ID) and the current status (for example, Running or Stopped).
(c) suspend (or Ctrl+Z)
- Syntax:
suspend [-f] - Purpose: The
suspendcommand (or, more commonly, pressing the key combination Ctrl+Z) sends aSIGTSTPsignal to the current foreground process, causing it to stop its execution immediately. The job is then put into the background in a Stopped state. This allows you to regain control of your shell prompt without terminating the application. - The stopped job can be resumed using
fg(to bring it back to the foreground) orbg(to run it in the background).
2. Standard Linux Utilities 📂
These commands are independent programs used for file-system information and file viewing, not primarily for job control.
(d) df (Disk Free)
- Syntax:
df [OPTIONS] [FILE] - Purpose: The
dfcommand reports the amount of free disk space on file systems. It displays the total size, used space, available space and percentage usage for all mounted file systems. - The common option
-h(human-readable) displays sizes in easy-to-understand units (for example, 10G, 500M).
(e) more
- Syntax:
more [OPTIONS] FILE... - Purpose: The
morecommand is a file-viewing utility (a pager). It displays the contents of a text file one screenful at a time, which is useful for reading large files because it prevents the content from scrolling past the terminal screen too quickly. - The user must press the Spacebar to advance to the next screen or q to quit the viewer. It is often considered less powerful than the modern alternative,
less.
(a) Four Environment Variables in the BASH Shell 🌳
Environment variables are dynamic named values stored within the operating system that affect how processes (including the shell itself) run. They are inherited by child processes.
Here are four commonly used environment variables in the BASH shell:
| Variable | Purpose | Example Value |
|---|---|---|
| HOME | Stores the absolute path to the current user’s home directory. The shell uses this variable when you type cd without arguments or the tilde symbol (~). | /home/user_name |
| PATH | A colon-separated list of directories where the shell looks for executable programs (commands) when you type a command name without specifying the full path. | /usr/local/bin:/usr/bin:/bin |
| SHELL | Stores the absolute path to the shell program the user is currently using or the user’s default login shell, as defined in the /etc/passwd file. | /bin/bash |
| PS1 | Primary Prompt String: stores the format and content of the command prompt displayed in the terminal. You can customize this to show information like username, current directory, hostname, etc. | \u@\h:\w\$ (for example, user@host:~$) |
You can view the value of any environment variable using the echo command:
echo $HOME(b) Running a Shell Script 🚀
A shell script is a text file containing a series of commands for the shell to execute.
1. The Script (Example: hello.sh)
First, create a file named hello.sh and add the following content:
#!/bin/bash
# This is a simple shell script
echo "Hello, Shell Scripting World!"#!/bin/bash (shebang): This is the mandatory first line that tells the operating system which interpreter (in this case, /bin/bash) should be used to execute the rest of the script.
2. Ways to Run a Shell Script
There are two primary ways to run a shell script:
Method 1: Making it Executable (Recommended)
This method requires setting the execute permission on the file and then running it as a command.
- Step 1: Set execute permission
chmod +x hello.sh(The
chmod +xcommand gives the user execute permission on the file.) - Step 2: Execute the script
./hello.sh(The
./ensures the shell executes the file in the current directory, which is necessary if the directory is not in your$PATH.)
Method 2: Calling the Interpreter Directly
This method does not require the script file to have execute permission, since you explicitly tell the shell (interpreter) to read and run the file.
- Step 1: Execute with the shell interpreter
/bin/bash hello.sh # OR (since /bin/bash is usually in the PATH) bash hello.sh(The
bashcommand reads the script and executes its commands.)
(a) Shell Script to Calculate Factorial
This script takes one integer argument from the command line and calculates its factorial using a for loop.
1. Script (factorial.sh)
#!/bin/bash
# Check if an argument was provided
if [ -z "$1" ]; then
echo "Error: Please provide an integer number."
echo "Usage: $0 "
exit 1
fi
# Store the input number and initialize factorial
NUMBER=$1
FACTORIAL=1
# Validate input is a non-negative integer
if ! [[ "$NUMBER" =~ ^[0-9]+$ ]]; then
echo "Error: Input must be a non-negative integer."
exit 1
fi
# Handle base cases
if [ "$NUMBER" -eq 0 ] || [ "$NUMBER" -eq 1 ]; then
echo "Factorial of $NUMBER is 1"
exit 0
fi
# Loop from 1 up to the number
for (( i=1; i<=NUMBER; i++ )); do
# Calculate FACTORIAL = FACTORIAL * i
FACTORIAL=$(( FACTORIAL * i ))
done
echo "The factorial of $NUMBER is: $FACTORIAL"2. How to Run It
- Save the code as a file named
factorial.sh. - Make the script executable:
chmod +x factorial.sh - Run the script with an argument:
./factorial.sh 5
3. Output Example
The factorial of 5 is: 120(b) Shell Script to Display a Multiplication Table
This script takes one integer argument from the command line and displays its multiplication table up to 10.
1. Script (table.sh)
#!/bin/bash
# Check if an argument was provided
if [ -z "$1" ]; then
echo "Error: Please provide an integer number."
echo "Usage: $0 "
exit 1
fi
# Store the input number
NUMBER=$1
# Validate input is an integer
if ! [[ "$NUMBER" =~ ^[0-9]+$ ]]; then
echo "Error: Input must be an integer."
exit 1
fi
echo "Multiplication Table for $NUMBER:"
echo "------------------------------"
# Loop from 1 to 10
for (( i=1; i<=10; i++ )); do
# Calculate the result
RESULT=$(( NUMBER * i ))
# Display the equation in a clean format
printf "%2d x %2d = %3d\n" "$NUMBER" "$i" "$RESULT"
done2. How to Run It
- Save the code as a file named
table.sh. - Make the script executable:
chmod +x table.sh - Run the script with an argument:
./table.sh 7
3. Output Example
Multiplication Table for 7:
------------------------------
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70