This example provides two bash scripts:
1. "backup.sh" - a script to backup a specific directory
2. "system_info.sh" - a script to gather and display basic system information
Script 1: backup.sh
```
#!/bin/bash
# Script to backup a specified directory
# Check if the correct number of arguments are provided
if [ "$#" -ne 2 ]; then
echo "Usage: $0 [source_directory] [destination_directory]"
exit 1
fi
# Assign command line arguments to variables
SOURCE_DIR="$1"
DEST_DIR="$2"
# Create the destination directory if it doesn't exist
mkdir -p "$DEST_DIR"
# Create a timestamp for the backup
TIMESTAMP=$(date +"%Y%m%d%H%M%S")
# Create the backup
tar -czf "${DEST_DIR}/backup_${TIMESTAMP}.tar.gz" "$SOURCE_DIR"
# Print a success message
echo "Backup of $SOURCE_DIR completed successfully. Backup saved at ${DEST_DIR}/backup_${TIMESTAMP}.tar.gz"
```
To use backup.sh, save the contents to a file called "backup.sh" and then use chmod to give it execute permissions:
```
$ chmod +x backup.sh
```
Run the script providing the source and destination directories:
```
$ ./backup.sh /path/to/source/folder /path/to/destination/folder
```
Script 2: system_info.sh
```
#!/bin/bash
# Script to gather and display basic system information
# Gather system information
OS=$(uname -s)
KERNEL=$(uname -r)
CPU=$(lscpu | grep "Model name" | sed 's/^.*: //')
MEMORY=$(free -h | grep "Mem:" | awk '{print $2 " (available: " $7 ")"}')
# Display system information
echo "Operating System: $OS"
echo "Kernel: $KERNEL"
echo "CPU: $CPU"
echo "Memory: $MEMORY"
```
To use system_info.sh, save the contents to a file called "system_info.sh" and then use chmod to give it execute permissions:
```
$ chmod +x system_info.sh
```
Run the script to display system information:
```
$ ./system_info.sh
```
2. Create two Bash shell scripts that will perform some work.
1 answer