Let’s explore the basic commands and syntax in shell scripting, focusing on running commands
in the shell, understanding command-line arguments, and using quotes effectively. Each section
will include use cases and examples to provide clarity.
Basic Commands and Syntax
1. Running Commands in the Shell
Commands in the shell are executed by typing them in the command line and pressing Enter.
You can run built-in commands, scripts, or executables.
Example: Running Commands
To display the current working directory:
bash
Copy code
pwd
To list files in the current directory:
bash
Copy code
ls -l
Use Case:
System Monitoring: You can use commands like top, df, and free to monitor system
performance directly from the shell.
Script Example: Here’s a simple script that checks disk usage and prints a message:
bash
Copy code
#!/bin/bash
# Check disk usage
USAGE=$(df -h / | grep / | awk '{ print $5 }')
echo "Current disk usage: $USAGE"
This script uses the df command to check disk usage and then prints it to the terminal.
2. Understanding Command-Line Arguments
Command-line arguments are values passed to a script when it is executed. They are accessed
within the script using special variables:
$0: The name of the script.
$1, $2, $3, etc.: The first, second, third, etc., arguments passed to the script.
Example: Accessing Command-Line Arguments Create a script named [Link]:
bash
Copy code
#!/bin/bash
# A simple greeting script
echo "Script name: $0"
echo "First argument: $1"
echo "Second argument: $2"
Use Case:
Dynamic Input: This script can be used to accept user input for personalized greetings.
For instance, you can run it as follows:
bash
Copy code
./[Link] "Alice" "Developer"
Output:
yaml
Copy code
Script name: ./[Link]
First argument: Alice
Second argument: Developer
3. Using Quotes in Scripts
Quotes are essential in shell scripting for handling strings and variables correctly. There are three
types of quotes: single quotes ('), double quotes ("), and backticks (`).
Single Quotes ('): Preserve the literal value of each character within the quotes.
Double Quotes ("): Preserve most characters, but allow for variable expansion and
command substitution.
Backticks (`): Used for command substitution, allowing the output of a command to
replace the command itself (though $(command) is preferred for clarity).
Example of Using Quotes
bash
Copy code
#!/bin/bash
# Demonstrating quotes
VAR="World"
echo 'Hello, $VAR' # Output: Hello, $VAR (literal)
echo "Hello, $VAR" # Output: Hello, World (variable expansion)
echo "Current date: $(date)" # Output: Current date: [current date]
Use Case:
String Manipulation: Quotes are used extensively in scripts to handle user input,
manage string formatting, and execute commands with specific outputs.
Complete Example: Putting It All Together
Let’s create a script that combines these elements: running commands, using command-line
arguments, and managing quotes.
bash
Copy code
#!/bin/bash
# A script to greet the user and show current system time and disk usage
if [ $# -ne 2 ]; then
echo "Usage: $0 <name> <role>"
exit 1
fi
NAME=$1
ROLE=$2
CURRENT_TIME=$(date)
DISK_USAGE=$(df -h / | grep / | awk '{ print $5 }')
echo "Hello, $NAME! You are a $ROLE."
echo "Current time: $CURRENT_TIME"
echo "Current disk usage: $DISK_USAGE"
Running the Script
1. Save the script as greet_system.sh.
2. Make it executable:
bash
Copy code
chmod +x greet_system.sh
3. Execute the script with arguments:
bash
Copy code
./greet_system.sh "Bob" "Admin"
Output Example:
sql
Copy code
Hello, Bob! You are a Admin.
Current time: Sat Nov 3 14:05:02 UTC 2024
Current disk usage: 45%
Summary
Running commands in the shell allows you to interact with the system and execute
tasks efficiently.
Command-line arguments enable scripts to receive input from users, making them more
dynamic and reusable.
Using quotes correctly is crucial for string handling, variable expansion, and command
execution within scripts.
By understanding these fundamental concepts, you will be well-equipped to write more complex
shell scripts and automate a variety of tasks effectively.