Bash Scripts

Bash Scripts

Basics of Bash Scripting

Creating a Bash Script

!touch script.sh

Make the script executable:

!chmod +x script.sh

Open the file in a text editor and start with the shebang (#!)

Tells the system to use bash to execute the script:

!echo '#!/bin/bash' > script.sh
!cat script.sh
#!/bin/bash

Add commands to your script:

!echo 'echo "Hello, World!"' >> script.sh

Run the script:

!./script.sh
Hello, World!

Variables

#!/bin/bash
name="John"
echo "Hello, $name!"

Conditionals

#!/bin/bash
if [ "$1" -gt 10 ]; then
  echo "The number is greater than 10"
else
  echo "The number is 10 or less"
fi

Loops

#!/bin/bash
for i in {1..5}; do
  echo "Iteration $i"
done

Functions

#!/bin/bash
greet() {
  echo "Hello, $1!"
}

greet "Alice"
greet "Bob"

Command Line Arguments

#!/bin/bash
echo "First argument: $1"
echo "Second argument: $2"

Run the script with arguments:

./script.sh arg1 arg2

Error Handling

#!/bin/bash
set -e  # Exit on error

trap 'echo "Error occurred on line $LINENO"' ERR

# Command that might fail
cp non_existent_file.txt destination/

Examples

Update and Upgrade System

#!/bin/bash

sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get dist-upgrade -y
sudo apt-get autoremove -y

Example Script for Setting Up a Django Project with PostgreSQL

#!/bin/bash

# Variables
PROJECT_NAME="myproject"
DB_NAME="mydatabase"
DB_USER="myuser"
DB_PASSWORD="mypassword"

# Update and install dependencies
sudo apt-get update
sudo apt-get install -y python3-pip python3-dev libpq-dev postgresql postgresql-contrib nginx curl

# Set up PostgreSQL
sudo -u postgres psql <<EOF
CREATE DATABASE $DB_NAME;
CREATE USER $DB_USER WITH PASSWORD '$DB_PASSWORD';
ALTER ROLE $DB_USER SET client_encoding TO 'utf8';
ALTER ROLE $DB_USER SET default_transaction_isolation TO 'read committed';
ALTER ROLE $DB_USER SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;
EOF

# Create a virtual environment
python3 -m venv myenv
source myenv/bin/activate

# Install Django and psycopg2
pip install django psycopg2-binary

# Create Django project
django-admin startproject $PROJECT_NAME

# Configure Django settings
cd $PROJECT_NAME/$PROJECT_NAME
sed -i "s/ENGINE': 'django.db.backends.sqlite3/ENGINE': 'django.db.backends.postgresql/g" settings.py
sed -i "s/NAME': BASE_DIR / 'db.sqlite3/NAME': '$DB_NAME'/g" settings.py
sed -i "/'ENGINE': 'django.db.backends.postgresql/a \        'USER': '$DB_USER',\n        'PASSWORD': '$DB_PASSWORD',\n        'HOST': 'localhost',\n        'PORT': ''," settings.py

# Run migrations
cd ..
python manage.py migrate

# Create superuser
python manage.py createsuperuser

# Start the development server
python manage.py runserver
Back to top