Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash one liner - Test if a file exists and source if it does other exit with error

Tags:

bash

php

testing

In other words I want to make this into a one-liner:

    test -e ${MY_HOME}/setup-env.sh || { echo "ERROR: MY_HOME not defined or does not contain srtup-env.sh" 1>&2 ; exit 1; }

    . ${MY_HOME}/setup-env.sh
like image 941
skanga Avatar asked Jul 11 '14 13:07

skanga


2 Answers

You can use this one liner:

[[ -e "${MY_HOME}/setup-env.sh" ]] && source "${MY_HOME}/setup-env.sh" || { echo "ERROR: MY_HOME not defined or does not contain srtup-env.sh" 1>&2 ; exit 1; }
like image 170
anubhava Avatar answered Oct 13 '22 01:10

anubhava


[[ -e ${MY_HOME}/setup-env.sh ]] && { source "${MY_HOME}/setup-env.sh"; exit; }; echo "ERROR: MY_HOME not defined or does not contain setup-env.sh" >&2; exit 1

Or if non-bash:

test -e "${MY_HOME}/setup-env.sh" && { . "${MY_HOME}/setup-env.sh"; exit; }; echo "ERROR: MY_HOME not defined or does not contain setup-env.sh" >&2; exit 1

It's actually not a "one-liner" for me but just a condensed form.

like image 24
konsolebox Avatar answered Oct 13 '22 01:10

konsolebox