0

I am currently learning bash programming and dont really understand why the passing argument for me is not working.

i have a script like this

#!/bin/bash
# the following environment variables must be set before running this script
# SIM_DIR name of directory containing armsim
# TEST_DIR name of the directory containing this script and the expected outputs
# LOG_DIR name of the directory that your output is written to by the run_test2 script
# ARMSIM_VERBOSE set to "-v" for verbose logging or leave unset

# First check the environment variables are set
giveup=0
if [[ ${#SIM_DIR} -eq 0 || ${#TEST_DIR} -eq 0 || ${#LOG_DIR} -eq 0 ]] ; then
    echo One or more of the following environment variables must be set:
    echo SIM_DIR, TEST_DIR, LOG_DIR
    giveup=1
fi

# Now check the verbose flag
if [[ ${#ARMSIM_VERBOSE} != 0 && "x${ARMSIM_VERBOSE}" != "x-v" ]] ; then
    echo ARMSIM_VERBOSE must be unset, empty or set to -v
    giveup=1
fi

# Stop if environment is not set up
if [ ${giveup} -eq 1 ] ; then
    exit 0
fi

cd ${TEST_DIR}
for i in test2-*.sh; do
  echo "**** Running test ${i%.sh} *****"
  ./$i > ${LOG_DIR}/${i%.sh}.log
done

When I run the .sh file and pass in 3 example argument as below:-

$ ./run_test2 SIM_DIR TEST_DIR LOG_DIR

It still show: One or more of the following environment variables must be set: SIM_DIR, TEST_DIR, LOG_DIR

Can anyone guide me on this? Thank you.

AlG
  • 14,697
  • 4
  • 41
  • 54
Keat84
  • 47
  • 1
  • 4
  • Do you even set the environment variables? – nhahtdh Jul 03 '12 at 11:24
  • 1
    Environment variables aren't really arguments in the sense I understand from your question/example. It sounds to me like you want to give arguments to a function/script, if you do that you can find your arguments in $1-9 (I think bash supports even more, unsure), the number of arguments are stored in $#. – Jite Jul 03 '12 at 11:47
  • @Jite - Right. Sounds like you could flesh that out into another answer. One which uses arguments, instead of environment variables. – ArjunShankar Jul 03 '12 at 11:52
  • @ArjunShankar - Just did, with a simple example, thanks. – Jite Jul 03 '12 at 12:04

2 Answers2

2

That's not how it's intended to work. The environment variables must be set beforehand either in the script or in the terminal like

export SIM_DIR=/home/someone/simulations
export TEST_DIR=/home/someone/tests
export LOG_DIR=/home/someone/logs

./run_test2

If you use these variables frequently, you might want to export them in ~/.bashrc. The syntax is identical to the exports in the above example.

ArjunShankar
  • 23,020
  • 5
  • 61
  • 83
Deve
  • 4,528
  • 2
  • 24
  • 27
  • @Alan Curry Where and why is it necessary? – Deve Jul 03 '12 at 11:29
  • `export SIM_DIR=/home/someon/simulations` - if you don't export it, it's just a shell variable in the current shell, not an environment variable – Alan Curry Jul 03 '12 at 11:30
  • @Deve - Unless exported, a variable is not available in sub-processes. This means, unless you `export` it, even though bash sees `SIM_DIR`, the script won't. See [this](http://stackoverflow.com/questions/1158091/bash-defining-a-variable-with-or-without-export) – ArjunShankar Jul 03 '12 at 11:34
1

Environment variables aren't really arguments in the sense I understand from your question/example. It sounds to me like you want to give arguments to a function/script, if you do that you can find your arguments in $1-9 (I think bash supports even more, unsure), the number of arguments are stored in $#

Example function that expects two arguments:

my_func() {
    if [ $# -ne 2 ]; then
        printf "You need to give 2 arguments\n"
        return
    fi

    printf "Your first argument: %s\n" "$1"
    printf "Your second argument: $s\n" "$2"
}

# Call the functionl like this
my_func arg1 arg2
Jite
  • 4,250
  • 1
  • 17
  • 18