{"id":41,"date":"2018-10-18T08:00:00","date_gmt":"2018-10-18T00:00:00","guid":{"rendered":""},"modified":"2018-11-11T16:10:59","modified_gmt":"2018-11-11T08:10:59","slug":"the-linux-command-line-some-real-work","status":"publish","type":"post","link":"https:\/\/yeslq.com\/?p=41","title":{"rendered":"The Linux Command Line&#8212;Some Real Work"},"content":{"rendered":"<h1 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\"><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><span style=\"font-size: xx-small;\"><br \/><\/span><\/div>\n<div><span style=\"font-size: xx-small;\"><\/p>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">In this lesson, we will develop some of our shell functions and get our script to produce some useful information.<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">show_uptime<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">The show_uptime function will display the output of the&nbsp;<a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/uptime1.html\" style=\"color: #002740;\"><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">uptime<\/tt><\/a>&nbsp;command. The uptime command outputs several interesting facts about the system, including the length of time the system has been &#8220;up&#8221; (running) since its last re-boot, the number of users and recent system load.<\/div>\n<div style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">[me@linuxbox me]$<\/tt>&nbsp;<tt style=\"background: transparent; color: white; font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">uptime<\/tt><br \/><tt style=\"font-family: courier, lucidatypewriter, monospace;\">9:15pm up 2 days, 2:32, 2 users, load average: 0.00, 0.00, 0.00<\/tt><\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">To get the output of the uptime command into our HTML page, we will code our shell function like this, replacing our temporary stubbing code with the finished version:<\/div>\n<div style=\"background-color: #e0e0e0; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">show_uptime()<br \/>{<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"&lt;h2&gt;System uptime&lt;\/h2&gt;\"<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"&lt;pre&gt;\"<br \/>    uptime<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"&lt;\/pre&gt;\"<br \/>}<br \/>      <\/tt><br \/><\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">As you can see, this function outputs a stream of text containing a mixture of HTML tags and command output. When the command substitution takes place in the main body of the our program, the output from our function becomes part of the here script.<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">drive_space<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">The drive_space function will use the&nbsp;<a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/df1.html\" style=\"color: #002740;\"><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">df<\/tt><\/a>&nbsp;command to provide a summary of the space used by all of the mounted file systems.<\/div>\n<div style=\"background-color: black; color: lime; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">[me@linuxbox me]$<\/tt>&nbsp;<tt style=\"background: transparent; color: white; font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">df<\/tt><\/p>\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">Filesystem   1k-blocks      Used Available Use% Mounted on<br \/><br \/>\/dev\/hda2       509992    225772    279080  45% \/<br \/>\/dev\/hda1        23324      1796     21288   8% \/boot<br \/>\/dev\/hda3     15739176   1748176  13832360  12% \/home<br \/>\/dev\/hda5      3123888   3039584     52820  99% \/usr<\/tt><br \/><\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">In terms of structure, the drive_space function is very similar to the show_uptime function:<\/div>\n<div style=\"background-color: #e0e0e0; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">drive_space()<br \/>{<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"&lt;h2&gt;Filesystem space&lt;\/h2&gt;\"<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"&lt;pre&gt;\"<br \/>    df<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"&lt;\/pre&gt;\"<br \/>}<br \/>      <\/tt><br \/><\/pre>\n<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">home_space<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">The home_space function will display the amount of space each user is using in his\/her home directory. It will display this as a list, sorted in descending order by the amount of space used.<\/div>\n<div style=\"background-color: #e0e0e0; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">home_space()<br \/>{<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"&lt;h2&gt;Home directory space by user&lt;\/h2&gt;\"<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"&lt;pre&gt;\"<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"Bytes Directory\"<br \/>    du -s \/home\/* | sort -nr<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"&lt;\/pre&gt;\"<br \/>}<br \/>      <\/tt><br \/><\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">Note that in order for this function to successfully execute, the script must be run by the superuser, since the&nbsp;<a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/du1.html\" style=\"color: #002740;\"><tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">du<\/tt><\/a>&nbsp;command requires superuser privileges to examine the contents of the \/home directory.<\/div>\n<h2 style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; font-weight: normal;\">system_info<\/h2>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif;\">We&#8217;re not ready to finish the system_info function yet. In the meantime, we will improve the stubbing code so it produces valid HTML:<\/div>\n<div style=\"background-color: #e0e0e0; font-family: verdana, arial, helvetica, sans-serif; padding: 0.5em;\">\n<pre style=\"font-family: courier, lucidatypewriter, monospace;\"><tt style=\"font-family: courier, lucidatypewriter, monospace;\">system_info()<br \/>{<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"&lt;h2&gt;System release info&lt;\/h2&gt;\"<br \/>    <tt style=\"font-family: courier, lucidatypewriter, monospace; font-weight: bold;\">echo<\/tt> \"&lt;p&gt;Function not yet implemented&lt;\/p&gt;\"<br \/>}<br \/>      <\/tt><br \/><\/pre>\n<\/div>\n<div style=\"background-color: white; font-family: verdana, arial, helvetica, sans-serif; padding: 1em;\"><\/div>\n<p><\/span><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The Linux Command Line&nbsp;by William Shotts In this l &#8230; <a title=\"The Linux Command Line&#8212;Some Real Work\" class=\"read-more\" href=\"https:\/\/yeslq.com\/?p=41\" aria-label=\"\u9605\u8bfb The Linux Command Line&#8212;Some Real Work\">\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":[3,4,6],"tags":[],"class_list":["post-41","post","type-post","status-publish","format-standard","hentry","category-linux","category-shell","category-the-linux-command-line"],"_links":{"self":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/41","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=41"}],"version-history":[{"count":1,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/41\/revisions"}],"predecessor-version":[{"id":102,"href":"https:\/\/yeslq.com\/index.php?rest_route=\/wp\/v2\/posts\/41\/revisions\/102"}],"wp:attachment":[{"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=41"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=41"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yeslq.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}