简体   繁体   中英

Checking cmd line argument in bash script bypass the source statement

I have an bash script "build.sh" like this:

# load Xilinx environment settings
source $XILINX/../settings32.sh

cp -r "../../../EDK/platform" "hw_platform"

if [ $# -ne 0 ]; then
    cp $1/system.xml hw_platform/system.xml
fi

echo "Done"

Normally I run it as "./build.sh" and it execute the "source" statement to set environment variables correct. Sometimes I need to let the script to copy file from an alternative place, I run it as "./build.sh ~/alternative_path/"; My script check whether there is an cmd line argument by checking $# against 0.

When I do that, the "source" statement at the beginning of the script somehow get skipped, and build failed. I have put two "echo" before and after the "source", and I see echo statements get executed.

Currently I circumvent this issue by "source $XILINX/../settings32.sh; build.sh". However, please advise what I have done wrong in the script? Thanks.

Try storing the values of your positional paramaters first on an array variable then reset them to 0. "$XILINX/../settings32.sh" may be acting differently when it detects some arguments.

# Store arguments.
ARGS=("$@")

# Reset to 0 arguments.
set --

# load Xilinx environment settings
source "$XILINX/../settings32.sh"

cp -r "../../../EDK/platform" "hw_platform"

if [[ ${#ARGS[@]} -ne 0 ]]; then
    cp "${ARGS[0]}/system.xml" hw_platform/system.xml
fi

echo "Done"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM