{"id":199,"date":"2020-08-18T19:23:47","date_gmt":"2020-08-18T20:23:47","guid":{"rendered":"http:\/\/www.linux-tutorial.info\/?page_id=77"},"modified":"2020-08-22T19:26:19","modified_gmt":"2020-08-22T20:26:19","slug":"this-is-the-page-title-toplevel-33","status":"publish","type":"page","link":"http:\/\/www.linux-tutorial.info\/?page_id=199","title":{"rendered":"Shell Odds and Ends"},"content":{"rendered":"\n<title>Odds and Ends<\/title>\n<p>\nThis section includes a few tidbits that I wasn&#8217;t sure where to put.\n<\/p>\n<concept id=\"\" description=\"Using 'set -x' in a script will show each line before it is executed.\" \/>\n<p>\nUnlike DOS Batch-Scripts, Linux Shell-Scripts do not diplay each line as it is executed. Instead,\nthe default is not to display anything unless you explicitely output it. However, if you place\n<keyinput>set -x<\/keyinput> in\nyour script, each command with its corresponding arguments is printed as it is\nexecuted. If you want to just show a section of your script, include the <keyinput>set -x<\/keyinput>\nbefore that section, then another <keyinput>set +x<\/keyinput> at the end.\nThe <keyinput>set +x<\/keyinput> turns off the output.\n<\/p>\n<p>\nIf you want, you can capture output into another file, without having it go\nto the screen. This is done using the fact that output generated as a result of\nthe <keyinput>set -x<\/keyinput> is going to <glossary>stderr<\/glossary>\nand not <glossary>stdout<\/glossary>. If you redirect stdout somewhere,\nthe output from <keyinput>set -x<\/keyinput> still goes to the screen. On the other hand, if you\nredirect <glossary>stderr<\/glossary>, <glossary>stdout<\/glossary>\nstill goes to your screen. To redirect sterr to a file\nstart, the script like this:\n<\/p>\n<p>\n<commandexample>myscript 2&gt;\/tmp\/output<\/commandexample>\n<\/p>\n<p>\nThis says to send file <glossary>descriptor<\/glossary>\n2 (stderr) to the file <file type=\"\">\/tmp\/output<\/file>.\n<\/p>\n<question id=\"\" type=\"mc\" text=\"What option to 'mkdir' is used to create multiple levels of directories?\" \/>\n<concept id=\"\" description=\"The '-p' option to 'mkdir' is used to create multiple levels of directories.\" \/>\n<p>\nTo create a directory that is several levels deep, you do not have to change\ndirectories to the parent and then run <command>mkdir<\/command> from there.\nThe <command>mkdir<\/command> command takes\nas an <glossary>argument<\/glossary>\nthe path name of the directory you want to create. It doesn&#8217;t\nmatter if it is a <glossary>subdirectory<\/glossary>,\n<glossary>relative path<\/glossary>,\nor <glossary>absolute path<\/glossary>. The system will\ndo that for you. Also, if you want to create several levels of directories, you\ndon&#8217;t have to make each <glossary>parent directory<\/glossary>\nbefore you make the subdirectories.\nInstead, you can use the -p option to mkdir, which will automatically create all\nthe necessary directories.\n<\/p>\n<p>\nFor example, imagine that we want to create the subdirectory\n<file type=\"\">.\/letters\/personal\/john<\/file>, but the <glossary>subdirectory<\/glossary>\nletters does not exist yet. This also means that the <glossary>subdirectory<\/glossary>\npersonal doesn&#8217;t exist, either. If we run <command>mkdir<\/command> like this:\n<\/p>\n<concept id=\"60\" description=\"The -p option to mkdir can be used to create multiple levels of directories.\" \/>\n<p>\n<commandexample command=\"mkdir\">mkdir -p .\/letters\/personal\/john<\/commandexample>\n<\/p>\n<p>\nthen the system will create <directory>.\/letters<\/directory>, then <directory>.\/letters\/personal<\/directory>, and then\n<directory>.\/letters\/personal\/john<\/directory>.\n<\/p>\n<p>\nAssume that you want to remove a file that has multiple links; for example,\nassume that ls, lc, lx, lf, etc., are links to the same file. The system keeps\ntrack of how many names reference the file through the <glossary>link count<\/glossary>\n(more on this\nconcept later). Such links are called hard links. If you remove one of them, the\nfile still exists as there are other names that reference it. Only when we\nremove the last link (and with that, the <glossary>link count<\/glossary>\ngoes to zero) will the file\nbe removed.\n<\/p>\n<question id=\"\" type=\"mc\" text=\"What is another term for 'symbolic link'?\" \/>\n<question id=\"\" type=\"tf\" text=\"Another term for 'symbolic link' is 'soft link'.\" \/>\n<concept id=\"\" description=\"Another term for 'symbolic link' is 'soft link'.\" \/>\n<p>\nThere is also the issue of symbolic links. A <glossary>symbolic link<\/glossary>\n(also called a soft link) is nothing more than a path name that points to some other file, or\neven to some directory. It is not until the link is accessed that the path is\ntranslated into the &#8220;real&#8221; file. This has some interesting effects. For example,\nif we create a link like this:\n<\/p>\n<p>\n<commandexample command=\"ln\">\nln -s \/home\/jimmo\/letter.john \/home\/jimmo\/text\/letter.john\n<\/commandexample>\n<\/p>\n<p>\nyou would see the <glossary>symbolic link<\/glossary>\nas something like this:<\/p>\n<p>\n<screenoutput>drw-r&#8211;r&#8211; 1 jimmo support 29 Sep 15 10:06 letter.john-&gt; \/home\/jimmo\/letter.john<\/screenoutput>\n<\/p>\n<p>\nThen,the file <file type=\"\">\/home\/jimmo\/text\/letter.john<\/file> is a <glossary>symbolic link<\/glossary>\nto <file type=\"\">\/home\/jimmo\/letter.john<\/file>. Note that the <glossary>link count<\/glossary>\non <file type=\"\">\/home\/jimmo\/letter.john<\/file> doesn&#8217;t change, because the system sees these as two separate files. It is easier to think of the file <file type=\"\">\/home\/jimmo\/text\/letter.john<\/file> as a text\nfile that contains\nthe <glossary>path<\/glossary> to <file type=\"\">\/home\/jimmo\/letter.john<\/file>. If we remove <file type=\"\">\/home\/jimmo\/letter.john<\/file>,\n<file type=\"\">\/home\/jimmo\/text\/letter.john<\/file> will still exist. However, it will point to\nsomething that doesn&#8217;t exist. Even if there are other hard links that point to\nthe same file like <file type=\"\">\/home\/jimmo\/letter.john<\/file>, that doesn&#8217;t matter. The symbolic\nlink, <file type=\"\">\/home\/jimmo\/text\/letter.john<\/file>, points to the <em>path<\/em>\n<file type=\"\">\/home\/jimmo\/letter.john<\/file>. Because the path no longer exists, the file can no\nlonger be accessed via the <glossary>symbolic link<\/glossary>.\nIt is also possible for you to create a <glossary>symbolic link<\/glossary>\nto a file that does <i>not<\/i> exist, as the system does not check\nuntil you access the file.\n<\/p>\n<question id=\"85\" text=\"Symbolic links can refer to files or directories on different filesystems.\" \/>\n<concept id=\"62\" description=\"Symbolic links can refer to files or directories on different filesystems.\" \/>\n<p>\nAnother important aspect is that <glossary term=\"symbolic link\">symbolic links<\/glossary> can\nextend across file systems. A regular or <glossary>hard link<\/glossary>\nis nothing more than a different name for the same\nphysical file and used the same <glossary>inode<\/glossary>\nnumber. Therefore it must be on the same\nfilesystem. Symbolic links contain a path, so the destination can be on another\nfilesystem (and in some cases on another machine).\nFor more on inodes,\n<tutorial id=95> see the section on filesystems<\/tutorial>.\n<p>\nThe file command can be\nused to tell you the type of file. With <glossary>DOS<\/glossary>\nand Windows, it&#8217;s fairly obvious by\nlooking at the file&#8217;s extension to determine the files type. For example, files\nending in .exe are executables (programs), files ending in .txt are text\nfiles, and files ending in .doc are documents (usually from some word processor).\nHowever, a program in <glossary>UNIX<\/glossary>\ncan just as easily have the ending .doc or .exe, or\nno ending at all.\n<\/p>\n<question id=\"99\" text=\"What command is used to determine a file's type or contents? (i.e. binary, shell-script, ASCII)\" \/>\n<question id=\"100\" text=\"What file contains information used to determine a file's type or contents? (i.e. binary, shell-script, ASCII)\" \/>\n<concept id=\"111\" description=\"The 'file' command is used to determine a file's type or contents (i.e. binary, shell-script, ASCII).\" \/>\n<concept id=\"112\" description=\"The \/etc\/magic file contains information used to determine a file's type or contents. (i.e. binary, shell-script, ASCII)\" \/>\n<p>\nThe <command>file<\/command> command uses the file <file type=\"\">\/etc\/magic<\/file> to make an\nassumption about the contents of a file. The file command reads the header\n(first part of the file) and uses the information in \/etc\/magic to make its\nguess. Executables of a specific type (a.out, ELF) all have the same basic\nformat, so file can easily recognize them. However, there are certain\nsimilarities between C source code, shell scripts, and even <glossary>text<\/glossary>\nfiles that could confuse file.\n<\/p>\n<p>\nFor a list of some of the more commonly used commands, take a look\n<tutorial id=30>here<\/tutorial>.\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Odds and Ends This section includes a few tidbits that I wasn&#8217;t sure where to put. Unlike DOS Batch-Scripts, Linux Shell-Scripts do not diplay each line as it is executed. Instead, the default is not to display anything unless you &hellip; <a href=\"http:\/\/www.linux-tutorial.info\/?page_id=199\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-199","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/199","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=199"}],"version-history":[{"count":1,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/199\/revisions"}],"predecessor-version":[{"id":700,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/199\/revisions\/700"}],"wp:attachment":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}