{"id":186,"date":"2018-10-19T12:00:00","date_gmt":"2018-10-19T04:00:00","guid":{"rendered":"https:\/\/yeslq.com\/201810186.html"},"modified":"2019-05-07T10:43:36","modified_gmt":"2019-05-07T02:43:36","slug":"the-linux-command-line-keyboard-inpu","status":"publish","type":"post","link":"https:\/\/yeslq.com\/?p=186","title":{"rendered":"The Linux Command Line&#8212;Keyboard Input And Arithmetic"},"content":{"rendered":"<h1 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\n<i style=\"font-size: x-small;\"><a href=\"http:\/\/linuxcommand.org\/tlcl.php\" style=\"color: #002740;\">The Linux Command Line&nbsp;<\/a><\/i><span style=\"font-size: xx-small;\">by William Shotts<\/span><\/h1>\n<div>\n<span style=\"font-size: xx-small;\"><br \/><\/span><\/div>\n<div>\n<span style=\"font-size: xx-small;\"><\/p>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nUp to now, our scripts have not been interactive. That is, they did not require any input from the user. In this lesson, we will see how your scripts can ask questions, and get and use responses.<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\nread<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nTo get input from the keyboard, you use the&nbsp;<a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/readh.html\" style=\"color: #002740;\">read<\/a>&nbsp;command. The&nbsp;read&nbsp;command takes input from the keyboard and assigns it to a variable. Here is an example:<\/div>\n<div class=\"codeexample\" style=\"background-color: #e0e0e0; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\">#!\/bin\/bash\n\necho -n \"Enter some text &gt; \"\nread text\necho \"You entered: $text\"\n       \n<\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nAs you can see, we displayed a prompt on line 3. Note that &#8220;-n&#8221; given to the&nbsp;echo&nbsp;command causes it to keep the cursor on the same line; i.e., it does not output a linefeed at the end of the prompt.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nNext, we invoke the&nbsp;read&nbsp;command with &#8220;text&#8221; as its argument. What this does is wait for the user to type something followed by a carriage return (the Enter key) and then assign whatever was typed to the variable&nbsp;text.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nHere is the script in action:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;read_demo.bash<br \/>Enter some text &gt;&nbsp;this is some text<br \/>You entered: this is some text<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nIf you don&#8217;t give the&nbsp;read&nbsp;command the name of a variable to assign its input, it will use the environment variable&nbsp;REPLY.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nThe&nbsp;read&nbsp;command also takes some command line options. The two most interesting ones are&nbsp;-t&nbsp;and&nbsp;-s. The&nbsp;-t&nbsp;option followed by a number of seconds provides an automatic timeout for the&nbsp;read&nbsp;command. This means that the&nbsp;read&nbsp;command will give up after the specified number of seconds if no response has been received from the user. This option could be used in the case of a script that must continue (perhaps resorting to a default response) even if the user does not answer the prompts. Here is the&nbsp;-t&nbsp;option in action:<\/div>\n<div class=\"codeexample\" style=\"background-color: #e0e0e0; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\">#!\/bin\/bash\n\necho -n \"Hurry up and type something! &gt; \"\nif read -t 3 response; then\n    echo \"Great, you made it in time!\"\nelse\n    echo \"Sorry, you are too slow!\"\nfi\n       \n<\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nThe&nbsp;-s&nbsp;option causes the user&#8217;s typing not to be displayed. This is useful when you are asking the user to type in a password or other confidential information.<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\nArithmetic<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nSince we are working on a computer, it is natural to expect that it can perform some simple arithmetic. The shell provides features for&nbsp;<i>integer arithmetic<\/i>.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nWhat&#8217;s an integer? That means whole numbers like 1, 2, 458, -2859. It does not mean fractional numbers like 0.5, .333, or 3.1415. If you must deal with fractional numbers, there is a separate program called&nbsp;<a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/bc1.html\" style=\"color: #002740;\">bc<\/a>&nbsp;which provides an arbitrary precision calculator language. It can be used in shell scripts, but is beyond the scope of this tutorial.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nLet&#8217;s say you want to use the command line as a primitive calculator. You can do it like this:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;echo $((2+2))<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nAs you can see, when you surround an arithmetic expression with the double parentheses, the shell will perform arithmetic expansion.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nNotice that whitespace is ignored:<\/div>\n<div class=\"display\" style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n[me@linuxbox me]$&nbsp;echo $((2+2))<br \/>4<br \/>[me@linuxbox me]$&nbsp;echo $(( 2+2 ))<br \/>4<br \/>[me@linuxbox me]$&nbsp;echo $(( 2 + 2 ))<br \/>4<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nThe shell can perform a variety of common (and not so common) arithmetic operations. Here is an example:<\/div>\n<div class=\"codeexample\" style=\"background-color: #e0e0e0; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\">#!\/bin\/bash\n\nfirst_num=0\nsecond_num=0\n\necho -n \"Enter the first number --&gt; \"\nread first_num\necho -n \"Enter the second number -&gt; \"\nread second_num\n\necho \"first number + second number = $((first_num + second_num))\"\necho \"first number - second number = $((first_num - second_num))\"\necho \"first number * second number = $((first_num * second_num))\"\necho \"first number \/ second number = $((first_num \/ second_num))\"\necho \"first number % second number = $((first_num % second_num))\"\necho \"first number raised to the\"\necho \"power of the second number   = $((first_num ** second_num))\"\n       \n<\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nNotice how the leading &#8220;$&#8221; is not needed to reference variables inside the arithmetic expression such as &#8220;first_num + second_num&#8221;.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nTry this program out and watch how it handles division (remember, this is integer division) and how it handles large numbers. Numbers that get too large&nbsp;<i>overflow<\/i>&nbsp;like the odometer in a car when you exceed the number of miles it was designed to count. It starts over but first it goes through all the negative numbers because of how integers are represented in memory. Division by zero (which is mathematically invalid) does cause an error.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nI&#8217;m sure that you recognize the first four operations as addition, subtraction, multiplication and division, but that the fifth one may be unfamiliar. The &#8220;%&#8221; symbol represents remainder (also known as&nbsp;<i>modulo<\/i>). This operation performs division but instead of returning a quotient like division, it returns the remainder. While this might not seem very useful, it does, in fact, provide great utility when writing programs. For example, when a remainder operation returns zero, it indicates that the first number is an exact multiple of the second. This can be very handy:<\/div>\n<div class=\"codeexample\" style=\"background-color: #e0e0e0; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\">#!\/bin\/bash\n\nnumber=0\n\necho -n \"Enter a number &gt; \"\nread number\n\necho \"Number is $number\"\nif [ $((number % 2)) -eq 0 ]; then\n    echo \"Number is even\"\nelse\n    echo \"Number is odd\"\nfi\n       \n<\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nOr, in this program that formats an arbitrary number of seconds into hours and minutes:<\/div>\n<div class=\"codeexample\" style=\"background-color: #e0e0e0; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\">#!\/bin\/bash\n\nseconds=0\n\necho -n \"Enter number of seconds &gt; \"\nread seconds\n\nhours=$((seconds \/ 3600))\nseconds=$((seconds % 3600))\nminutes=$((seconds \/ 60))\nseconds=$((seconds % 60))\n\necho \"$hours hour(s) $minutes minute(s) $seconds second(s)\"\n       \n<\/pre>\n<\/div>\n<div class=\"pagenav\" style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; padding: 1em;\">\n<\/div>\n<p><\/span><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The Linux Command Line&nbsp;by William Shotts Up to now &#8230; <a title=\"The Linux Command Line&#8212;Keyboard Input And Arithmetic\" class=\"read-more\" href=\"https:\/\/yeslq.com\/?p=186\" aria-label=\"\u9605\u8bfb The Linux Command Line&#8212;Keyboard Input And Arithmetic\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[51,52,53],"class_list":["post-186","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-linux","tag-shell","tag-the-linux-command-line"],"_links":{"self":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/186","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=186"}],"version-history":[{"count":1,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/186\/revisions"}],"predecessor-version":[{"id":318,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/186\/revisions\/318"}],"wp:attachment":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}