Skip to content

Advanced Level

This section covers expert-level concepts in automation and scripting, including advanced debugging techniques, signal handling, remote execution, and complex pipeline configurations.

0 / 20 reviewed (0%)
  1. How do you debug a Bash script?

    Primary tool: trace mode. set -x prints every command after expansion, prefixed with +:

    bash
    #!/bin/bash
    set -x            # start tracing
    NAME="prod"
    echo "Deploying to $NAME"    # you see: + echo 'Deploying to prod'
    set +x            # stop tracing

    A rounded answer also mentions:

    • bash -x script.sh — trace the whole script without editing it
    • set -v — print lines before expansion (shows the raw source)
    • PS4='+ $LINENO: ' — include line numbers in trace output
    • bash -n script.sh — syntax-check without executing (great for CI linting)

    In production, prefer explicit log() functions with timestamps over leaving set -x enabled — traces can leak secrets into logs.

  2. How do you trap signals in a Bash script?

    trap registers a handler that runs when a signal arrives:

    bash
    #!/bin/bash
    TMPDIR=$(mktemp -d)
    trap 'echo "Cleaning up..."; rm -rf "$TMPDIR"; exit 130' SIGINT SIGTERM
    trap 'rm -rf "$TMPDIR"' EXIT          # runs on every exit path
    
    while true; do echo "working..."; sleep 5; done

    Signals to know: SIGINT (Ctrl+C), SIGTERM (the default kill), and EXIT (fires when the script ends for any reason). The professional pattern is a cleanup trap that removes temp files, releases lockfiles, or kills child processes — so an interrupted deploy doesn't leave garbage behind. Container entrypoints rely on trap + exec to handle SIGTERM from docker stop gracefully.

  3. What is the difference between $(command) and backticks `command` in Bash?

    Both perform command substitution, but $() is strictly better:

    bash
    NOW=$(date -u +%Y-%m-%d)                  # preferred
    NESTED=$(echo "today is $(date +%A)")     # $() nests cleanly
    NESTED=`echo "today is \`date +%A\`"`     # backticks need escaping to nest

    Backticks are the legacy POSIX form: they're hard to read, don't nest without ugly escaping, and can silently mangle backslashes inside. Always answer with $() — and mention that any command's output (including $(pwd), $(whoami)) is captured this way, which is how scripts compose dynamic values.

  4. How do you handle multiline commands in a Bash script?

    Two idiomatic ways:

    bash
    # 1. Backslash continuation
    echo "Deploying to staging with" \
      "--timeout 300s" \
      "--retries 3"
    
    # 2. Heredoc for blocks of text or scripted input
    cat <<'EOF' > config.txt
    line one stays literal
    no $variable expansion with quoted delimiter
    EOF

    The backslash must be the last character on the line (no trailing spaces). For long docker run or terraform invocations, backslash continuations keep commands readable; heredocs excel at feeding multi-line input to commands or writing config files from scripts. Note the quoting detail: a quoted delimiter (<<'EOF') disables variable expansion inside the block.

  5. How do you use conditionals inside a YAML file?

    YAML itself has no conditional syntax — conditionals come from the tool that processes the YAML. In Ansible that's the when clause (with Jinja2 expressions):

    yaml
    - name: Install web server
      package:
        name: httpd
      when: ansible_os_family == "RedHat"

    The same pattern exists elsewhere: GitHub Actions uses if: runner.os == 'Linux', and templates use Jinja2 {% if %} blocks. A precise answer separates the layers — YAML is pure data; the runtime provides the logic — which is exactly what interviewers are testing with this question.

  6. How do you encrypt secrets in YAML (Ansible)?

    Use Ansible Vault, which encrypts files or single values with AES256:

    bash
    ansible-vault encrypt secrets.yml       # encrypt a whole file
    ansible-vault encrypt_string 'p@ss' --name 'db_password'   # encrypt one value
    ansible-playbook site.yml --ask-vault-pass

    Decrypt/edit/re-encrypt with ansible-vault view / edit. The follow-up interviewers expect: in CI, the vault password itself must come from a secrets manager or the CI's encrypted variables (never committed), and --vault-password-file (mode 600) is the scripted alternative to --ask-vault-pass. Vaulted content is still safe to commit because it's ciphertext — verify with git log discipline that no plaintext secret was ever pushed.

  7. How do you execute a Python script inside a Bash script?

    bash
    # 1. As a file
    python3 /opt/scripts/report.py --format json
    
    # 2. Inline via heredoc
    python3 <<'EOF'
    import boto3
    print(boto3.client("s3").list_buckets()["Buckets"][0]["Name"])
    EOF
    
    # 3. One-liner with -c
    python3 -c 'import json; print(json.dumps({"ok": True}))'

    The heredoc form shines when a Bash script needs a few lines of Python logic without a separate file — and quoting the delimiter (<<'EOF') stops Bash from expanding $ inside the Python code. For anything beyond ~10 lines, keep it as a proper .py file so it's testable; the Bash script should orchestrate, not embed whole programs.

  8. What is the difference between continue and break in Bash loops?

    bash
    for env in dev staging prod; do
      [[ "$env" == "staging" ]] && continue   # skip staging, keep looping
      [[ "$env" == "prod" ]] && break         # stop the loop entirely
      echo "Deploying to $env"
    done

    continue skips to the next iteration; break exits the loop. In nested loops, break 2 / continue 2 affect the second-level loop — worth naming explicitly since it rarely comes up outside interviews. Typical use: continue to skip hosts that are unreachable in a deployment loop, break to stop on a fatal error when retrying.

  9. How do you run tasks in parallel from a Bash script?

    Three levels, in increasing sophistication:

    bash
    # 1. Background jobs + wait
    for host in web1 web2 web3; do
      ssh "$host" 'uptime' &
    done
    wait    # block until all background jobs finish
    
    # 2. xargs -P: run N at a time
    cat hosts.txt | xargs -P 5 -I{} ssh {} 'uptime'
    
    # 3. GNU parallel: progress, retries, output grouping
    parallel -j5 ssh {} 'uptime' ::: web1 web2 web3

    The interview point beyond syntax: parallel output interleaves, so redirect each job to its own log file (> /tmp/$host.log 2>&1) and aggregate afterward. Also mention bounding concurrency (-P 5) — unbounded parallelism against hundreds of hosts is how you DDoS your own infrastructure or the API you're calling.

  10. How do you use arrays and maps in Groovy?

    groovy
    def list = ["a", "b", "c"]            // java.util.List
    println list[0]                       // a
    list.each { item -> println item }    // closure iteration
    
    def server = [name: "web-01", env: "prod"]   // map literal
    println server.name                   // dot access works on maps
    server.each { k, v -> println "$k=$v" }

    Groovy's literals and closures are why Jenkinsfiles stay readable: a matrix of build targets is a list of maps you can iterate with .each, and closures like collect {} (map) and findAll {} (filter) express deploy logic compactly. Under the hood they're plain Java collections, so all JDK methods are available.

  11. What is a Jenkins shared library, and how is it structured?

    A shared library is a versioned Groovy codebase stored in its own Git repo that pipelines load, turning repeated pipeline logic into one call:

    groovy
    @Library('my-shared-library@v2.3') _
    prodevops.deployApp(env: 'staging', replicas: 3)

    The standard layout:

    code
    vars/deployApp.groovy      # global steps callable from any pipeline
    src/org/prodevops/Utils.groovy  # helper classes (static imports)
    resources/                 # config files, templates

    The payoff interviewers want stated: 50 pipelines that each copy-pasted 80 lines of deploy logic become one versioned, tested, centrally-upgradable step — and tagging releases (@v2.3) lets teams upgrade deliberately instead of every pipeline changing at once.

  12. How do you set a timeout for a script in Bash?

    bash
    timeout 30s ./backup.sh              # kill with SIGTERM after 30s
    timeout -k 5s 30s ./backup.sh        # then SIGKILL 5s later if still alive
    timeout --preserve-status 30s ./script.sh   # exit with the script's own code
    echo $?                              # 124 means the timeout fired

    timeout (GNU coreutils) sends SIGTERM first so the child can clean up — the -k flag is the backstop for processes that ignore it. In CI this replaces hand-rolled sleep-and-check retry logic around network calls. Also name the alternative: many tools have native timeouts (curl --max-time, ssh -o ConnectTimeout=5), which are cleaner when available.

  13. What happens when you run a Bash function in a subshell?

    bash
    cd /tmp
    my_function() { cd /var/log; echo "inside: $PWD"; }
    
    (my_function)                # subshell: prints /var/log
    echo "outside: $PWD"         # still /tmp — parent unaffected
    
    var="before"
    (var="changed")
    echo "$var"                  # before — variable changes don't leak

    Parentheses spawn a child shell (a fork): the function runs, but every state change stays inside — variable assignments, cd, and option changes like set -e. That isolation is a feature: wrap a risky or state-mutating step in ( ) so the main script's environment stays clean. It's also exactly how $(command substitution) works — the command runs in a subshell and its stdout is captured.

  14. How do you use Python to send an HTTP request?

    python
    import requests
    
    response = requests.get(
        "https://api.github.com/users/torvalds",
        timeout=10,                      # never let a request hang forever
    )
    response.raise_for_status()          # raise on 4xx/5xx
    data = response.json()
    print(data["public_repos"])

    Production habits to mention: always set timeout (without it a hung endpoint hangs your pipeline forever), call raise_for_status() instead of assuming success, and use a requests.Session() when making many calls to one host (connection reuse + shared headers). For retries, urllib3.util.Retry mounted on a Session is the standard answer — status codes and backoff handled for you.

  15. How do you handle authentication in a Python script?

    python
    import os
    import requests
    
    token = os.environ["API_TOKEN"]        # from the environment, never hardcoded
    
    response = requests.get(
        "https://api.example.com/v1/deployments",
        headers={"Authorization": f"Bearer {token}"},
        timeout=10,
    )

    The rules interviewers probe:

    • Never hardcode credentials — read them from environment variables, vaults (HashiCorp Vault, AWS Secrets Manager), or mounted secret files
    • os.environ["KEY"] failing fast is good — a missing secret should stop the script
    • For OAuth/token flows, refresh tokens via a requests.Session with an auth hook so every request carries the fresh header
    • Never print the token, and watch that error tracebacks don't include the URL with credentials in it
  16. How do you find and replace values in YAML files?

    Use yq — the YAML counterpart of jq (mikefarah's v4 syntax):

    bash
    yq -i '.replicas = 3' deployment.yaml          # set a scalar
    yq -i '.spec.template.spec.containers[0].image = "nginx:1.27"' deployment.yaml
    yq '.spec.replicas' deployment.yaml            # read a value

    Text tools like sed also work on YAML but are dangerous: sed 's/replicas: 2/replicas: 3/' will happily rewrite the wrong occurrence (any file with a similar line). yq addresses nodes by path, parses valid YAML only, and preserves the file structure — that's the safe answer for pipelines that mutate Kubernetes manifests or Ansible vars.

  17. How do you define a multi-stage Jenkins pipeline in Groovy?

    groovy
    pipeline {
        agent any
        parameters {
            string(name: 'TARGET_ENV', defaultValue: 'staging')
        }
        stages {
            stage('Build')  { steps { sh 'make build' } }
            stage('Test')   { steps { sh 'make test' } }
            stage('Deploy') {
                when { expression { params.TARGET_ENV != 'prod' } }
                steps { sh "./deploy.sh ${params.TARGET_ENV}" }
            }
            stage('Prod') {
                when { branch 'main' }
                steps { input message: 'Approve prod deploy?' ; sh './deploy.sh prod' }
            }
        }
        post {
            always  { cleanWs() }
            failure { emailext subject: "Build failed", to: "team@example.com" }
        }
    }

    What makes this "advanced": parameters for input, when conditions to gate stages (branch or environment), the input step for manual approval gates before production, and post blocks for cleanup and notification. This maps directly to how real promotion pipelines — build once, test, promote through environments — are structured.

  18. What is an inline script in a CI/CD pipeline?

    An inline script is shell logic written directly inside the pipeline definition instead of being checked out as a file:

    yaml
    # GitHub Actions
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v4
          - run: |
              set -euo pipefail
              make build
              ./scripts/verify.sh

    Trade-offs to articulate: inline scripts keep everything visible in one file — great for short steps — but they're hard to lint/test and encourage copy-paste across repos. Best practice: short one-liners inline, anything with real logic as a versioned script file (also reusable locally), with set -euo pipefail at the top either way. Note the | block scalar and that actions/checkout must run first or your script files don't exist yet.

  19. How do you execute a script remotely via SSH in Bash?

    bash
    # Stream a local script and run it remotely
    ssh -o BatchMode=yes deploy@web-01 'bash -s' < /opt/scripts/setup.sh
    
    # Or with arguments
    ssh deploy@web-01 'bash -s' -- --env staging < setup.sh

    Why 'bash -s' is the pattern: the remote shell reads the script from stdin (-s), so nothing needs to be copied over first, and quoting the remote command keeps all parentheses/semicolons local. Points that complete the answer: use key-based auth (with BatchMode=yes so it fails instead of hanging on a password prompt in CI), and prefer running the script from a Git checkout on the target (or an agent/Ansible) for anything repeated — ad-hoc SSH doesn't scale or audit well.

  20. How do you ensure idempotency in an Ansible playbook?

    Idempotency means running the playbook any number of times produces the same result with no side effects on repeat runs. Ansible gives it to you when you use the right tools:

    yaml
    - name: Ensure nginx is installed
      package:
        name: nginx
        state: present        # module checks and only changes if needed
    
    - name: Deploy config
      template:
        src: nginx.conf.j2
        dest: /etc/nginx/nginx.conf
      notify: restart nginx   # handler runs ONLY if the file actually changed

    The contrast to articulate: raw shell/command tasks run every time (not idempotent) — so they need guards (creates: argument or an stat/when check) when used. Ansible modules report changed/ok per task, which is what makes outputs like --check (dry-run) meaningful. Handlers + notify complete the picture: services restart only when their configuration actually changed.