![]() The bc command is probably one of the best tools for doing calculations on the command line. $ for i in `jot 7 17` do echo April $i done The jot command can be useful if you want to iterate through a series of numbers to create a list for some other purpose. Here we're asking it to decrease the numbers by telling it we want to stop when we get to 2: $ jot 8 10 2 Provide it with the number of values you want to see and the number that you want to start with. The jot command allows you to create a list of numbers. NOTE: The factor command didn't get very far on factoring that last value because 1987 is a prime number. You feed it a number, and it tells you what its factors are. The factor command works just like you'd probably expect. That sure isn't going to help with sales! With a small change, this would work as we'd expect: #!/bin/bash The example below shows that using expr in this kind of context doesn't work because if works with the opposite orientation (0=true). Of course, having true comparisons resulting in 1 and false resulting in 0 goes against what we generally expect on Linux systems. In the following examples, we're asking whether 10 is greater than 5 and, then, whether it's greater than 99. Putting the numbers in quotes doesn't mean that you'd actually be doing a string comparison rather than a numeric one - unless one of the arguments is clearly a string. The % operator is for modulo calculations. ![]() Notice that you have to use a \ character in front of * to keep the shell from interpreting the asterisk as a reference to all files in the current directory and in front of > to keep it from being used to redirect the command's output. Performing a simple calculations and comparisons $ expr 11 123 ![]() Here are some examples: Incrementing a variable $ count=0 It can manage addition, subtraction, division, and multiplication. exprįirst and probably the most obvious and commonly used command for performing mathematical calculations on the command line is the expr (expression) command. Let's look at some very useful commands and syntax for command line math. Can you do math on the Linux command line? You sure can! In fact, there are quite a few commands that can make the process easy and some you might even find interesting. ![]()
0 Comments
Leave a Reply. |