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.
hello, guys. i am new to linux shell script. i have a problem about math scripting. how to write a shell script " 1x1+2x2+3x3+...+100x100= " . and can i use shift command in shell script to have this "1+3+5+7+...+99" work?
There are many shells; which one are you using? Why would you want to use shift for this?
Shells are usually limited to integer math. For example, Bash can do
Code:
sum=0; for (( i=1; i<=100; i++ )); do sum=$((sum + i*i)) ; done ; echo $sum
and
Code:
sum=0; for (( i=1; i<=100; i+=2 )); do sum=$((sum + i)) ; done ; echo $sum
In general, it is better to use something like awk or bc to do the math instead. In this case, bc is the most suitable candidate, as it is an arbitrary-precision programmable calculator. For example, try
Code:
echo 'sum=0; for (i=1; i<=100; i++) sum += i*i; print sum, "\n"' | bc
and
Code:
echo 'sum=0; for (i=1; i<=100; i+=2) sum += i; print sum, "\n"' | bc
There are many shells; which one are you using? Why would you want to use shift for this?
Shells are usually limited to integer math. For example, Bash can do
Code:
sum=0; for (( i=1; i<=100; i++ )); do sum=$((sum + i*i)) ; done ; echo $sum
and
Code:
sum=0; for (( i=1; i<=100; i+=2 )); do sum=$((sum + i)) ; done ; echo $sum
In general, it is better to use something like awk or bc to do the math instead. In this case, bc is the most suitable candidate, as it is an arbitrary-precision programmable calculator. For example, try
Code:
echo 'sum=0; for (i=1; i<=100; i++) sum += i*i; print sum, "\n"' | bc
and
Code:
echo 'sum=0; for (i=1; i<=100; i+=2) sum += i; print sum, "\n"' | bc
See man 1 bc for details.
thanks so much. i use bash shell as my default shell. i just wanna know more about linux shell scripts. but you did not answer my second question : could i use shift command on this test ?
Last edited by jack_green; 11-19-2011 at 10:54 PM.
Yes, there is a way you can calculate the sums in a way that utilizes the shift built-in. It involves a function (or a separate script if the shell does not support functions -- Bash does), which sums its arguments (or squares of its arguments), and echoes the results.
shift is useful in looping over all arguments in a while loop, although one could use a for loop over the arguments as well.
To get the result, you would need to call the function with all summands as arguments, but the seq utility is useful for that.
The solution using shift is almost a dozen lines of Bash. I cannot imagine why you would want to do it that way -- unless this is homework.
Yes, there is a way you can calculate the sums in a way that utilizes the shift built-in. It involves a function (or a separate script if the shell does not support functions -- Bash does), which sums its arguments (or squares of its arguments), and echoes the results.
shift is useful in looping over all arguments in a while loop, although one could use a for loop over the arguments as well.
To get the result, you would need to call the function with all summands as arguments, but the seq utility is useful for that.
The solution using shift is almost a dozen lines of Bash. I cannot imagine why you would want to do it that way -- unless this is homework.
Is this homework?
sorry, it's not my homework . i just happened to heard that shift can use for looping , but i don't understand how to use it.
it said that VAR=1,VAR=2,VAR=3... after "shift", it should be VAR1=2,VAR2=3..... but how to use the position parameter for looping . i am a totally shell script beginner.
sumargs () {
local sum=0
while [ $# -gt 0 ]; do
sum=$(( sum + $1 ))
shift 1
done
echo $sum
}
sqrargs () {
local sum=0
while [ $# -gt 0 ]; do
sum=$(( sum + $1 * $1 ))
shift 1
done
echo $sum
}
sqrargs $(seq 1 100)
sumargs $(seq 1 2 100)
$# evaluates the number of positional parameters -- command-line parameters to a script, or parameters if in a function. First one is $1, second $2 and so on. shift N will discard the first N parameters, and rename the leftovers starting from one ($1). It only works for positional parameters, not for variables in general.
The while loops above can be read as "as long as there are more than zero positional parameters, do ..., then discard the first positional parameter shifting the rest one place down." This results in $1 cycling through each parameter given to the function.
seq firstlast would output each integer number from first to last, inclusive.
seq firststeplast outputs first , first+step , first+step+step , and so on, up to and including last.
The $(...) is equivalent to backticks, and evaluates to the output of ... . Thus, the output from the seq is given to the function as positional parameters.
While Bash functions can return a number, it is limited to a very small range, usually between 0 and 255, inclusive, with 0 meaning success/true, and all others failure/false. (Nonzero values always indicate an error code, although the meaning of each code tends to be program-specific. There are lists and conventions, but nothing universal.)
Therefore the function really needs to output the result. I used echo. If I wanted to format the output, I'd use for example printf '%d\n' $sum instead.
To provide (hopefully) a clear and simple explanation:
Every command you enter consists of the command-name and zero or more arguments following it.
In shell scripting, the name of the command itself is stored in the special parameter $0, and the arguments following it in $1, $2, $3, etc.
The shift built-in command simply moves the contents of the parameters down the list. When executed, the contents of $1 are deleted and $2 moves over to become $1, $3 becomes $2, etc. ($0 remains unaffected). A number can also optionally be given so that it shifts multiple steps at once.
So depending on what you want to do, it is possible to use shift as a kind of looping device, but it's far from the most usual option, as it's both limited and destructive to your data. Your shell provides much better ways to iterate through multiple inputs.
Here are a few useful bash scripting references for you to study. I particularly recommend the first as a good introduction into the basics:
sumargs () {
local sum=0
while [ $# -gt 0 ]; do
sum=$(( sum + $1 ))
shift 1
done
echo $sum
}
sqrargs () {
local sum=0
while [ $# -gt 0 ]; do
sum=$(( sum + $1 * $1 ))
shift 1
done
echo $sum
}
sqrargs $(seq 1 100)
sumargs $(seq 1 2 100)
$# evaluates the number of positional parameters -- command-line parameters to a script, or parameters if in a function. First one is $1, second $2 and so on. shift N will discard the first N parameters, and rename the leftovers starting from one ($1). It only works for positional parameters, not for variables in general.
The while loops above can be read as "as long as there are more than zero positional parameters, do ..., then discard the first positional parameter shifting the rest one place down." This results in $1 cycling through each parameter given to the function.
seq firstlast would output each integer number from first to last, inclusive.
seq firststeplast outputs first , first+step , first+step+step , and so on, up to and including last.
The $(...) is equivalent to backticks, and evaluates to the output of ... . Thus, the output from the seq is given to the function as positional parameters.
While Bash functions can return a number, it is limited to a very small range, usually between 0 and 255, inclusive, with 0 meaning success/true, and all others failure/false. (Nonzero values always indicate an error code, although the meaning of each code tends to be program-specific. There are lists and conventions, but nothing universal.)
Therefore the function really needs to output the result. I used echo. If I wanted to format the output, I'd use for example printf '%d\n' $sum 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.