{"id":193,"date":"2018-10-14T01:00:00","date_gmt":"2018-10-13T17:00:00","guid":{"rendered":"https:\/\/yeslq.com\/201810193.html"},"modified":"2019-05-07T10:43:24","modified_gmt":"2019-05-07T02:43:24","slug":"the-linux-command-line-here-scripts-2","status":"publish","type":"post","link":"https:\/\/yeslq.com\/?p=193","title":{"rendered":"The Linux Command Line&#8212;Here Scripts"},"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;\"><\/span><\/p>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\n<span style=\"font-size: xx-small;\">Beginning with this lesson, we will construct a useful application. This application will produce an HTML document that contains information about your system. I spent a lot of time thinking about how to teach shell programming, and the approach I have chosen is very different from most others that I have seen. Most favor a systematic treatment of shell features, and often presume experience with other programming languages. Although I do not assume that you already know how to program, I realize that many people today know how to write HTML, so our program will produce a web page. As we construct our script, we will discover step by step the tools needed to solve the problem at hand.<\/span><\/div>\n<p><span style=\"font-size: xx-small;\"><\/p>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">\nWriting An HTML File With A Script<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nAs you may know, a well formed HTML file contains the following content:<\/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;\">&lt;html&gt;\n&lt;head&gt;\n    &lt;title&gt;\n    The title of your page\n    &lt;\/title&gt;\n&lt;\/head&gt;\n\n&lt;body&gt;\n    Your page content goes here.\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nNow, with what we already know, we could write a script to produce the above content:<\/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\n# sysinfo_page - A script to produce an html file\n\necho \"&lt;html&gt;\"\necho \"&lt;head&gt;\"\necho \"  &lt;title&gt;\"\necho \"  The title of your page\"\necho \"  &lt;\/title&gt;\"\necho \"&lt;\/head&gt;\"\necho \"\"\necho \"&lt;body&gt;\"\necho \"  Your page content goes here.\"\necho \"&lt;\/body&gt;\"\necho \"&lt;\/html&gt;\"\n       \n<\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nThis script can be used as follows:<\/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;sysinfo_page &gt; sysinfo_page.html<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nIt has been said that the greatest programmers are also the laziest. They write programs to save themselves work. Likewise, when clever programmers write programs, they try to save themselves typing.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nThe first improvement to this script will be to replace the repeated use of the&nbsp;echo&nbsp;command with a single instance by using quotation more efficiently:<\/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\n# sysinfo_page - A script to produce an HTML file\n\necho \"&lt;html&gt;\n &lt;head&gt;\n   &lt;title&gt;\n   The title of your page\n   &lt;\/title&gt;\n &lt;\/head&gt;\n \n &lt;body&gt;\n   Your page content goes here.\n &lt;\/body&gt;\n &lt;\/html&gt;\"\n\n<\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nUsing quotation, it is possible to embed carriage returns in our text and have the&nbsp;echo&nbsp;command&#8217;s argument span multiple lines.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nWhile this is certainly an improvement, it does have a limitation. Since many types of markup used in html incorporate quotation marks themselves, it makes using a quoted string a little awkward. A quoted string can be used but each embedded quotation mark will need to be escaped with a backslash character.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nIn order to avoid the additional typing, we need to look for a better way to produce our text. Fortunately, the shell provides one. It&#8217;s called a&nbsp;<i>here script<\/i>.<\/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\n# sysinfo_page - A script to produce an HTML file\n\ncat &lt;&lt; _EOF_\n&lt;html&gt;\n&lt;head&gt;\n    &lt;title&gt;\n    The title of your page\n    &lt;\/title&gt;\n&lt;\/head&gt;\n\n&lt;body&gt;\n    Your page content goes here.\n&lt;\/body&gt;\n&lt;\/html&gt;\n_EOF_\n       \n<\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nA here script (also sometimes called a here document) is an additional form of&nbsp;<a href=\"http:\/\/linuxcommand.org\/lc3_lts0070.php\" style=\"color: #002740;\">I\/O redirection<\/a>. It provides a way to include content that will be given to the standard input of a command. In the case of the script above, the standard input of the&nbsp;cat&nbsp;command was given a stream of text from our script.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nA here script is constructed like this:<\/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;\">command &lt;&lt; token\ncontent to be used as command's standard input\ntoken\n       \n<\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\n<i>token<\/i>&nbsp;can be any string of characters. I use &#8220;_EOF_&#8221; (EOF is short for &#8220;End Of File&#8221;) because it is traditional, but you can use anything, as long as it does not conflict with a bash reserved word. The token that ends the here script must exactly match the one that starts it, or else the remainder of your script will be interpreted as more standard input to the command.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nThere is one additional trick that can be used with a here script. Often you will want to indent the content portion of the here script to improve the readability of your script. You can do this if you change the script as follows:<\/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\n# sysinfo_page - A script to produce an HTML file\n\ncat &lt;&lt;- _EOF_\n    &lt;html&gt;\n    &lt;head&gt;\n        &lt;title&gt;\n        The title of your page\n        &lt;\/title&gt;\n    &lt;\/head&gt;\n\n    &lt;body&gt;\n        Your page content goes here.\n    &lt;\/body&gt;\n    &lt;\/html&gt;\n_EOF_\n       \n<\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nChanging the the &#8220;&lt;&lt;&#8221; to &#8220;&lt;&lt;-&#8221; causes bash to ignore the leading tabs (but not spaces) in the here script. The output from the cat command will not contain any of the leading tab characters.<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nO.k., let&#8217;s make our page. We will edit our page to get it to say something:<\/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\n# sysinfo_page - A script to produce an HTML file\n\ncat &lt;&lt;- _EOF_\n    &lt;html&gt;\n    &lt;head&gt;\n        &lt;title&gt;\n        My System Information\n        &lt;\/title&gt;\n    &lt;\/head&gt;\n\n    &lt;body&gt;\n    &lt;h1&gt;My System Information&lt;\/h1&gt;\n    &lt;\/body&gt;\n    &lt;\/html&gt;\n_EOF_\n       \n<\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">\nIn our next lesson, we will make our script produce real information about the system.<\/div>\n<p><\/span><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The Linux Command Line&nbsp;by William Shotts Beginning &#8230; <a title=\"The Linux Command Line&#8212;Here Scripts\" class=\"read-more\" href=\"https:\/\/yeslq.com\/?p=193\" aria-label=\"\u9605\u8bfb The Linux Command Line&#8212;Here Scripts\">\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-193","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\/193","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=193"}],"version-history":[{"count":1,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/193\/revisions"}],"predecessor-version":[{"id":311,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/193\/revisions\/311"}],"wp:attachment":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}