LinuxQuestions.org
Review your favorite Linux distribution.
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 11-04-2014, 08:46 AM   #1
glekar
LQ Newbie
 
Registered: Sep 2014
Posts: 6

Rep: Reputation: Disabled
Create a small script


Hi!

I'm trying to learn how to make simple shell scripts. It is a bit tricky :-)

Problem number one: I need this output :
If user input is a number between 1-9
1 if input is 1
2 2 if 2 ,1 and 2 etc
3 3 3
4 4 4 4 and son on until 9.
Finally it would be great if it returned the numbers in a pyramid shape.

Any help is appreciated.

GLenn
 
Old 11-04-2014, 08:49 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
not really sure what you need from your description. can you post what you did so far and what errors you are getting and perhaps someone will be able to help debug ?
 
Old 11-04-2014, 09:20 AM   #3
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
This help?

Code:
echo -n "input the number: "
read input_number

count=0
while [ $count -le $input_number ]; do
        ((count++))
        echo -n "$input_number "
done
 
Old 11-04-2014, 10:20 AM   #4
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
Usual story, so us what you have done and where you are stuck?
 
Old 11-04-2014, 11:53 AM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by glekar View Post
Finally it would be great if it returned the numbers in a pyramid shape.
I have done this and am willing to post the code... but first, show us what you have done.

Daniel B. Martin
 
Old 11-06-2014, 08:17 AM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Hi glekar,

Welcome to the forums. As said by others, please post what you presently have. Further, shell scripting is not one language, there are a variety of ways to script and languages with which to do this. If you've chosen a particular language for now that's helpful to enable members to assist you more in line with the script language you're working with.

As you've said, you're trying to make simple shell scripts. Well, what you've written is simple to some but to a complete beginner, it is not a very basic script. It involves parsing input from somewhere, be that a file, passing arguments, or standard input. It involves decisions to accomodate a sort algorithm, formatting of output, and qualification of the output depending on the input.

Not saying this can't or shouldn't be a first attempt, but I'm saying what others are saying to you which is that you ought to try some level of the problem on your own and post what you've done to get further assistance.

The other part of this is, if you have very little script experience at all and might also have a difficult initial learning curve, then being shown some contrite, or complex, and complete solution might not be helpful to you except for the fact that you can run an offered example and view the result of someone else's efforts.

If you need references for shell scripting, please let us know. If you have some experience at writing shell scripts, then post what you have to support your question.
 
Old 11-19-2014, 01:36 PM   #7
glekar
LQ Newbie
 
Registered: Sep 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
This is where i am stuck. The below will return the numbers centered left. i need the output to be in a pyramide shape.
tall=number

#!/bin/bash

read -p "Tast inn et tall 1-9 > " tall

for (( i=1; $i <= $tall; i++ )); do

for (( j=1; $j <= $i; j++ )) ; do
echo -n $i

done
echo
done
 
Old 11-19-2014, 02:00 PM   #8
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by glekar View Post
This is where i am stuck. The below will return the numbers centered left. i need the output to be in a pyramide shape.
So you need to add some blanks. I don't know how to tell you how to figure the rest out yourself. I know giving the answer is not the best teaching method, but I don't know a better way at this point.
Code:
#!/bin/bash

read -p "Tast inn et tall 1-9 > " tall

for (( i=1; $i <= $tall; i++ )); do

for (( j=$i; $j < $tall; j++ )) ; do
echo -n ' '
done

for (( j=1; $j <= $i; j++ )) ; do
echo -n ' '$i

done
echo
done

Last edited by johnsfine; 11-19-2014 at 02:02 PM.
 
Old 11-19-2014, 02:01 PM   #9
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Firstly, please use [code][/code] tags, Advanced mode gives you a widget to do this with if you have trouble.

What I would do is modify the for loops by adding a step which echo's without a newline, SPACE characters up to the indentation amount you wish to have.

A big problem with fixed fonts is that you can't really have a perfectly shaped pyramid. And for even numbered entries, you can't do this either.

Example is if '2' is entered. Do you indent '1' by one space, or not indent it? Either case, the pyramid is never centered.

Next example is if '3' is entered. You can indent '1' by one space, leaving it perfectly centered above the '333' row. However do you indent '22' by one space, or not? Either case, there's "no" solution which gives you a perfect pyramid.

So then you'd have to add spaces in between each character to make it so that you could align properly.

Since the problem only involves 1-9, I'd stage that in an editor to determine how to line it all up properly, determine if this meets your criteria, and then code it by adding a loop which adds white spaces before the line begins and also adding white spaces between repeated characters.
 
Old 11-19-2014, 02:54 PM   #10
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
With this code ...
Code:
# Prompt for user input.
echo "Please enter an integer between 1 and 9, inclusive."
echo
read n
if [ $n -gt 9 ]; then
  echo "Out-of-range value was entered."
  echo "Execution ended."
  echo
  exit 7
fi

# n = number of rows in the output file.
awk -v n=$n 'BEGIN{refstr="        #########"  # Create a reference string.
  for (j=1;j<=n;j++)
    {outstr=substr(refstr,9-n+j,n);  # Extract a subset of the reference.  
     gsub(/#/,j" ",outstr);          # Replace hash marks with row numbers.
     print outstr}}' >$OutFile       # Print the output string.
... and a user-keyed value 7 the result is ...
Code:
      1 
     2 2 
    3 3 3 
   4 4 4 4 
  5 5 5 5 5 
 6 6 6 6 6 6 
7 7 7 7 7 7 7
Daniel B. Martin
 
1 members found this post helpful.
Old 11-19-2014, 04:29 PM   #11
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
To johnsfine ...

My bash skills are weak. I learn by reading posts on this forum and then tinkering with them. Your code ...
Code:
read -p "Tast inn et tall 1-9 > " tall

for (( i=1; $i <= $tall; i++ )); do

for (( j=$i; $j < $tall; j++ )) ; do
echo -n ' '
done

for (( j=1; $j <= $i; j++ )) ; do
echo -n ' '$i

done
echo
done
... generates the expected result. However, I didn't understand the reason for $ signs. I eliminated them to create this derivative version of your code ...
Code:
for (( i=1; i <= n; i++ )); do
  for (( j=$i; j < n; j++ )) ; do
    echo -n ' '
  done
  for (( j=1; j <= i; j++ )) ; do
    echo -n ' '$i
  done
  echo
done
... which also generates the expected result.

Is the code with the $ signs the proper practice? Why?

Daniel B. Martin

Last edited by danielbmartin; 11-19-2014 at 04:35 PM.
 
Old 11-20-2014, 03:18 AM   #12
glekar
LQ Newbie
 
Registered: Sep 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
Assigning a Value to a Variable
You can assign a value to a variable simply by typing the variable name followed by an equal sign and the value you want to assign to the variable. For example, if you wanted to assign a value of 5 to the variable count, you would enter the following command "count=5". With the bash syntax for setting a variable, you must make sure that there are no spaces on either side of the equal sign. Notice that you do not have to declare the variable as you would if you were programming in C or Pascal. This is because the shell language is a non-typed interpretive language. This means that you can use the same variable to store character strings that you use to store integers. You would store a character string into a variable in the same way that you stored the integer into a variable. For example "name=Garry " is also completely valid.

Accessing the Value of a Variable
Once you have stored a value into a variable, how do you get the value back out? You do this in the shell by preceding the variable name with a dollar sign ($). If you wanted to print the value stored in the count variable to the screen, you would do so by entering the command "echo -n $count". If you omitted the $ from the preceding command, the echo command would display the word count on-screen. Also, here "-n" is an option for echo not to append a carriage return to the end of the line.
 
Old 11-20-2014, 03:19 AM   #13
glekar
LQ Newbie
 
Registered: Sep 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thank you all for helping out!
/gik
 
Old 11-20-2014, 06:17 AM   #14
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by danielbmartin View Post
My bash skills are weak. I learn by reading posts on this forum and then tinkering with them. Your code ...

... generates the expected result. However, I didn't understand the reason for $ signs.
My bash skills are even weaker.

But programming is programming. I just took the bash script in post #7 and made the obvious corrections to its behavior. I did not worry about about the syntax, since the script as posted executed with no obvious syntax problem.
 
Old 11-20-2014, 09:02 AM   #15
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
Dollar ($) signs are not required inside (()) for variables (of course positional parameters and special characters like $@ still need them).
Many still prefer to always prefix the $ sign as at a quick glance they know immediately what is a variable, but neither way is incorrect.

Here is a slightly modified version of the same in bash:
Code:
#!/usr/bin/env bash

n=$1

for ((i = 1; i <= n; i++))
do
	for ((j = 1; j <= i; j++ ))
	do
		printf "%$(( j == 1 ? n - i + 1 : 2 ))d" $i
	done
	echo
done
This simply cut down the need for one extra loop

If you really want to get excited, see if you use your solution for Pascal's Triangle instead
 
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
create small linux box for environment monitoring? vbsaltydog Linux - Hardware 7 03-15-2009 11:36 AM
Best way to create a small wireless network. Master Fox General 2 10-27-2008 06:55 PM
URGENT: how to create a small LAN karthikg356 Linux - Networking 2 12-26-2007 11:43 PM
Looking for SMALL program to create graphs Mike Davies Linux - Software 7 07-01-2007 06:49 AM
How to create astlinux-like small distro? Newbie question garby Linux From Scratch 3 01-01-2007 03:33 PM

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

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