0

i have created this bash script to create django projects

#!/bin/bash
PROJECT_NAME=$1
ENV_NAME=$2
PROJECT_DIR="$HOME/$PROJECT_NAME"
echo $PROJECT_NAME;
echo $ENV_NAME;
mkdir -p "$PROJECT_NAME"
cd "$PROJECT_DIR"
python3 -m venv "$ENV_NAME"
. "$PROJECT_DIR/$ENV_NAME/bin/activate"
pip install django django-extensions django-debug-toolbar python-memcached djangorestframework
pip freeze > "$PROJECT_DIR/requirements.txt"
django-admin startproject "$PROJECT_NAME"
mkdir -p public
cd "$PROJECT_DIR/$PROJECT_NAME"
pwd
mkdir -p templates
mkdir -p static
cd templates
touch index.html base.html
cd ..
pwd
cd "$PROJECT_NAME"
mv settings.py settings_base.py
touch settings.py settings_local.py settings_local_sample.py
ls

when i run source file.sh project_name env it creates the project, i want to pass the requirements of the project as array argument in the bash , how i can do it

1 Answer 1

1

The command arguments are in the $@ array. So you could pass the requirements as additional command line arguments. Then, after you set PROJECT_NAME to the first argument, do a shift to remove the first argument from $@. Then, after you set ENV_NAME to the (new) first argument, do a shift again. Then, $@ will contain only the remaining arguments = the list of requirements, so you can use that with pip install.

Minor changes in your script:

#!/usr/bin/env bash
PROJECT_NAME=$1; shift
ENV_NAME=$1; shift
...
pip install "$@"

And call the script with:

path/to/script.sh the_project_name the_env_name django django-extensions django-debug-toolbar python-memcached djangorestframework

If the list of requirements are already in a Bash array in the context where you call the script, then you can pass the array content as command arguments:

path/to/script.sh the_project_name the_env_name "${requirements[@]}"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.