Hi,
I want to start MY_PROGRAM in a bash script with additional parameters given in the CONFIGURATION_ARRAY.
IFS="'"
CONFIGURATION_ARRAY=( '-N 0 -m 0' '-N 0 -m 1' )
for configuration in ${CONFIGURATION_ARRAY[@]}
do
//DEBUG
N=${configuration%-*} //-N 0
M=-${configuration##*-} //-m 0
a="-N 0"
b="-m 0"
printf "%s \n" $N //-N 0
printf "%s \n" $a //-m 0
if [ $N == $a ]
then
echo "SAME" //it's not the same - WHY???
fi
if [ $M == $b ]
then
echo "SAME" //it's the same
fi
Actual execution
sudo ./MY_PROGRAM $configuration //I want this to work but: doesn't work
sudo ./MY_PROGRAM $N $M //doesn't work
sudo ./MY_PROGRAM $N //doesn't work
sudo ./MY_PROGRAM $M //works!!!
sudo ./MY_PROGRAM $a $b //works!!! but a and b contain the same strings as N and M do.. what the hell?
I have absolutly no idea, I already spent hrs on this... can someone please help me? What's the difference between variable N and variable a? It contains the same and both are strings... but the programm only works with a.
That cannot possibly be your code... BASH does not use // for comments, it uses #.
If your variables contain spaces you need to quote them in double-quote characters, like "$a" and "${CONFIGURATION_ARRAY[@]}" or BASH will split them in unintended ways.
That last "sudo" is probably the only place you don't want to quote them -- you want it to split there, to get normal behavior.
Instead of feeding everything into sudo, I suggest doing this: printf "[%s]\n" $N $M ...so you can tell exactly what arguments you're actually getting with those different methods.
What is your intention with those // ? I'm pretty sure bash doesn't like those; you should get plenty of error msgs therefor.
And, why should it make any difference when calling it with $N $M parameters as opposed to $a $b? you don't use positional parameters anyhow in your code.
Pay attention to trailing spaces when extracting and comparing your variables...
That's almost certainly related to your issue... IFS controls all splitting! So your script is effectively being forcefed each element as one parameter like:
./script "-N 0 -m 0"
instead of being split like you'd want, into
./script "-N" "0" "-m" "0"
...because that string contains no single-quotes to split upon.
Anyway, you don't need to set IFS here at all. It only controls how unquoted strings work in an array, it has nothing to do with the behavior of quotes themselves. Just be sure to quote your variables where you don't want them to split!
CONFIGURATION_ARRAY=( '-N 0 -m 0' '-N 0 -m 1' )
for configuration in "${CONFIGURATION_ARRAY[@]}"
do
printf "Quoted does not split: %s\n" "$configuration"
printf "Unquoted split: %s\n" $configuration
done
The quotes in red are special. When you put double-quotes around $@ or ${ARRAY[@]} it splits on array elements and not IFS.
This outputs:
Quoted does not split: -N 0 -m 0
Unquoted split: -N
Unquoted split: 0
Unquoted split: -m
Unquoted split: 0
Quoted does not split: -N 0 -m 1
Unquoted split: -N
Unquoted split: 0
Unquoted split: -m
Unquoted split: 1