As a Windows developer transitioning to Linux environments, mastering shell scripting becomes crucial when dealing with deployment automation, system administration, and repetitive tasks. While basic command-line navigation is manageable, true efficiency comes from writing robust scripts that can:
- Automate application deployments
- Configure new server instances
- Handle monthly maintenance tasks
- Process log files and system outputs
Before diving into books, understand these core scripting components:
#!/bin/bash
# Basic deployment script example
APP_NAME="my_application"
INSTALL_DIR="/opt/$APP_NAME"
# Check for root privileges
if [ "$(id -u)" -ne 0 ]; then
echo "This script must be run as root" >&2
exit 1
fi
# Automated installation process
echo "Deploying $APP_NAME to $INSTALL_DIR"
mkdir -p $INSTALL_DIR
cp -r ./app_files/* $INSTALL_DIR/
chmod +x $INSTALL_DIR/bin/*
# Systemd service configuration
cat > /etc/systemd/system/${APP_NAME}.service <
1. "Linux Command Line and Shell Scripting Bible" by Richard Blum
Comprehensive coverage from basics to advanced scripting, including regular expressions and sed/awk.
2. "Classic Shell Scripting" by Arnold Robbins
Focuses on portable scripting techniques that work across Unix-like systems.
3. "Bash Cookbook" by Carl Albing
Practical solutions to real-world problems with ready-to-use code examples.
Here's an enhanced version of a monthly deployment script with error handling:
#!/bin/bash
# Monthly deployment script with logging and rollback
LOG_FILE="/var/log/deployments/$(date +%Y-%m).log"
BACKUP_DIR="/opt/backups/$(date +%Y-%m-%d)"
APP_DIR="/opt/my_app"
exec > >(tee -a "$LOG_FILE") 2>&1
function cleanup {
if [ -f "/tmp/deploy_failed" ]; then
echo "Reverting to previous version..."
rm -rf "$APP_DIR"
mv "$BACKUP_DIR" "$APP_DIR"
systemctl restart my_app
fi
}
trap cleanup EXIT
echo "Starting deployment $(date)"
# Create backup
mkdir -p "$(dirname "$BACKUP_DIR")"
cp -r "$APP_DIR" "$BACKUP_DIR" || { echo "Backup failed"; exit 1; }
# Deployment steps
rsync -avz --delete ./new_version/ "$APP_DIR/" || touch /tmp/deploy_failed
systemctl restart my_app || touch /tmp/deploy_failed
# Verify deployment
curl -s http://localhost:8080/healthcheck | grep -q "OK" || touch /tmp/deploy_failed
if [ -f "/tmp/deploy_failed" ]; then
echo "Deployment failed"
exit 1
else
echo "Deployment successful"
fi
- Process management and job control
- Signal handling and traps
- Parallel execution with xargs or GNU parallel
- Configuration management integration (Ansible/Puppet)
- Secure credential handling
As a Windows-based developer occasionally working with Linux systems, mastering shell scripting becomes crucial when dealing with deployment automation. While basic commands like service management and package installation are straightforward, creating robust deployment scripts requires deeper knowledge.
1. "Linux Command Line and Shell Scripting Bible" by Richard Blum
This comprehensive guide covers everything from basic commands to advanced scripting techniques. The third edition includes modern practices for cloud environments.
#!/bin/bash
# Example deployment script from the book
APP_NAME="myapp"
DEPLOY_DIR="/opt/$APP_NAME"
TEMP_DIR="/tmp/$APP_NAME-build"
# Create directory structure
mkdir -p $DEPLOY_DIR $TEMP_DIR
chown -R www-data:www-data $DEPLOY_DIR
# Download and extract application
wget -O $TEMP_DIR/latest.tar.gz https://example.com/$APP_NAME/latest
tar -xzf $TEMP_DIR/latest.tar.gz -C $TEMP_DIR
# Deploy with version control
TIMESTAMP=$(date +%Y%m%d%H%M%S)
cp -R $TEMP_DIR/* $DEPLOY_DIR/$TIMESTAMP
ln -sfn $DEPLOY_DIR/$TIMESTAMP $DEPLOY_DIR/current
# Cleanup
rm -rf $TEMP_DIR
2. "Classic Shell Scripting" by Arnold Robbins
Focuses on portable scripting techniques that work across different Unix-like systems, perfect for developers working with various Linux distributions.
- Process management and job control
- Regular expressions for log parsing
- Secure remote execution with SSH
- Package management automation
- Configuration file templating
While bash is fundamental, these tools are valuable additions:
# Ansible playbook example for comparison
- hosts: webservers
tasks:
- name: Ensure app is deployed
git:
repo: 'https://github.com/example/myapp.git'
dest: /opt/myapp
version: master
- name: Install dependencies
apt:
name: "{{ item }}"
state: present
with_items:
- python3
- python3-pip
Here's a more complete monthly deployment script:
#!/bin/bash
# Full deployment automation script
# Configuration
APP_REPO="git@github.com:example/production-app.git"
DEPLOY_USER="deploy"
SERVER_LIST="servers.txt"
BACKUP_DIR="/backups/app-$(date +%Y-%m)"
# Initialize logging
LOG_FILE="/var/log/deployments/deploy-$(date +%Y%m%d).log"
exec > >(tee -a "$LOG_FILE") 2>&1
# Main deployment function
deploy_to_server() {
local server=$1
echo "Starting deployment to $server"
ssh -T $DEPLOY_USER@$server <<-EOF
# Server-side commands
mkdir -p $BACKUP_DIR
pg_dump myapp_db > $BACKUP_DIR/db-backup.sql
cd /opt/application
git pull origin master
./bin/install_dependencies.sh
sudo systemctl restart myapp_service
echo "Deployment complete on $server"
EOF
}
# Process all servers
while read server; do
deploy_to_server "$server"
done < $SERVER_LIST
echo "Monthly deployment completed at $(date)"
Beyond books, these resources help:
- Linux Documentation Project (tldp.org)
- ShellCheck static analysis tool
- Bash debugging techniques (set -x, trap)