Bash syntax problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash syntax problem
# 1  
Old 05-08-2018
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.
Bash syntax problem-bash1png

Last edited by sablista; 05-08-2018 at 11:51 AM.. Reason: mistake
# 2  
Old 05-08-2018
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.

Last edited by Scrutinizer; 05-08-2018 at 12:01 PM..
# 3  
Old 05-08-2018
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.
# 4  
Old 05-08-2018
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?

Code:
 #!/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[@]}

# 5  
Old 05-09-2018
Have a look at : LMGTFY

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

Code:
tableau=("${@}")

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

Last edited by rovf; 05-09-2018 at 03:30 AM.. Reason: Making it clearer
# 6  
Old 05-09-2018
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.
# 7  
Old 05-09-2018
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.

Last edited by RudiC; 05-09-2018 at 08:03 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Syntax error in subtraction in Bash

I am sharing a code snippet. for (( i=0; i<=$(( $count -1 )); i++ )) do first=${barr2} search=${barr1} echo $first echo "loop begins" for (( j=0; j<=5000; j++ )) do if } == $search ]]; then echo $j break; fi done second=${harr2} echo $second (2 Replies)
Discussion started by: ngabrani
2 Replies

2. Shell Programming and Scripting

Bash function using variable in it syntax error

The below bash function uses multiple variables CODING, SAMPLE, SURVEY, andvariant in it. The user selects the cap function and details are displayed on the screen using the $SURVEY variable, the directory is changed to $SAMPLE and the samples.txt is opened so the user can select the sample to... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. Shell Programming and Scripting

Bash syntax

Hello, I have seen this syntax, { ;;};quite often and I don't know what it means exactly. It seems like a distinctive thing of Bash, so it's been used for the logo of the last bug, ShellShock: All you need to know about the Bash Bug vulnerability | Symantec Connect I have also seen... (3 Replies)
Discussion started by: Kibou
3 Replies

4. Shell Programming and Scripting

Bash syntax error

while read line do mkdir $line scp -r Docking_results/docking_$line.pdb $line/ cd /$line/ set a=`grep ENDMDL docking_'$line'.pdb | wc -l` set b=`expr $a - 2` csplit -k -s -n 3 -f docking_'$line'. docking'$line'.pdb '/^ENDMDL/+1' '{'$b'}' foreach f (... (4 Replies)
Discussion started by: chrisjorg
4 Replies

5. Web Development

Bash Script, ec2addtag syntax?

ec2addtag --region us-west-1 vol1234 --tag Name=$nameinst; It should execute ec2addtag --region us-west-1 vol1234 --tag Name=webserver; Instead it thinks that Name is equal to that variable. Please help. Thanks! Please use code tags! (0 Replies)
Discussion started by: svalenciatech
0 Replies

6. Shell Programming and Scripting

Bash syntax behaviour : [[ vs [

Hello. In the following : RESTORE_FF contain a file name : a_file.tar.gz I am testing in a directory if "a_file.tar.gz" exists and or if any file like "a_file.tar.gz" exists. So "a_file.tar.gz" will give me file exists So "a_file.tar.gz." will give me file exists So... (5 Replies)
Discussion started by: jcdole
5 Replies

7. Shell Programming and Scripting

syntax issue mysql in bash script

I'm running mysql in a bash script mysql <<EOF query EOF one query is like this: UPDATE $dbname.$prefix"config" SET value = $var WHERE "$prefix"config.name = 'table colname'; with variable but it's giving an error i'm not sure what to put for "$prefix"config.name the table... (3 Replies)
Discussion started by: vanessafan99
3 Replies

8. Shell Programming and Scripting

Solaris bash syntax different from Linux?

I have a script that's meant to check the disk usage on a particular volume and delete the oldest logfile if it's over a certain percentage. It runs fine on a Linux machine, but on a Solaris one, I get this error: diskspace_check.sh: syntax error at line 3: `diskspace=$' unexpected I assume... (2 Replies)
Discussion started by: cara_k
2 Replies

9. Shell Programming and Scripting

bash syntax error: command not found

I am trying to create a shell that asks the user to enter their name, and compare it to my own by saying we have the same name or saying my name and that they have a nice name too. Here is my script... #!/bin/bash-x echo "Enter your name". read name if then echo "My name is Adam too"... (1 Reply)
Discussion started by: amaxey45
1 Replies

10. Shell Programming and Scripting

BASH Script syntax error

I'm trying to write a simple script that takes all the .tar.gz files in a directory and verifies them by using the gzip -tv command: for zip in *.tar.gz do gzip -tv $zip if ; then #Check return code from tar echo "File ${zip} verified OK." exit... (4 Replies)
Discussion started by: kelldan
4 Replies
Login or Register to Ask a Question