LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-18-2015, 10:16 AM   #1
ianconner
LQ Newbie
 
Registered: Dec 2015
Posts: 2

Rep: Reputation: Disabled
need help with my first script


PHP Code:
Hey thereI'm fairly new to Linux (actually sitting in a Linux + class right now) and very new to scripting in Linux.

I keep getting a syntax error saying "Near Unexpected toke n `do'" On Line 15

Here is a copy of my script

#!/bin/bash
MAX_NO=0
echo -n "
Enter Number between (5 to 9) : "
read MAX_NO
if ! [ 
$MAX_NO -ge 5 -a $MAX_NO -le 9 ] ; then
   echo "
WTF... I ask to enter number between 5 and 9, Try Again"
   exit 1
fi
clear
for (( i=1; i<=MAX_NO; i++ )) do     for (( s=MAX_NO; s>=i; s-- ))
    do
       echo -n " "
    done
    for (( j=1; j<=i;  j++ ))     do      echo -n " 
."      done     echo "" done ###### Second stage ###################### for (( i=MAX_NO; i>=1; i-- ))
do
    for (( s=i; s<=MAX_NO; s++ ))
    do
       echo -n " "
    done
    for (( j=1; j<=i;  j++ ))
    do
     echo -n " 
."
    done
    echo ""
done



Any help would be appreciated!! 

Last edited by ianconner; 12-18-2015 at 10:18 AM.
 
Old 12-18-2015, 10:34 AM   #2
Mr. Alex
Senior Member
 
Registered: May 2010
Distribution: No more Linux. Done with it.
Posts: 1,238

Rep: Reputation: Disabled
Quote:
###### Second stage ######################
After "#" symbol everything is considered a comment and is not executed. You have to remove comments from your script or move them somewhere where there is no executable code.
 
1 members found this post helpful.
Old 12-18-2015, 10:38 AM   #3
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
To start please don't wrap your entire post in code tags just around the code bit.
You have a peculier coding style which is causing the problems, with bash if you put more than one statement on a line you must use the ; as a seperator, or better yet stick to one patricular style if you want your do/done loops on seperate line do it thoughout the script, other wise it is awkward to follow and you get syntax errors, your code when smartened up works fine like so
Code:
#!/bin/bash -e

#©keithhedger Fri 18 Dec 16:30:09 GMT 2015 kdhedger68713@gmail.com
#!/bin/bash
MAX_NO=0
echo -n "Enter Number between (5 to 9) : "
read MAX_NO
if ! [ $MAX_NO -ge 5 -a $MAX_NO -le 9 ] ; then
   echo "WTF... I ask to enter number between 5 and 9, Try Again"
   exit 1
fi
clear
for (( i=1; i<=MAX_NO; i++ ))
	do
		for (( s=MAX_NO; s>=i; s-- ))
    do
       echo -n " "
    done
    for (( j=1; j<=i;  j++ ))
    do
    	echo -n " ."
    done
    	echo ""
    done
###### Second stage ######################
    for (( i=MAX_NO; i>=1; i-- ))
	do
    	for (( s=i; s<=MAX_NO; s++ ))
    do
       echo -n " "
    done
    for (( j=1; j<=i;  j++ ))
    do
     echo -n " ."
    done
    echo ""
done
I haven't actual changed any code just seperated the commands onto their own lines.
 
1 members found this post helpful.
Old 12-18-2015, 10:39 AM   #4
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
Quote:
Originally Posted by Mr. Alex View Post
After "#" symbol everything is considered a comment and is not executed. You have to remove comments from your script or move them somewhere where there is no executable code.
Yeah that as well!
 
1 members found this post helpful.
Old 12-18-2015, 11:03 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Others have answered the issue, here are some other points:

1. Be consistent with testing, ie. if you are going to use (()) for testing numbers, like in your for loops, do not start your script by using []. Especially as (()) is designed specifically for dealing with numbers
Code:
if ! [ $MAX_NO -ge 5 -a $MAX_NO -le 9 ] ; then

if ! (( MAX_NO >= 5 && MAX_NO <= 9 )) ; then
2. As above with placing 'do' on same line or on next line, if you are going to place on the next line then place 'then' for your 'if' on the next line too
Code:
if ! (( MAX_NO >= 5 && MAX_NO <= 9 )) ; then

if ! (( MAX_NO >= 5 && MAX_NO <= 9 ))
then
3. Your 'if' advises the user to try again, but to do this they currently need to re-run the script. Maybe try putting the test in a loop. Remember though, if you do this, you might also need to offer
them a way out

4. I would also suggest having a look at how you might reduce your code from needing 6 for loops (hint: if you search this site for something like 'pyramid', I think you might find some alternatives)

My last thought is a question ... your current solution repeats the longest line, ie. if you user enters 5 then they get a diamond shape with 2 rows of 5 dots in the center. Unless this was the question
asked, I would think the diamond should be uniform and only have a single row of maximum dots (even if not, it would make a good challenge for you to work out how to solve )
 
1 members found this post helpful.
Old 12-18-2015, 11:17 AM   #6
ianconner
LQ Newbie
 
Registered: Dec 2015
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thank you for the help and guidance. This is my first time scripting in any language or OS. That cleared up the issue and ill work on condensing and modifying. I just wanted to get it to work as a baseline and then go in a tweak things to see how changing things will create different results.
 
Old 12-18-2015, 11:49 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
All good Just to let you know I had a quick play and managed to use a single for loop.

Some hints to look into:

1. Arrays

2. printf

3. IFS variable

4. ternary operator (?:)
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script/Perl Script to remove the string until it finds special character '_' pooppp Programming 10 07-17-2012 09:36 AM
Shell script, Perl script, command or utility to convert Binary to text Perseus Programming 26 07-12-2012 06:00 AM
[SOLVED] bash and xterm: how make apps started by and for a script persist when script terminates porphyry5 Linux - General 4 06-15-2011 01:27 PM
[SOLVED] Script question: create a shell script in kde to log in on a server with ssh c4719929 Linux - Newbie 1 01-31-2011 03:05 AM
How to get full path to script file inside script itself? And in case of sym links? maggus Linux - Newbie 3 05-28-2009 08:40 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration