LinuxQuestions.org
Visit Jeremy's Blog.
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 11-09-2015, 09:04 AM   #1
kjeska
LQ Newbie
 
Registered: Nov 2015
Posts: 15

Rep: Reputation: Disabled
Schooltask - Make a pyramid of numbers


Hi!

In school we just received a task.

The program can be started with numbers parameter between 1 and 9. If it is not given parameter, the program will ask user about a number between 1 and 9. Then the program should print "pyramid" using "double loop" - loop inside loop :
1
22
333
4444

etc. to spoken numbers. If you manage to get this variation is even better:
1
22
333
4 4 4 4
etc. To spoken numbers.

Can somebody help? We are a group of 4 totally new to the linux commander.

Thanks!
 
Old 11-09-2015, 09:09 AM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,219

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
Which language are you expected to use?
 
Old 11-09-2015, 09:10 AM   #3
kjeska
LQ Newbie
 
Registered: Nov 2015
Posts: 15

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by dugan View Post
Which language are you expected to use?
Bin/bash

Shellscripting :-)
 
Old 11-09-2015, 09:52 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So completely new to a class teaching you how to do scripting (amongst other things). So what have you done so far? Do you know how to ask the user for information? Do you know how to store that information? Do you know how to check that information is in the range 1 to 9 or that it is even a number??

I imagine the teacher did not set this on the first day without teaching you anything, so help us to understand where you are up to?
 
Old 11-09-2015, 11:11 AM   #5
kjeska
LQ Newbie
 
Registered: Nov 2015
Posts: 15

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
So completely new to a class teaching you how to do scripting (amongst other things). So what have you done so far? Do you know how to ask the user for information? Do you know how to store that information? Do you know how to check that information is in the range 1 to 9 or that it is even a number??

I imagine the teacher did not set this on the first day without teaching you anything, so help us to understand where you are up to?
I know some few things, as making easy scripts. He went through alot in a very short time, so it wasn't easy at all.
We don't know how to ask the user for information or how to store it.
"help us to understand where you are up to?" We are making a program with shellscripting?

We have done this so far:

Quote:
#!/bin/bash
for i in {1..5}; do
for j in $(eval echo {1..$i}); do
echo -n $i
done
echo
done
I just asked for help, not for people to do the task for us. If that wasn't clear enough, i'm pointing that out here.

Last edited by kjeska; 11-09-2015 at 11:13 AM.
 
Old 11-09-2015, 12:02 PM   #6
hortageno
Member
 
Registered: Aug 2015
Distribution: Ubuntu 22.04 LTS
Posts: 240

Rep: Reputation: 67
Quote:
Originally Posted by kjeska View Post
We have done this so far:
Code:
#!/bin/bash
for i in {1..5}; do
for j in $(eval echo {1..$i}); do
echo -n $i
done
echo
done
I just asked for help, not for people to do the task for us. If that wasn't clear enough, i'm pointing that out here.
That's more than half the job done, very good.

Now you have to check whether the first argument "$1" is between 1 and 9. If yes, assign it to x and use it as upper boundary for the first loop. Else "read x" from standard input. I assume that you already you had "if ... then ... else ... fi" in your lesson. That should bring you a bit further.
 
Old 11-09-2015, 01:13 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
1. Telling the user what to do :- see echo

2. Getting information from the user :- see read

3. Checking the user gave you information when starting the script :- google bash command line parameters and look for $@, $# and $1..$N

4. Testing how many parameters you got :- see test and associated [[]] or (())

5. Checking you received a number :- google testing variable contains a number, this may lead you to =~ as an option

6. Check here for reasons not to use eval

7. As an alternative to your eval and a sequence, try looking up a for loop control, using the form :- counter; counter expression number;counter[inc/dec]

Hope some of that helps too. Below is a site you may wish to work through:

http://tldp.org/LDP/abs/html/ (don't let the name put you off )
 
Old 11-09-2015, 06:00 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,352

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
This is also a guide cli tutorial http://rute.2038bug.com/index.html.gz

You may also want to try writing test scripts for each of the things mentioned by grail, before building the whole script in one.
This way you understand how each concept works.

