LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-22-2006, 01:57 PM   #1
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Rep: Reputation: 47
Blinking output


The following program doesn't work. I just get a blinking window. What is the problem? Your help is appreciated.
----------------------------------------------------------------

#!/bin/bash

x=0
while [ "$x" -lt 10 ] ; # this is loop1
do
y="$x"
while [ "$y" -ge 0 ] ; # this is loop2
do
echo "$y \c"
y=´echo "$y - 1" | bc´
done
echo
x=´echo "$x + 1" | bc´
done
--------------------------------------

The expected output of the above program is as follows:

0
1 0
2 1 0
3 2 1 0
4 3 2 1 0
5 4 3 2 1 0
6 5 4 3 2 1 0
7 6 5 4 3 2 1 0
8 7 6 5 4 3 2 1 0
9 8 7 6 5 4 3 2 1 0

---------------------------------------------------------------------------
[ I just get a blinking window and I can't copy and past here. Something with the line 15.]
 
Old 07-22-2006, 02:24 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Code:
#!/bin/bash

x=0
while [ $x -lt 10 ]; do
  y=$x
  while [ $y -ge 0 ]; do
    echo -n "$y"
    y=`expr $y - 1`
  done
  echo
  x=`expr $x + 1`
done
echo "Done."
Quote:
0
10
210
3210
43210
543210
6543210
76543210
876543210
9876543210
Done.
'Hope that helps .. PSM

PS:
Please start using "[code]" tags!
 
Old 07-22-2006, 04:17 PM   #3
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
Thanks paulsm4 for the reply. You are very clever at scripting; I am not flattering you. I wish I could write scripts like you.

I must ask some questions.
x=0 [ Here we initialize the variable x. So it is initialized to 0.]

y=$x [ Here we initialíze the the variable y to x. Obviously x too is a variable.]





echo -n "$y"
y=`expr $y - 1` [ These two lines creates the vertical line.]



echo
x=`expr $x + 1` [ This creates the horizontal lines.]

Please tell me if I understood incorrectly. I have a limited knowledge of scripting.
 
Old 07-22-2006, 04:26 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Quote:
I must ask some questions.

x=0 [ Here we initialize the variable x. So it is initialized to 0.]
Yes, exactly right!

y=$x [ Here we initialíze the the variable y to x. Obviously x too is a variable.]
Yes again

echo -n "$y"
y=`expr $y - 1` [ These two lines creates the vertical line.]
No. I would prefer to say:
a) the first line displays "y" (without writing a newline)
b) the second line decrements "y"

...
Thank you for the kind words, and I hope that helps .. PSM

PS:
You'll note that I *didn't* leave a space between the numbers.
This is easily accomplished. You can either put a space in "echo" (as you did above), or you can use a tool like "awk" (which permits more sophisticated, "printf-sytle" formatting).

Last edited by paulsm4; 07-22-2006 at 04:28 PM.
 
Old 07-22-2006, 04:34 PM   #5
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
Thanks paulsm again for taking time to reply me.

echo -n "$y" [ What is the use of ' -n ' here?]

Because echo is a command to show some texts. Please read the following. It didn't make any difference when I inserted ' -n ' .


-------------------------
[nissanka@c83-250-110-112 ~]$ echo paulsm
paulsm
[nissanka@c83-250-110-112 ~]$


[nissanka@c83-250-110-112 ~]$ echo -n paulsm
paulsm[nissanka@c83-250-110-112 ~]$
 
Old 07-22-2006, 11:15 PM   #6
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Try executing the above script with - and without - "echo -n" and compare what happens ;-)

Last edited by paulsm4; 07-22-2006 at 11:19 PM.
 
Old 07-23-2006, 11:46 AM   #7
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
Paulsm4

It doesn't work when I altered it. Please read the following:


#!/bin/bash

x=0
while [ $x -lt 10 ]; do
y=$x
while [ $y -ge 0 ]; do
echo n "$y"
y=`expr $y - 1`
done
echo
x=`expr $x + 1`
done
echo "Done




n 0

n 1
n 0

n 2
n 1
n 0

n 3
n 2
n 1
n 0

n 4
n 3
n 2
n 1
n 0

n 5
n 4
n 3
n 2
n 1
n 0

n 6
n 5
n 4
n 3
n 2
n 1
n 0

n 7
n 6
n 5
n 4
n 3
n 2
n 1
n 0

n 8
n 7
n 6
n 5
n 4
n 3
n 2
n 1
n 0

n 9
n 8
n 7
n 6
n 5
n 4
n 3
n 2
n 1
n 0
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------



#!/bin/bash

x=0
while [ $x -lt 10 ]; do
y=$x
while [ $y -ge 0 ]; do
echo "$y"
y=`expr $y - 1`
done
echo
x=`expr $x + 1`
done
echo "Done."




[nissanka@c83-250-110-112 ~]$ ./unwanted20
0

1
0

2
1
0

3
2
1
0

4
3
2
1
0

5
4
3
2
1
0

6
5
4
3
2
1
0

7
6
5
4
3
2
1
0

8
7
6
5
4
3
2
1
0

