ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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 ?
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.
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
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.
# 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.
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.
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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.