FYI add this at the top of your scripts to see what is going on
Code:
#!/bin/bash
set -xv     # this line shows the parser in action
 
Old 11-10-2015, 02:57 AM   #9
Kjakan
LQ Newbie
 
Registered: Nov 2015
Posts: 1

Rep: Reputation: Disabled
Dette blir sett paa som juks.

Hilsen Tor L
 
Old 11-10-2015, 05:14 AM   #10
kjeska
LQ Newbie
 
Registered: Nov 2015
Posts: 15

Original Poster
Rep: Reputation: Disabled
Thanks guys! Much appriciated :-)

Will post the result here when we are done.
 
Old 11-10-2015, 06:02 AM   #11
kjeska
LQ Newbie
 
Registered: Nov 2015
Posts: 15

Original Poster
Rep: Reputation: Disabled
Now we figured out how to ask the user first.

We are still stuck on this though:

#!/bin/bash
# Test av posisjons-parametre
echo "velg et tall mellom 1 og 9:"
read


for i in {1..5}; do
for j in $(eval echo {1..$i}); do
echo -n $i
done
echo
done

exit 0
 
Old 11-10-2015, 06:24 AM   #12
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Well, you stated that:
Quote:
Originally Posted by kjeska
If it is not given parameter, the program will ask user about a number between 1 and 9
In other words, what you need to do FIRST is to check if an argument was passed or not. If an argument was NOT passed, then you should ask for user input.

I will give you a hint with regards to the first problem (checking for an argument):
Code:
$#
Quote:
Originally Posted by Kjakan
Dette blir sett paa som juks.

Hilsen Tor L
^If you are the instructor, then you ought to know that using forums to ask for help has been done in the GNU/Linux community since day one. This is not cheating, this is asking for help. Furthermore, much respect to the OP for clearly stating in the first post that this is a school project.

Best regards,
HMW
 
Old 11-10-2015, 06:28 AM   #13
kjeska
LQ Newbie
 
Registered: Nov 2015
Posts: 15

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by HMW View Post
Well, you stated that:


In other words, what you need to do FIRST is to check if an argument was passed or not. If an argument was NOT passed, then you should ask for user input.

I will give you a hint with regards to the first problem (checking for an argument):
Code:
$#


^If you are the instructor, then you ought to know that using forums to ask for help has been done in the GNU/Linux community since day one. This is not cheating, this is asking for help. Furthermore, much respect to the OP for clearly stating in the first post that this is a school project.

Best regards,
HMW
Thanks! It was not the teacher actually. Just someone in my class trolling.. But if it was - thanks for pointing out that this isn't cheating. We are looking for help to learn and to be as well prepared for exams as possible.

From what I understood we forgot to write parameters. $# for every number up to 9?
 
Old 11-10-2015, 06:36 AM   #14
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by kjeska View Post
From what I understood we forgot to write parameters. $# for every number up to 9?
No, that's not what I meant.

Here, check this out:
https://lmddgtfy.net/?q=bash%20comma...ne%20arguments
 
Old 11-10-2015, 08:13 AM   #15
kjeska
LQ Newbie
 
Registered: Nov 2015
Posts: 15

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by HMW View Post
No, that's not what I meant.

Here, check this out:
https://lmddgtfy.net/?q=bash20comman...ne%20arguments
Okey.

We are struggeling to make the program stop on the number we are choosing.

When we have 1-9 and choose 5, the program or pyramid should stop on 5, but it keeps going till 9.

This is so hard. Been stuck for hours. Sorry for dumb questions.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] How to make a diamond with progressing & rectracting numbers andrew.comly Programming 13 12-02-2014 10:55 AM
making a number pyramid imran042 Programming 20 06-16-2012 11:02 AM
gcc and linux pyramid board shabi Linux - Embedded & Single-board computer 3 06-23-2010 11:42 PM
How do i make a script that checks that my input is letters or numbers ? ministeren Programming 13 05-20-2010 09:36 AM
how do I make lpr always print page numbers fakie_flip Linux - Software 2 11-30-2007 07:03 PM

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

All times are GMT -5. The time now is 11:22 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