9
8
7
6
5
4
3
2
1
0

[Simply speaking -n stops creating new lines.]

Last edited by Gins; 07-23-2006 at 12:18 PM.
 
Old 07-23-2006, 03:58 PM   #8
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
I still can't fathom out how this right-angled triangle forms.I understand all the codes and what they are doing.


Are you able to shed some light on this? I know it is a tall order to explain these things in an on line forum.
 
Old 07-23-2006, 04:04 PM   #9
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Please cut/paste this script:
Code:
#!/bin/bash
x=0
while [ $x -lt 10 ]; do
  y=$x
  while [ $y -ge 0 ]; do
    echo -n "$y"
    y=`expr $y - 1`
  done
  echo
  x=`expr $x + 1`
done
QUESTION: Now execute it.
Does it produce this output?
Quote:
0
10
210
3210
43210
543210
6543210
76543210
876543210
9876543210
<= I HOPE THE ANSWER IS "YES"
Now remove the "-n" and re-run the script:
Code:
#!/bin/bash
x=0
while [ $x -lt 10 ]; do
  y=$x
  while [ $y -ge 0 ]; do
    echo "$y"
    y=`expr $y - 1`
  done
  echo
  x=`expr $x + 1`
done
The output from this version looks a lot like your output above, doesn't it?

Try this:
Quote:
man echo
<= WHAT DOES THE "MAN" PAGE SAY ABOUT "-n"
Finally, let's consider an approach that doesn't involve "echo -n" at all:
Code:
#!/bin/bash
X=0
while [ $X -lt 10 ]; do
  Y=$X
  S=""
  while [ $Y -ge 0 ]; do
    S="$S $Y"
    Y=`expr $Y - 1`
  done
  echo $S
  X=`expr $X + 1`
done
Quote:
0
1 0
2 1 0
3 2 1 0
4 3 2 1 0
5 4 3 2 1 0
6 5 4 3 2 1 0
7 6 5 4 3 2 1 0
8 7 6 5 4 3 2 1 0
9 8 7 6 5 4 3 2 1 0
Does that help?

PS:
Code blocks are Good. Please start using code blocks.

PPS:
Please cut/paste this one final example and try it out:
Code:
#!/bin/bash
X=0
while [ $X -lt 10 ]; do
  Y=$X
  S=""
  while [ $Y -ge 0 ]; do
    S="$S -- $Y"
    Y=`expr $Y - 1`
  done
  echo $S
  X=`expr $X + 1`
done
Does this help explain "the right angled triangle" question?????

Last edited by paulsm4; 07-23-2006 at 04:17 PM.
 
Old 07-23-2006, 04:42 PM   #10
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
Paulsm4
Thanks for taking time to give me a excellent lengthy reply. Now it is too late here. I am a bit tired because I trained today. As a matter of fact I am training 3 times a week at a gym. I must sleep now.

I will attend this tomorrow. So please keep a good watch on this thread.
Are you based in the USA? If you are in the USA, it is not too late for you.
 
Old 07-23-2006, 06:49 PM   #11
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by Gins
The expected output of the above program is as follows:

0
1 0
2 1 0
3 2 1 0
4 3 2 1 0
5 4 3 2 1 0
6 5 4 3 2 1 0
7 6 5 4 3 2 1 0
8 7 6 5 4 3 2 1 0
9 8 7 6 5 4 3 2 1 0
here's a simpler approach. please try it.

Code:
#!/bin/bash

for ((a = 0; a <= 9; a++)); do
	echo "$a $b"
	b="$a $b"
done
OR
Code:
#!/bin/bash

MAXNUMBER=9

for ((a = 0; a <= MAXNUMBER; a++)); do
	b=$a
	until [[ $b < 0 ]]; do
		echo -n "$b "
		(( b-- ))
	done
	echo
done
 
Old 07-24-2006, 02:47 PM   #12
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
I thank both paulsm4 and konsolebox for the help. I am very proud of your knowledge of programming.

Our friend konsolebox has done the same programme in two different ways. Oh! What a talent!
The way konsolebox has made programmes reminds me of the codes use in C++ programming. I worked with C++ programming some years ago. One of his programmes has only 4 lines; it works fine.


The following programme from paulsm4 works fine too.
#!/bin/bash
x=0
while [ $x -lt 10 ]; do
y=$x
while [ $y -ge 0 ]; do
echo "$y"
y=`expr $y - 1`
done
echo
x=`expr $x + 1`
done


So if you know all the pros and cons of programming, you could write a same programme in many different ways. I wish I could write programmes like paulsm4 and konsolebox.
 
  


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
Blinking GL Screensavers mattdev121 Linux - Software 1 07-26-2005 10:54 AM
post blinking... o2_sparx Linux - Laptop and Netbook 4 07-13-2005 01:30 AM
Blinking Install Screen BigAmp Linux - Laptop and Netbook 10 03-29-2005 04:28 PM
Blinking dir jsolanky Linux - Software 1 12-03-2004 02:31 PM
slowing down blinking cursor jester_69 Linux - General 0 02-25-2004 06:43 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:09 PM.

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