This is the shell script code
if [ -z "$1" ]
then
echo "src directory will be considered for analysis"
${PYTHON3_ENV} $sca_wrapper_path/astree_launcher/create_astree_config.py ${prjRoot} ${OUTDIR} ${prjRoot}/src
else
echo "Dax file will be created for the selected folders"
${PYTHON3_ENV} -m pip install -r ${SCA_WRAPPER_PATH}/requirements.txt
set u
# optional argument. In case arguments are not provided, empty string will be set intentionally
export SCA_include1=$2
export SCA_include2=$3
export SCA_include3=$4
export SCA_include4=$5
export SCA_include5=$6
export SCA_include6=$7
export SCA_include7=$8
export SCA_include8=$9
set -u
${PYTHON3_ENV} $sca_wrapper_path/astree_launcher/create_astree_config.py ${OPTION1} ${prjRoot} ${OUTDIR} ${SCA_include1} ${SCA_include2} ${SCA_include3} ${SCA_include4} ${SCA_include5} ${SCA_include6} ${SCA_include7} ${SCA_include8}
fi
In this Bash Script can i use for loop for
export SCA_include1=$2
export SCA_include2=$3
export SCA_include3=$4
export SCA_include4=$5
export SCA_include5=$6
export SCA_include6=$7
export SCA_include7=$8
export SCA_include8=$9
and reduce the number of lines, please help me
CodePudding user response:
Typically, you access all of the positional parameters via "$@"
, which expands to each positional argument (or to the null string if there are none). Your snippet could be re-written:
if [ -n "$1" ]; then
echo "Dax file will be created for the selected folders"
${PYTHON3_ENV} -m pip install -r ${SCA_WRAPPER_PATH}/requirements.txt
option1=$1 # This is my guess as to what OPTION1 is
shift # remove $1 so that "$@" does not repeat it
if test $# -lt 8; then
echo 'You must pass at least 8 arguments' >&2
exit 1
fi
else
echo "src directory will be considered for analysis"
unset option1
unset outdir
set -- "${prjRoot}/src" # Append $prjRoot/src so that "$@" expands to "${prjRoot}/src"
fi
# See discussion below on quoting
${PYTHON3_ENV} $sca_wrapper_path/astree_launcher/create_astree_config.py \
${option1: "$option1"} "${prjRoot}" ${outdir: "$outdir"} "$@"
Note that your original code uses set -u
to ensure that a specific number of positional parameters are set. That type of check could be done by evaluating $#
.
Also note that quoting really matters here. Typically, best practice is to double quote variables, and the above command should almost certainly put quotes around ${prjRoot}
, but you cannot simply quote ${option1}
or ${outdir}
since you want them to expand to nothing. Passing an empty string as an argument is very different than passing no argument at all, and if you quote those variables you will get the former behavior. The above uses ${option1: "$option1"}
, which expands either to the empty string (so no argument at all is passed), or the quoted value of $option1
(so that field splitting is suppressed and only one argument is passed). I suspect outdir
and option1
could be handled more cleanly, but you haven't shown complete code so it's difficult to be certain. It's not clear to me how you should handle ${prjRoot}
if it is empty, so I'll assume that cases is handled earlier in the script (eg, by aborting with an error message).
CodePudding user response:
Try:
for i in {2..9}; do
export "SCA_include$((i-1))=${!i}"
done
${!i}
is variable indirection. See Shell Parameter Expansion in the Bash Reference Manual.