Strange type mistake?!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Strange type mistake?!
# 1  
Old 08-15-2014
Strange type mistake?!

Hi,
I want to start MY_PROGRAM in a bash script with additional parameters given in the CONFIGURATION_ARRAY.

Code:
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
Code:
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.

Last edited by rbatte1; 08-15-2014 at 10:51 AM.. Reason: Break up the code and the output
# 2  
Old 08-15-2014
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.

Last edited by Corona688; 08-15-2014 at 12:38 PM..
# 3  
Old 08-15-2014
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...

Why sudo?
# 4  
Old 08-15-2014
Hi,
the // comments are only for you, I don't use them in my script.
I use IFS="'" for the strings in my array.
Code:
echo $configuration

the output is:
Code:
-N 0 -m 0

But my program doesn't work with this call:
Code:
./MY_PROGRAM $configuration

But if I use another variable, let's say this one:
Code:
TEST="-N0 -m 0"

and I call my program:

Code:
./MY_PROGRAM $TEST

then it works! Why?!
# 5  
Old 08-15-2014
Oh! I'd missed the IFS! That's very important.

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:

Code:
./script "-N 0 -m 0"

instead of being split like you'd want, into

Code:
./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!

Code:
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:

Code:
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


Last edited by Corona688; 08-15-2014 at 12:55 PM..
This User Gave Thanks to Corona688 For This Post:
# 6  
Old 08-15-2014
Wow, thank you! You saved my day! Smilie
This User Gave Thanks to xraystorm For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Programming

Strange value of the double type variable: -nan(0x8000000000000)

I am confused by the value of "currdisk->currangle" after adding operation. Initially the value of "currdisk->currangle" is 0.77500000000000013, but after adding operation, it's changed to "-nan(0x8000000000000)", Can anyone explain ? Thanks! The following is the occasion of gdb debugging. 3338 ... (8 Replies)
Discussion started by: 915086731
8 Replies

2. UNIX for Dummies Questions & Answers

Can anyone help me to spot my mistake?

Hi there can anyone help me to spot my mistake and please explain why it appears My code : #!/usr/bin/gawk -f BEGIN { bytes =0} { temp=$(grep "datafeed\.php" | cut -d" " -f8) bytes += temp} END { printf "Number of bytes: %d\n", bytes } when I am running ./q411 an411 an411: ... (6 Replies)
Discussion started by: FUTURE_EINSTEIN
6 Replies

3. UNIX for Dummies Questions & Answers

Probably some stupid mistake...

Hi everyone ! I have a file wich look like this : >Sis01 > Sis02 ... >Sis44 I want to separe each paragraphe in a different file, so I decide to use the "FOR" loop + sed. for f in {01..44} do (5 Replies)
Discussion started by: sluvah
5 Replies

4. Shell Programming and Scripting

Is there any mistake in this code:

cat $1 | sort -n | uniq | $1 in other words, I sort the content of the file and put the ouput in the same file, is there any mistakes in this cshell code ??? (4 Replies)
Discussion started by: Takeeshe
4 Replies

5. AIX

Did a Mistake with HACMP

Hi, I needed space on a FS, and when I've added the space on the filesystem, I did it trough the regular smitty fs inteface and not with smitty cl_lvm. Can someone help me to repair the situat before a faileover happen ? Thanks for your help,:mad: (13 Replies)
Discussion started by: azzed27
13 Replies

6. Programming

array type has incomplete element type

Dear colleagues, One of my friend have a problem with c code. While compiling a c program it displays a message like "array type has incomplete element type". Any body can provide a solution for it. Jaganadh.G (1 Reply)
Discussion started by: jaganadh
1 Replies

7. Shell Programming and Scripting

String type to date type

Can one string type variable changed into the date type variable. (1 Reply)
Discussion started by: rinku
1 Replies

8. UNIX for Dummies Questions & Answers

Crontab Mistake!!!

Hi. I hope someone can help me with this problem. Being a novice to Unix, I editted my crontab directly by typing " crontab -e ". Well, I needed to make some changes so, I typed " crontab -r ". Now I have no crontab, and I can't seem to get crontab to write a new file. I' ve tried: vi... (4 Replies)
Discussion started by: cstovall
4 Replies
Login or Register to Ask a Question