Introduction
This documentation provides an overview and examples of various commands and constructs commonly used in Unix/Linux environments for managing processes, manipulating data, and handling conditions.
Commands
df
- The
df
command is used to display disk space usage on Unix/Linux systems. - Example:
df
nproc
- The
nproc
command prints the number of processing units available to the current process. - Example:
nproc
top
- The
top
command displays real-time information about running processes, system load, and CPU usage. - Example:
top
ps
- The
ps
command is used to display information about processes. - Example:
ps -ef
grep
- The
grep
command is used to search for specific patterns within files or output. - Example:
grep "amazon" /path/to/file
curl
- The
curl
command is used to transfer data to or from a server. - Example:
curl https://example.com
find
- The
find
command is used to search for files or directories in a directory hierarchy. - Example:
find / -name "filename"
Special Commands and Constructs
sudo
- The
sudo
command is used to execute commands with elevated privileges. - Example:
sudo find / -name "filename"
set
- The
set
command is used to set shell options. - Example:
set -e # exit the script when error
set -o # debug mode
set -x # debug mode
shell Script :
Certainly! Below are examples demonstrating the usage of set -e
, set -x
, and set -o
.
Example 1: set -e
(Exit on Error)
#!/bin/bash
# Enable exit on error
set -e
# Command that might fail
ls /nonexistent_directory
# This line won't be executed if the previous command fails
echo "This won't be printed"
In this example, if the ls /nonexistent_directory
command fails (since the directory doesn't exist), the script will terminate immediately due to set -e
, preventing the subsequent echo command from executing.
Example 2: set -x
(Debug Mode)
#!/bin/bash
# Enable debug mode
set -x
# Commands to be debugged
echo "Debug mode enabled"
ls -l /etc/passwd
echo "Debug mode disabled"
When this script runs, each command will be printed to the terminal before it is executed, allowing the user to see the commands being run. This is useful for troubleshooting and understanding the flow of execution.
Example 3: set -o
(Options)
#!/bin/bash
# Enable shell options
set -o nounset # Treat unset variables as errors
set -o errexit # Equivalent to set -e, exit on error
# Undefined variable, will trigger an error
echo $undefined_variable
# This line won't be executed due to errexit option
echo "This won't be printed"
In this example, set -o nounset
ensures that using undefined variables will result in an error. Additionally, set -o errexit
is equivalent to set -e
, causing the script to exit immediately if any command fails.
if else
- Conditional statements in Unix/Linux are implemented using the
if else
construct. - Example:
if [expression]; then
command1
command2
else
command3
fi
Advanced Usage
curl
and grep
- Combining
curl
andgrep
can be useful to filter specific content from web responses. - Example:
curl https://example.com | grep "Error"
Pipeline Usage
- Unix/Linux commands can be chained together using pipes (
|
) for more complex operations. - Example:
./tst.sh | grep "1"
Conclusion
This documentation provides a brief overview and examples of common Unix/Linux commands, special constructs, and their usage in various scenarios. Experimenting with these commands and constructs will help users become proficient in Unix/Linux system administration and scripting.
Top comments (0)