x_maras
x_maras

Reputation: 2217

export works in command line but not in a bash script

I tried to write a script in order to automate the switching between php5.3 and 5.4 in my path. Although the export works when I execute it, it does nothing when it runs inside the script.

Here is my script

#!/bin/bash
# chphp
# Switch between php versions 5.3 and 5.4

FIXED_PATH=/here/is/my/path

if [ "$1" != "-v" ]
then
    echo "Wrong usage of the script.\n"
else
    if [ "$2" == 3 ]
    then
            export PATH="$FIXED_PATH:/path/to/php-5.3/bin"
            export PHP_INI_SCAN_DIR=""
    elif [ "$2" == 4 ]
    then
            export PATH="$FIXED_PATH:/path/to/php-5.4/bin"
            export PHP_INI_SCAN_DIR="/path/to/php-5.4/etc/php-dbg.d"
    else
            echo "Wrong usage of the script.\n"
    fi
fi

I also tried the following, which I found by searching for similar questions, but they didn't work

this

. ./chphp.sh -v 3

and this

source ./chphp.sh -v 3

Any suggestions or ideas how I could fix it or do things differently?

EDIT: The code was missing an else after the first echo, as Kent noticed and which I edited. The code now runs with both ways now!

Upvotes: 0

Views: 242

Answers (1)

Kent
Kent

Reputation: 195029

your script will set vars only if $1 is NOT -v

if $1 is -v, your script does nothing. check your script the first if-then

does this help? (to set php5.3 stuffs)

source ./chphp.sh foo 3

Upvotes: 1

Related Questions