Bash syntax problem

Hello! i try to understand the art of bash scripting but unfortunately, more i try and less i understand it.
Can someone tell me how i can learn its logic? i will give you an example why its making me crazy. Look at this basic script:
my for loops are working like this, but it took me more than one hour because "for" is not in yellow. Is it a bug?

I also need help to compare table items in the second for loop.
Please don't give me another way to do it, i just try to understand syntax logic.
I swear i've tried to do it alone but no way.
Sorry, i m not good in english

ps: in fact, the first for loop is working, the second give an an error. Of course i 've changed the second one.

Hi could you post the text version instead of a picture?

There are several problems I noticed:

  • The third for loop needs extra parentheses, like the second for loop
  • Also it is missing a comparison in the middle field
  • An array element reference needs curly braces like this: ${array[idx]}

--
I don't understand why in this case the for statement gets highlighted in a different color within a function though.

Welcome to the forum.

Please - don't post pictures of your code, although your nice colour scheme might be lost if you copy & paste text into your post. And, post your OS and shell versions. And, very important, tell people in here WHAT doesn't behave, and HOW, so errors can be tracked.

"for is not in yellow" certainly is not a bug. Some editors help your get the syntax right by trying to interpret your code and then colourise the parts that it recognizes. So - if there was a bug, it would be somewhere upfront, confusing the editor in its syntax check. Quite often it's missing (closing) quotes or parentheses...
The only error that jumps to my eyes on first sight is the missing / wrong break condition in the innermost for loop.

Thank you for help me. I'm sorry those mistakes are very stupids. Also i forgot a "t" at $tableau in the last for. Now it's working but "for" is still in grey, so i suppose there's a litlle error somewhere. I give you the working code if you want to look for it.

If you can give me some good tutorial to understand some logic basis... for example when use (( or { , also the diference between " and quotes... you understand what i mean?

 #!/bin/bash

if [ $# -gt 1 ]
then 
	declare -a tableau
	idx=0
	for i in "$@"
	do
			tableau[$idx]=$i
			((idx++))

	done
fi

tri(){
	for ((i=0;i < $idx;i++))
	do	
		recom=0
		for ((a=0;a < $idx ;a++)) 
		do 
			if [ ${tableau[$i]} -lt ${tableau[$a]} ]
			then
				tempor=${tableau[$i]}
				tableau[$i]=${tableau[$a]}
				tableau[$a]=$tempor
				recom=1
			fi
		done

		if [ $recom -eq 1 ]
		then 
			((i--))
		fi
	done
}

tri
echo ${tableau[@]}

 

Have a look at : LMGTFY

BTW, you can simplify the creation of your tableau array to

tableau=("${@}")

Note also that your variable idx, refered to in function tri, is empty if your script is called without parameters.

thank you rovf. I know google lol but all tutorials i found tell how to do but don't explain the logic. I would like to be able to understand. Right now i often need to use google. What i want is to be able to write a script without it. I don't have syntax problem with C or java but with bash i'm very disapointed. Maybe I'm wrong but i think there's no "universal" syntax. I mean, sometimes i look at "resolved problems", i copy/paste the code but it's not working for me. RudiC asked me which version i use, so I suppose there are differences...
I'm very interested in learning sytem and networking administration, it's very interesting but i must be able to write scripts to automate tasks. That's my only problem right know.

If you know programming languages like C or java, you are able to cope with (bourne type) shells. The logics is not too different from the formers', the syntax might be. It's just a matter of exercise. If copy / pasted code doesn't work for you, you indeed should compare the versions. All shells, be it bash or ksh or what have you, evolve over time, and functionality found in recent ones may not be available in older versions. Same is true between shells - although bash and ksh both are descendants of the original sh , and are generally compatible with each other, there are parts in each that don't work in the other.
Run your code, and post it as well as shell version, results and errors in here to be analysed and commented on.

OK RudiC, thanks a lot! Now i know why it is so disapointing for me. I think i used another version few years ago... i suppose with more practice it will seem easier.
Tank you again.

I don't know, what you mean by "universal syntax", but bash has a well-defined syntax with, AFIK, virtually know "exceptions from the rules". Of course, copy&paste without understanding what's behind, is always difficult - but this is not different in C or Java. Also, language evolution means that there are differences between versions, but this is ALSO true for nearly every other language.

For an explanation of a certain construct, the bash man page is a good source. This is similar to consulting the ANSI standard document for C for really knowing what's going on.

Of course, if a certain construct is not clear, you can ask here, but write a separate post for each construct you are interested in, and try to ask a precise question.

You could look in the FAQ sub forum here for pointers to online books/tutorials or phsyical books to learn from. There is also the online copy of Learning the Korn Shell. There are differences between the two shells, so not everything in the book applies to bash (also, both shells have evolved since this book was published).

Andrew

To be honest, while bash and ksh have a lot in common, the differences are large enough to a complete beginner in shell programming, so I personally would recommend against it, even if the text is written well.