Script

Getting Started

Sample script with some basic error checking:

#!/bin/bash
# exit immediately if a command exits with a nonzero exit status.
set -e
# treat unset variables as an error when substituting.
set -u

# Print Hello World!
# Do the work.
echo Hello world

Tip

Look at this wonderful comic https://wizardzines.com/comics/bash-errors/ (it explains everything you need to know)…

../../_images/julia-evans-bash-errors.jpg

Permissions:

chmod 755 scriptname

Note: Not sure if these are the correct permissions… (perhaps try chmod +x myfile.py).

To run this script:

bash <script-name>

Sample

Run a python application, passing all parameters to the command:

#!/bin/sh
. ~/repo/env/env-app/bin/activate
PYTHONPATH=/srv/django/:/srv/django/app/:/srv/django/modules/region/src/; export PYTHONPATH
python /root/repo/env/env-app/bin/django-admin.py $* --settings=settings_webtest

Note: To pass all parameters to a command, Simon says to use $@. You should (almost) always put it in double-quotes to avoid misparsing of argument with spaces in them e.g:

#!/bin/sh
ack-grep "$@"

Debug

To see which command is running, use the -xv command:

sh -xv ./myscript.sh