# Day 2 :  Shell Scripting

* **Learn**: Variables, loops, conditionals, functions in Bash.
* **Task**: Write a script (setup.sh) to automate installing dependencies and creating project dirs (/opt/ecomm).

\--> First Deleted all the packages that we installed yesterday as part of the learning goal of the day.

\--> wrote a shell script named `script.sh` which contains all the instructions /commands required to install all the necessary packages for our project

{% code title="setup-script.sh" %}

```sh
#!/bin/bash

set -e  # Exit the script immediately if a command fails
set -u  # Treat unset variables as an error

USER=$USER
PROJECT_DIR="/opt/ecomm"

echo "Updating System..."
sudo apt update && sudo apt upgrade -y || { echo "System update failed! Exiting..."; exit 1; }

echo "Installing Tools..."
packages=("git" "python3" "python3-pip" "nodejs" "npm" "openjdk-17-jdk" "golang-go" "docker.io")

for package in "${packages[@]}"; do
    if dpkg -l | grep -q "^ii  $package "; then
        installed_version=$(dpkg -l | grep "^ii  $package " | awk '{print $3}')
        echo "$package is already installed (version: $installed_version)."
        read -p "Do you want to upgrade $package to the latest version? (y/n): " choice
        if [[ "$choice" == "y" ]]; then
            sudo apt install --only-upgrade -y "$package" || { echo "Failed to upgrade $package! Exiting..."; exit 1; }
            echo "$package upgraded successfully."
        else
            echo "$package upgrade skipped."
        fi
    else
        sudo apt install -y "$package" || { echo "Failed to install $package! Exiting..."; exit 1; }
        echo "$package installed successfully."
    fi
done

echo "Configuring Docker..."
sudo systemctl enable docker || { echo "Failed to enable Docker! Exiting..."; exit 1; }
sudo systemctl start docker || { echo "Failed to start Docker! Exiting..."; exit 1; }
sudo usermod -aG docker "$USER" || { echo "Failed to add user to Docker group! Exiting..."; exit 1; }

echo "Creating Project directories..."
if ! sudo mkdir -p "$PROJECT_DIR"; then
    echo "Failed to create project directory! Exiting..."
    exit 1
fi

sudo chown "$USER:$USER" "$PROJECT_DIR" || { echo "Failed to set ownership for project directory! Exiting..."; exit 1; }
cd "$PROJECT_DIR" || { echo "Failed to navigate to project directory! Exiting..."; exit 1; }
mkdir -p user-service product-service order-service recommend-service || { echo "Failed to create subdirectories! Exiting..."; exit 1; }

echo "Verifying Installations..."
commands=("git --version" "python3 --version" "node -v" "java --version" "go version" "docker --version")

for cmd in "${commands[@]}"; do
    if ! eval "$cmd"; then
        echo "Verification failed for command: $cmd. Exiting..."
        exit 1
    fi
done

echo "Setup complete! Log out and back in for Docker group changes."

```

{% endcode %}

<figure><img src="/files/sOFpDWrlcZAC4gugAQBo" alt=""><figcaption></figcaption></figure>

That's it For today, see you tommorrow with another day of #50daysofdevopsJourney


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://charan-techjourney.gitbook.io/charan-techjournal/50-days-of-devops-ecommerce-microservices/project-daily-tasks/day-2-shell-scripting.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
