{"id":37,"date":"2022-03-04T15:11:16","date_gmt":"2022-03-04T20:11:16","guid":{"rendered":"https:\/\/pressbooks.library.ryerson.ca\/opsyshiraki\/chapter\/controlstruct1\/"},"modified":"2025-05-14T13:27:39","modified_gmt":"2025-05-14T17:27:39","slug":"controlstruct1","status":"publish","type":"chapter","link":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/chapter\/controlstruct1\/","title":{"raw":"Control Structures - Part 1 - branching","rendered":"Control Structures &#8211; Part 1 &#8211; branching"},"content":{"raw":"<p class=\"c15\"><span class=\"c0\">In order to support control structures (branching, looping, etc.), all computer languages need some method of evaluating a condition. \u00a0In Unix, the command to evaluate a condition is the test command. \u00a0The test command is often at the heart of most control structures in Unix.<\/span><\/p>\r\n&nbsp;\r\n<h1 class=\"c15\"><span class=\"c0\">test<\/span><\/h1>\r\n<p class=\"c15\"><span class=\"c0\">What does the test command do? \u00a0It evaluates a condition and sets the special shell variable $?, the return code. \u00a0Much to the confusion of new users, the test command is silent in that it does not print anything to standard output. \u00a0Thus when running the test command, it appears as if to do nothing. \u00a0To check the return code, one may simply print its value with an echo statement.<\/span><\/p>\r\n\r\n<div class=\"textbox textbox--examples\"><header class=\"textbox__header\">\r\n<p class=\"textbox__title\"><span class=\"c0\">Example 1: \u00a0Check if a file is readable.<\/span><\/p>\r\n\r\n<\/header>\r\n<div class=\"textbox__content\">\r\n<pre id=\"h.st1gw9c00v55\" class=\"c1 c11\"><span class=\"c2\">$ <strong>test -r myfile<\/strong><\/span>\r\n<span class=\"c2\">$ <strong>echo $?<\/strong><\/span>\r\n<span class=\"c2\">0<\/span><\/pre>\r\n<\/div>\r\n<\/div>\r\n<p class=\"c3\"><span class=\"c0\">\u00a0<\/span><\/p>\r\n<p class=\"c15\"><span class=\"c0\">How does one interpret the return code? \u00a0 <\/span><\/p>\r\n<p class=\"c15\"><span class=\"c0\">Unix convention:<\/span><\/p>\r\n\r\n<table class=\"grid\" style=\"height: 58px\">\r\n<tbody>\r\n<tr class=\"c25\" style=\"height: 29px\">\r\n<th class=\"c27\" style=\"height: 29px;width: 126.146px\" colspan=\"1\" rowspan=\"1\" scope=\"row\">\r\n<p class=\"c1\"><span class=\"c0\">0 (zero)<\/span><\/p>\r\n<\/th>\r\n<td class=\"c39\" style=\"height: 29px;width: 109.271px\" colspan=\"1\" rowspan=\"1\">\r\n<p class=\"c1\"><span class=\"c0\">means TRUE<\/span><\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"c25\" style=\"height: 29px\">\r\n<th class=\"c27\" style=\"height: 29px;width: 126.146px\" colspan=\"1\" rowspan=\"1\" scope=\"row\">\r\n<p class=\"c1\"><span class=\"c0\">not 0 (not zero)<\/span><\/p>\r\n<\/th>\r\n<td class=\"c39\" style=\"height: 29px;width: 109.271px\" colspan=\"1\" rowspan=\"1\">\r\n<p class=\"c1\"><span class=\"c0\">means FALSE<\/span><\/p>\r\n<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<p class=\"c3\"><span class=\"c0\">\u00a0<\/span><\/p>\r\n\r\n<div class=\"textbox textbox--examples\"><header class=\"textbox__header\">\r\n<p class=\"textbox__title\"><span class=\"c0\">Example 2: \u00a0Compare if two strings are equal.<\/span><\/p>\r\n\r\n<\/header>\r\n<div class=\"textbox__content\">\r\n<pre id=\"h.g5mey17jgskn\" class=\"c1 c11 \"><span class=\"c2\">$ <strong>test \"this\" = \"that\"<\/strong><\/span>\r\n<span class=\"c2\">$ <strong>echo $?<\/strong><\/span>\r\n<span class=\"c2\">1<\/span><\/pre>\r\n<\/div>\r\n<\/div>\r\n<p class=\"c3\"><span class=\"c0\">\u00a0<\/span><\/p>\r\n\r\n<h2 class=\"c15\"><span class=\"c0\">Synonym to the test command: \u00a0[<\/span><\/h2>\r\n<p class=\"c15\"><span class=\"c0\">For reasons of readability, many programmers with use the synonym or abbreviation for the test command which is the left square bracket: [ . \u00a0In all examples above, replace the word test with the left square bracket. \u00a0Note that a matching right square bracket needs to be added for syntactic reasons. \u00a0As with all Unix commands, spaces are a big deal and a space is required after the left square bracket and before the right square bracket.<\/span><\/p>\r\n<p class=\"c3\"><span class=\"c0\">\u00a0<\/span><\/p>\r\n\r\n<div class=\"textbox textbox--examples\"><header class=\"textbox__header\">\r\n<p class=\"textbox__title\"><span class=\"c0\">Example 3: Synonym to the test command [<\/span><\/p>\r\n\r\n<\/header>\r\n<div class=\"textbox__content\">\r\n<pre id=\"h.3arageq61qd\" class=\"c1 c11\">$ <strong>[ -r myfile ]<\/strong> # <span class=\"c19 c23\">note the space after the left bracket<\/span>\r\n$ <strong><span class=\"c2\">echo $?<\/span>\r\n<\/strong><span class=\"c2\">0<\/span><span class=\"c0\">\u00a0<\/span><\/pre>\r\n<\/div>\r\n<\/div>\r\n&nbsp;\r\n<h1 id=\"h.qrw2dg5f4ok8\" class=\"c33 c11\"><span class=\"c30 c26\">Control Structures<\/span><\/h1>\r\n<h2 id=\"h.cmha19g1ccek\" class=\"c28 c11\"><span class=\"c29 c26\">if<\/span><\/h2>\r\n<p class=\"c15\"><span class=\"c0\">The if statement in Unix is the basic two-way branch.<\/span><\/p>\r\n&nbsp;\r\n<div class=\"textbox\">\r\n<p class=\"c15\"><span class=\"c0\">Syntax<\/span><\/p>\r\n\r\n<pre id=\"h.w0txwnysszi9\" class=\"c1 c11\"><span class=\"c2\">if unix_statements<\/span>\r\n<span class=\"c2\">then<\/span>\r\n<span class=\"c2\">  \u00a0actions_true<\/span>\r\n<span class=\"c2\">else \u00a0# optional<\/span>\r\n<span class=\"c2\">  \u00a0actions_false<\/span>\r\n<span class=\"c2\">fi<\/span><\/pre>\r\n<\/div>\r\n<p class=\"c15\"><span class=\"c0\">How does the if statement work? \u00a0Here is the sequence of operations. \u00a0The if statement will:<\/span><\/p>\r\n\r\n<ol class=\"c35 lst-kix_7gl0q3kbqu7q-0 start\" start=\"1\">\r\n \t<li class=\"c15 c37 li-bullet-0\"><span class=\"c0\">Run all the unix_statements. \u00a0 <\/span><\/li>\r\n \t<li class=\"c15 c37 li-bullet-0\"><span class=\"c0\">Check the return code ($?) of the last statement in the list (just prior to the keyword \"then\". <\/span><\/li>\r\n \t<li class=\"c15 c37 li-bullet-0\">If the return code is true. the \"then\" clause is executed (\r\n<span class=\"c4\">actions_true)<\/span>. \u00a0If the return code is false, the \"else\" clause is executed (<span class=\"c4\">actions_false)<\/span><span class=\"c0\">. \u00a0The else clause is optional (can be left out if not needed).<\/span><\/li>\r\n<\/ol>\r\n<div class=\"textbox textbox--examples\"><header class=\"textbox__header\">\r\n<p class=\"textbox__title\"><span class=\"c0\">Example of if<\/span><\/p>\r\n\r\n<\/header>\r\n<div class=\"textbox__content\">\r\n<pre id=\"h.kzwhp4rz3e59\" class=\"c1 c11\"><span class=\"c2\">#!\/bin\/bash<\/span>\r\n<span class=\"c2\">comfort=20<\/span>\r\n<span class=\"c2\">temperature=18<\/span>\r\n<span class=\"c2\">if echo The current temperature is $temperature<\/span>\r\n<span class=\"c2\">  \u00a0echo Temperature for comparison is $comfort<\/span>\r\n<span class=\"c2\">  \u00a0[ $temperature -lt $comfort ]<\/span>\r\n<span class=\"c2\">then<\/span>\r\n<span class=\"c2\">  echo It is cold.<\/span>\r\n<span class=\"c2\">else<\/span>\r\n<span class=\"c2\">  echo It is warm.<\/span>\r\n<span class=\"c2\">fi<\/span><span class=\"c0\">\u00a0<\/span><\/pre>\r\n<\/div>\r\n<\/div>\r\n[h5p id=\"5\"]\r\n<p class=\"c15\"><span class=\"c0\">Nested conditions are supported in Unix. \u00a0Any statement in the \"then\" or \"else\" clause can itself be another if statement.<\/span><\/p>\r\n\r\n<h3>elif<\/h3>\r\n<p class=\"c15\"><span class=\"c0\">Although the if statement is primarily a two-way branch (e.g. true or false), a multi-way branch (e.g. red, yellow, green) can be coded using a set of nested if statements. \u00a0Some computer languages support an \"else-if\"-type clause; Unix is one of them.<\/span><\/p>\r\n<p class=\"c15\"><span class=\"c0\">There is an \"else-if\" clause called \"elif\" which requires a statement just like the if clause. <\/span><\/p>\r\n\r\n<div class=\"textbox textbox--examples\"><header class=\"textbox__header\">\r\n<p class=\"textbox__title\">Example: elif<\/p>\r\n\r\n<\/header>\r\n<div class=\"textbox__content\">\r\n<pre id=\"h.kzwhp4rz3e59-15\" class=\"c1 c11\"><span class=\"c2\">if [ $temperature -lt $comfort ]<\/span>\r\n<span class=\"c2\">then<\/span>\r\n<span class=\"c2\">  echo It is cold.<\/span>\r\n<span class=\"c2\">elif [ $temperature -eq $comfort ]<\/span>\r\n<span class=\"c2\">then<\/span>\r\n<span class=\"c2\">  echo It is perfect.<\/span>\r\n<span class=\"c2\">else<\/span>\r\n<span class=\"c2\">  echo It is warm.<\/span>\r\n<span class=\"c2\">fi<\/span>\r\n<span class=\"c0\"> <\/span><\/pre>\r\n<p class=\"c15\">Note that the \"elif\" clause is actually a clause of the main \"if\" and <span class=\"c43\">not <\/span><span class=\"c0\">a nested if statement. \u00a0Thus, there is only one \"fi\" (end if) required for each opening \"if\" regardless of how many \"elif\" clauses there are.<\/span><\/p>\r\n\r\n<\/div>\r\n<\/div>\r\n<span>[h5p id=\"6\"]<\/span>\r\n\r\n<span>[h5p id=\"7\"]<\/span>\r\n<h2 id=\"h.6bd3ulkf9b8a\" class=\"c28 c11\">case<\/h2>\r\n<p class=\"c15\">Many computer languages support a multi-way branch. Unix is included as one of them.<\/p>\r\n\r\n<div class=\"textbox\">\r\n<p class=\"c15\"><span class=\"c26\">Simplified syntax:<\/span><\/p>\r\n<p class=\"c40 c14\"><span class=\"c19 c18\">\u00a0<\/span><\/p>\r\n\r\n<pre id=\"h.r7oy87p532rc\" class=\"c5 c1\"><span class=\"c2\">case $variable <\/span>\r\n<span class=\"c2\">in<\/span>\r\n<span class=\"c2\">  val1) action1;;<\/span>\r\n<span class=\"c2\">  val2) action2;;<\/span>\r\n<span class=\"c2\">  *) default action;;<\/span>\r\n<span class=\"c2\">esac<\/span><span class=\"c19 c18\">\u00a0<\/span><\/pre>\r\n<p class=\"c15\"><span class=\"c0\">The keywords are case, in, and esac. \u00a0The double semi-colon is a syntactic requirement to separate the inner clauses of the case statement.<\/span><\/p>\r\n\r\n<\/div>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n<div class=\"textbox textbox--examples\"><header class=\"textbox__header\">\r\n<p class=\"textbox__title\">Example: \u00a0F<span class=\"c26\">lexible command line processing. <\/span><\/p>\r\n\r\n<\/header>\r\n<div class=\"textbox__content\">\r\n<p class=\"c15\"><span class=\"c26\">Allow your user to <\/span><span class=\"c0\">run your script in various ways.\u00a0 Accommodate all of the following invocations.<\/span><\/p>\r\n\r\n<pre class=\"c3\"><span class=\"c2\">$ .\/<strong>mysc<\/strong><\/span><span class=\"c2\"><strong>ript<\/strong> <\/span><span class=\"c19 c18\"># user omits filename; give second chance<\/span>\r\n$ .\/<strong><span class=\"c2\">myscript chapter3<\/span><\/strong><span class=\"c19 c18\"> # <\/span><span class=\"c18\">preferred syntax; just proceed<\/span>\r\n$ .\/<strong><span class=\"c2\">myscript chapter3 chapter5 <\/span><\/strong><span class=\"c19 c18\"># multiple arguments not s<\/span><span class=\"c18\">upported; \r\n<\/span><span class=\"c19 c18\">inform user<\/span>\r\n\r\n<span class=\"c2\">Place this code snippet at the beginning of your script like this:<\/span>\r\n<span class=\"c2\">$ <strong>cat myscript<\/strong>\r\ncase $# in<\/span>\r\n<span class=\"c2\">  0) echo Enter file name:<\/span>\r\n<span class=\"c2\">  \u00a0 \u00a0 \u00a0 \u00a0 \u00a0read arg1;;<\/span>\r\n<span class=\"c2\">  1) arg1=$1;;<\/span>\r\n<span class=\"c2\">  *) echo invalid number of arguments<\/span>\r\n<span class=\"c2\">  \u00a0 \u00a0echo \"Syntax: $0 filename\"<\/span>\r\n<span class=\"c2\">  \u00a0 \u00a0exit 1;;<\/span>\r\n<span class=\"c2\">esac<\/span>\r\n<span class=\"c2\"># rest of program continues after esac\r\n$<\/span><\/pre>\r\n<\/div>\r\n<\/div>\r\n&nbsp;\r\n<div class=\"textbox\">\r\n<p class=\"c15\"><span style=\"text-align: initial;font-size: 1em\">General syntax:<\/span><span class=\"c19 c18\">\u00a0<\/span><\/p>\r\n\r\n<pre><span class=\"c2\">case match_string_expr in<\/span>\r\n<span class=\"c2\">  match_pattern) action1;;<\/span>\r\n<span class=\"c2\">  match_pattern) action2;;<\/span>\r\n<span class=\"c2\">  ...<\/span><\/pre>\r\n<pre id=\"h.c44v635lr96i\" class=\"c5 c1\">esac<\/pre>\r\n<\/div>\r\n&nbsp;\r\n<div class=\"textbox textbox--examples\"><header class=\"textbox__header\">\r\n<p class=\"textbox__title\"><span class=\"c0\">Example: \u00a0Demonstrate pattern matching use in case statement. \u00a0Print a message about the length of the current month.<\/span><\/p>\r\n\r\n<\/header>\r\n<div class=\"textbox__content\">\r\n<pre>lhiraki@metis:~\/test$ <strong>cat case_month\r\n<\/strong>case $(date '+%m') in\r\n01|03|05|07|08|10|12)\r\n  \u00a0 \u00a0echo This is a long month;;\r\n04|06|09|11)\r\n  \u00a0 \u00a0echo This is a short month;;\r\n02)\r\n  \u00a0 \u00a0echo This is the shortest month;;\r\n*)\r\n  \u00a0 \u00a0echo Something wrong with date command;;\r\nesac\r\n\r\n# Example run in September\r\n\r\nlhiraki@metis:~\/test$ <strong>.\/case_month<\/strong>\r\nThis is a short month\r\nlhiraki@metis:~\/test$\r\n\r\n<\/pre>\r\n<p class=\"c3\"><span style=\"text-align: initial;font-size: 1em\">The date command is called with an option to return the numerical value of the month (e.g. Jan=01, Feb=02, etc.). \u00a0Depending on the month, a message is printed regarding the length of the month. \u00a0Months with the same number of days are grouped together using a pattern with the OR (vertical bar) syntax.<\/span><\/p>\r\n\r\n<\/div>\r\n<\/div>\r\n&nbsp;\r\n<h3 class=\"c47 c11\" id=\"h.x0r9zvaxt49t\"><span class=\"c48 c26 c43\">Defensive programming<\/span><\/h3>\r\n<p class=\"c15\"><span class=\"c0\">Well the previous example is rather trivial and the date command has been well tested over the years, it is considered good practice to always have a default clause even if you think you have covered all possible conditions.<\/span><\/p>\r\n\r\n<h2 class=\"c15\">Summary<\/h2>\r\n[caption id=\"\" align=\"alignnone\" width=\"519\"]<img alt=\"Entry arrow points to circle labled case; multiple arrows leave case circle to rectangles each with a separate action; each action has one arrow leaving which are all collected to a common exit.\" src=\"http:\/\/pressbooks.library.ryerson.ca\/opsyshiraki\/wp-content\/uploads\/sites\/330\/2022\/03\/image1-1.png\" style=\"width: 519.00px;height: 171.44px;margin-left: 0.00px;margin-top: 0.00px\" width=\"519\" height=\"170\" \/> Flow of case statement: one entry point, only one action chosen.[\/caption]\r\n\r\n&nbsp;\r\n<p class=\"c6\"><span class=\"c19 c23\">\u00a0<\/span>The match_string_expr is matched against each match_pattern in the order coded. \u00a0At the first match, the corresponding action is taken. \u00a0After one action is completed, the case statement terminates and execution continues after the esac (end case). \u00a0There is no \"fall-through\". \u00a0The case statement will not execute multiple actions.<\/p>\r\n<span>[h5p id=\"8\"]<\/span>\r\n<h2 id=\"h.df6xnvncbf1j\" class=\"c28 c11\">s<span class=\"c29 c26\">hift<\/span><\/h2>\r\n<p class=\"c15\"><span class=\"c0\">When processing multiple command-line arguments, it may be necessary to manipulate them to facilitate processing.<\/span><\/p>\r\n<p class=\"c15\">The shift command moves all command-line arguments one position to the left. \u00a0For example the second command line argument is moved into the first position; the third command line argument is moved into the second position, and so forth.<\/p>\r\n&nbsp;\r\n<div class=\"textbox textbox--examples\"><header class=\"textbox__header\">\r\n<p class=\"textbox__title\">Example of\u00a0 shift command<\/p>\r\n\r\n<\/header>\r\n<div class=\"textbox__content\">\r\n<pre id=\"h.7hj2ijv33hcw\" class=\"c9 c1\"><span class=\"c2\">lhiraki@thebe:~\/test$ <strong>cat shifter<\/strong><\/span>\r\n\r\n<span class=\"c2\">#!\/bin\/bash<\/span>\r\n<span class=\"c2\">echo The 1st arg is $1<\/span>\r\n<span class=\"c2\">echo The 2nd arg is $2<\/span>\r\n<span class=\"c2\">shift<\/span>\r\n<span class=\"c2\">echo The 2nd arg is $1<\/span>\r\n<span class=\"c2\">shift<\/span>\r\n<span class=\"c2\">echo The 3rd arg is $1<\/span><span class=\"c19 c18\">\u00a0<\/span><\/pre>\r\n<p class=\"c32 c36\"><span class=\"c19 c18\">Execution results in:<\/span><\/p>\r\n\r\n<pre id=\"h.u6zk61ioj0o6\" class=\"c9 c1\"><span class=\"c2\">lhiraki@thebe:~\/te<\/span><span class=\"c2\">st$ <strong>.\/shifter apple pear grape<\/strong><\/span>\r\n<span class=\"c2\">The 1st arg is apple<\/span>\r\n<span class=\"c2\">The 2nd arg is pear<\/span>\r\n<span class=\"c2\">The 2nd arg is pear<\/span>\r\n<span class=\"c2\">The 3rd arg is grape<\/span>\r\n<span class=\"c2\">lhiraki@thebe:~\/test$<\/span><\/pre>\r\n<p id=\"h.2myj48q95b02\" class=\"c9 c1 c14\"><span class=\"c2\">\u00a0<\/span><\/p>\r\n\r\n<\/div>\r\n<\/div>\r\n<h3 class=\"c11 c47\" id=\"h.n1jlngeudrck\">The exit command<\/h3>\r\n<p class=\"c15\">The exit command <span class=\"c26\">does <\/span>two<span class=\"c0\">\u00a0things:<\/span><\/p>\r\n\r\n<ol class=\"c35 lst-kix_lp44ssjvz62w-0 start\" start=\"1\">\r\n \t<li class=\"c15 c37 li-bullet-0\">It <span class=\"c0\">terminates the current shell (or script), returning control to the calling shell, if any.<\/span><\/li>\r\n \t<li class=\"c15 c37 li-bullet-0\">It <span class=\"c26\">sets the return code ($?) for your script<\/span>.<\/li>\r\n<\/ol>\r\n<div class=\"textbox textbox--examples\"><header class=\"textbox__header\">\r\n<p class=\"textbox__title\">Example - exit command usage<\/p>\r\n\r\n<\/header>\r\n<div class=\"textbox__content\">\r\n\r\n<span class=\"c0\">The trivial but illustrative script exit_example shows the exit command setting return code for the script to 3 and then terminating the script.\u00a0 Control returns to the calling program, in this case just back to the operating system prompt.<\/span>\r\n<pre>$ <strong>cat exit_example<\/strong>\r\n#!\/bin\/bash\r\nexit 3\r\necho This line never executed.\r\n\r\n$ <strong>.\/exit_example<\/strong>\r\n$ <strong>echo $?<\/strong>\r\n3\r\n$<\/pre>\r\n<span class=\"c0\">Note that one must inspect the return code ($?) immediately after running exit_example.\u00a0 The return code is updated (overwritten) by each Unix command executed. <\/span>\r\n\r\n<\/div>\r\n<\/div>\r\n<p class=\"c15\"><span class=\"c0\">The exit command is typically used to terminate a script midway through often due to an errror condition. The other primary use of the exit command is for a sub-script to communicate information back to the calling script.<\/span><\/p>\r\n&nbsp;\r\n<div class=\"textbox textbox--examples\"><header class=\"textbox__header\">\r\n<p class=\"textbox__title\">Example - parent\/child script relation<\/p>\r\n\r\n<\/header>\r\n<div class=\"textbox__content\">\r\n<table class=\"grid\" style=\"width: 284px\">\r\n<thead>\r\n<tr class=\"c25\">\r\n<th class=\"c41\" style=\"width: 199.302px\" scope=\"col\">\r\n<p class=\"c1\"><span class=\"c0\">parent<\/span><\/p>\r\n<\/th>\r\n<th class=\"c12\" style=\"width: 57.8646px\" scope=\"col\">\r\n<p class=\"c1\"><span class=\"c0\">child<\/span><\/p>\r\n<\/th>\r\n<\/tr>\r\n<\/thead>\r\n<tbody>\r\n<tr class=\"c25\">\r\n<td class=\"c41\" style=\"width: 199.302px\">\r\n<pre class=\"c1\"><span class=\"c2\">.\/child\u00a0\r\n<\/span><span class=\"c2\">ret_val=$?\r\n<\/span><span class=\"c2\">case $ret_val<\/span><\/pre>\r\n<\/td>\r\n<td class=\"c12\" style=\"width: 57.8646px\">\r\n<pre id=\"h.zgjskii3y53y\" class=\"c1 c49\"><span class=\"c2\">...<\/span>\r\n<span class=\"c2\">\u00a0 \u00a0exit 2\r\n<\/span><span class=\"c2\">...\r\n<\/span><span class=\"c2\">exit 0<\/span><\/pre>\r\n<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<p class=\"c40\"><span class=\"c0\">Note: parent-child relationship can be used for team project where multiple people can work on the same file at the same time.<\/span><\/p>\r\n\r\n<\/div>\r\n<\/div>\r\n<p class=\"c3\"><span class=\"c0\">\u00a0<\/span><\/p>\r\n\r\n<h2 id=\"h.9e9qi4h1u243\" class=\"c11 c28\"><span class=\"c26 c29\">Shebang line<\/span><\/h2>\r\n<p class=\"c15\"><span class=\"c0\">To specify which interpreter Unix should use when executing your script, as the first line of the file place the path to the interpreter after \"#!\". \u00a0While the number sign character (#) normally introduces a comment, when used with the exclamation mark at the beginning of a file, Unix will load the interpreter specified in the path to run the rest of the script file.<\/span><\/p>\r\n<p class=\"c15\"><span class=\"c0\">This is especially important to make your script portable in Unix environments. \u00a0If you write your script in bash, and you give your script for someone else to use who works in a c-shell or Korn shell environment, your script may not work properly. \u00a0To ensure that the script is run under bash, you must specify the shebang line.<\/span><\/p>\r\n<p class=\"c15\">Careful: \u00a0The shebang line must be the <span class=\"c43\">first line of the file<\/span><span class=\"c0\">, not just the first line of text, or the first line of code. \u00a0A common mistake is to have a blank line as the first line, or some comments above the shebang line. \u00a0Unix does not look beyond the first line of the file in order to identify the expected interpreter.<\/span><\/p>\r\n\r\n<table class=\"grid\">\r\n<thead>\r\n<tr class=\"c25\">\r\n<th class=\"c22\" style=\"width: 168px\" colspan=\"1\" rowspan=\"1\" scope=\"col\">\r\n<p class=\"c1\"><span class=\"c0\">Right<\/span><\/p>\r\n<\/th>\r\n<th class=\"c22\" style=\"width: 130px\" colspan=\"1\" rowspan=\"1\" scope=\"col\">\r\n<p class=\"c1\"><span class=\"c0\">Wrong<\/span><\/p>\r\n<\/th>\r\n<th class=\"c22\" style=\"width: 117px\" colspan=\"1\" rowspan=\"1\" scope=\"col\">\r\n<p class=\"c1\"><span class=\"c0\">Wrong<\/span><\/p>\r\n<\/th>\r\n<\/tr>\r\n<\/thead>\r\n<tbody>\r\n<tr class=\"c25\">\r\n<td class=\"c22\" style=\"width: 168px\" colspan=\"1\" rowspan=\"1\">\r\n<pre id=\"h.k9b970rv56o2\" class=\"c1 c11\"><span class=\"c42\">1:#!\/bin\/bash\r\n<\/span><span class=\"c2\">2:#comments\r\n<\/span><span class=\"c2\">3:#comments\r\n<\/span><span class=\"c2\">4:code begins here\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/span><\/pre>\r\n<\/td>\r\n<td class=\"c22\" style=\"width: 130px\" colspan=\"1\" rowspan=\"1\">\r\n<pre id=\"h.gz85vvtz5oro\" class=\"c1 c11\"><span class=\"c2\">1:#comments\r\n<\/span>2:#<span class=\"c4\">comments\r\n<\/span><span class=\"c42\">3:#!\/bin\/bash\r\n<\/span><span class=\"c2\">4:code begins here<\/span><\/pre>\r\n<\/td>\r\n<td class=\"c22\" style=\"width: 117px\" colspan=\"1\" rowspan=\"1\">\r\n<pre id=\"h.u6ke8weax8l\" class=\"c1 c11 c14\"><span class=\"c42\">1:\r\n2:#!\/bin\/bash\r\n3:\r\n4:<\/span><span class=\"c2\">code begins here<\/span><\/pre>\r\n<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<span>[h5p id=\"9\"]<\/span>\r\n\r\n&nbsp;","rendered":"<p class=\"c15\"><span class=\"c0\">In order to support control structures (branching, looping, etc.), all computer languages need some method of evaluating a condition. \u00a0In Unix, the command to evaluate a condition is the test command. \u00a0The test command is often at the heart of most control structures in Unix.<\/span><\/p>\n<p>&nbsp;<\/p>\n<h1 class=\"c15\"><span class=\"c0\">test<\/span><\/h1>\n<p class=\"c15\"><span class=\"c0\">What does the test command do? \u00a0It evaluates a condition and sets the special shell variable $?, the return code. \u00a0Much to the confusion of new users, the test command is silent in that it does not print anything to standard output. \u00a0Thus when running the test command, it appears as if to do nothing. \u00a0To check the return code, one may simply print its value with an echo statement.<\/span><\/p>\n<div class=\"textbox textbox--examples\">\n<header class=\"textbox__header\">\n<p class=\"textbox__title\"><span class=\"c0\">Example 1: \u00a0Check if a file is readable.<\/span><\/p>\n<\/header>\n<div class=\"textbox__content\">\n<pre id=\"h.st1gw9c00v55\" class=\"c1 c11\"><span class=\"c2\">$ <strong>test -r myfile<\/strong><\/span>\r\n<span class=\"c2\">$ <strong>echo $?<\/strong><\/span>\r\n<span class=\"c2\">0<\/span><\/pre>\n<\/div>\n<\/div>\n<p class=\"c3\"><span class=\"c0\">\u00a0<\/span><\/p>\n<p class=\"c15\"><span class=\"c0\">How does one interpret the return code? \u00a0 <\/span><\/p>\n<p class=\"c15\"><span class=\"c0\">Unix convention:<\/span><\/p>\n<table class=\"grid\" style=\"height: 58px\">\n<tbody>\n<tr class=\"c25\" style=\"height: 29px\">\n<th class=\"c27\" style=\"height: 29px;width: 126.146px\" colspan=\"1\" rowspan=\"1\" scope=\"row\">\n<p class=\"c1\"><span class=\"c0\">0 (zero)<\/span><\/p>\n<\/th>\n<td class=\"c39\" style=\"height: 29px;width: 109.271px\" colspan=\"1\" rowspan=\"1\">\n<p class=\"c1\"><span class=\"c0\">means TRUE<\/span><\/p>\n<\/td>\n<\/tr>\n<tr class=\"c25\" style=\"height: 29px\">\n<th class=\"c27\" style=\"height: 29px;width: 126.146px\" colspan=\"1\" rowspan=\"1\" scope=\"row\">\n<p class=\"c1\"><span class=\"c0\">not 0 (not zero)<\/span><\/p>\n<\/th>\n<td class=\"c39\" style=\"height: 29px;width: 109.271px\" colspan=\"1\" rowspan=\"1\">\n<p class=\"c1\"><span class=\"c0\">means FALSE<\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"c3\"><span class=\"c0\">\u00a0<\/span><\/p>\n<div class=\"textbox textbox--examples\">\n<header class=\"textbox__header\">\n<p class=\"textbox__title\"><span class=\"c0\">Example 2: \u00a0Compare if two strings are equal.<\/span><\/p>\n<\/header>\n<div class=\"textbox__content\">\n<pre id=\"h.g5mey17jgskn\" class=\"c1 c11\"><span class=\"c2\">$ <strong>test \"this\" = \"that\"<\/strong><\/span>\r\n<span class=\"c2\">$ <strong>echo $?<\/strong><\/span>\r\n<span class=\"c2\">1<\/span><\/pre>\n<\/div>\n<\/div>\n<p class=\"c3\"><span class=\"c0\">\u00a0<\/span><\/p>\n<h2 class=\"c15\"><span class=\"c0\">Synonym to the test command: \u00a0[<\/span><\/h2>\n<p class=\"c15\"><span class=\"c0\">For reasons of readability, many programmers with use the synonym or abbreviation for the test command which is the left square bracket: [ . \u00a0In all examples above, replace the word test with the left square bracket. \u00a0Note that a matching right square bracket needs to be added for syntactic reasons. \u00a0As with all Unix commands, spaces are a big deal and a space is required after the left square bracket and before the right square bracket.<\/span><\/p>\n<p class=\"c3\"><span class=\"c0\">\u00a0<\/span><\/p>\n<div class=\"textbox textbox--examples\">\n<header class=\"textbox__header\">\n<p class=\"textbox__title\"><span class=\"c0\">Example 3: Synonym to the test command [<\/span><\/p>\n<\/header>\n<div class=\"textbox__content\">\n<pre id=\"h.3arageq61qd\" class=\"c1 c11\">$ <strong>[ -r myfile ]<\/strong> # <span class=\"c19 c23\">note the space after the left bracket<\/span>\r\n$ <strong><span class=\"c2\">echo $?<\/span>\r\n<\/strong><span class=\"c2\">0<\/span><span class=\"c0\">\u00a0<\/span><\/pre>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<h1 id=\"h.qrw2dg5f4ok8\" class=\"c33 c11\"><span class=\"c30 c26\">Control Structures<\/span><\/h1>\n<h2 id=\"h.cmha19g1ccek\" class=\"c28 c11\"><span class=\"c29 c26\">if<\/span><\/h2>\n<p class=\"c15\"><span class=\"c0\">The if statement in Unix is the basic two-way branch.<\/span><\/p>\n<p>&nbsp;<\/p>\n<div class=\"textbox\">\n<p class=\"c15\"><span class=\"c0\">Syntax<\/span><\/p>\n<pre id=\"h.w0txwnysszi9\" class=\"c1 c11\"><span class=\"c2\">if unix_statements<\/span>\r\n<span class=\"c2\">then<\/span>\r\n<span class=\"c2\">  \u00a0actions_true<\/span>\r\n<span class=\"c2\">else \u00a0# optional<\/span>\r\n<span class=\"c2\">  \u00a0actions_false<\/span>\r\n<span class=\"c2\">fi<\/span><\/pre>\n<\/div>\n<p class=\"c15\"><span class=\"c0\">How does the if statement work? \u00a0Here is the sequence of operations. \u00a0The if statement will:<\/span><\/p>\n<ol class=\"c35 lst-kix_7gl0q3kbqu7q-0 start\" start=\"1\">\n<li class=\"c15 c37 li-bullet-0\"><span class=\"c0\">Run all the unix_statements. \u00a0 <\/span><\/li>\n<li class=\"c15 c37 li-bullet-0\"><span class=\"c0\">Check the return code ($?) of the last statement in the list (just prior to the keyword &#8220;then&#8221;. <\/span><\/li>\n<li class=\"c15 c37 li-bullet-0\">If the return code is true. the &#8220;then&#8221; clause is executed (<br \/>\n<span class=\"c4\">actions_true)<\/span>. \u00a0If the return code is false, the &#8220;else&#8221; clause is executed (<span class=\"c4\">actions_false)<\/span><span class=\"c0\">. \u00a0The else clause is optional (can be left out if not needed).<\/span><\/li>\n<\/ol>\n<div class=\"textbox textbox--examples\">\n<header class=\"textbox__header\">\n<p class=\"textbox__title\"><span class=\"c0\">Example of if<\/span><\/p>\n<\/header>\n<div class=\"textbox__content\">\n<pre id=\"h.kzwhp4rz3e59\" class=\"c1 c11\"><span class=\"c2\">#!\/bin\/bash<\/span>\r\n<span class=\"c2\">comfort=20<\/span>\r\n<span class=\"c2\">temperature=18<\/span>\r\n<span class=\"c2\">if echo The current temperature is $temperature<\/span>\r\n<span class=\"c2\">  \u00a0echo Temperature for comparison is $comfort<\/span>\r\n<span class=\"c2\">  \u00a0[ $temperature -lt $comfort ]<\/span>\r\n<span class=\"c2\">then<\/span>\r\n<span class=\"c2\">  echo It is cold.<\/span>\r\n<span class=\"c2\">else<\/span>\r\n<span class=\"c2\">  echo It is warm.<\/span>\r\n<span class=\"c2\">fi<\/span><span class=\"c0\">\u00a0<\/span><\/pre>\n<\/div>\n<\/div>\n<div id=\"h5p-5\">\n<div class=\"h5p-iframe-wrapper\"><iframe id=\"h5p-iframe-5\" class=\"h5p-iframe\" data-content-id=\"5\" style=\"height:1px\" src=\"about:blank\" frameBorder=\"0\" scrolling=\"no\" title=\"Unix if else\"><\/iframe><\/div>\n<\/div>\n<p class=\"c15\"><span class=\"c0\">Nested conditions are supported in Unix. \u00a0Any statement in the &#8220;then&#8221; or &#8220;else&#8221; clause can itself be another if statement.<\/span><\/p>\n<h3>elif<\/h3>\n<p class=\"c15\"><span class=\"c0\">Although the if statement is primarily a two-way branch (e.g. true or false), a multi-way branch (e.g. red, yellow, green) can be coded using a set of nested if statements. \u00a0Some computer languages support an &#8220;else-if&#8221;-type clause; Unix is one of them.<\/span><\/p>\n<p class=\"c15\"><span class=\"c0\">There is an &#8220;else-if&#8221; clause called &#8220;elif&#8221; which requires a statement just like the if clause. <\/span><\/p>\n<div class=\"textbox textbox--examples\">\n<header class=\"textbox__header\">\n<p class=\"textbox__title\">Example: elif<\/p>\n<\/header>\n<div class=\"textbox__content\">\n<pre id=\"h.kzwhp4rz3e59-15\" class=\"c1 c11\"><span class=\"c2\">if [ $temperature -lt $comfort ]<\/span>\r\n<span class=\"c2\">then<\/span>\r\n<span class=\"c2\">  echo It is cold.<\/span>\r\n<span class=\"c2\">elif [ $temperature -eq $comfort ]<\/span>\r\n<span class=\"c2\">then<\/span>\r\n<span class=\"c2\">  echo It is perfect.<\/span>\r\n<span class=\"c2\">else<\/span>\r\n<span class=\"c2\">  echo It is warm.<\/span>\r\n<span class=\"c2\">fi<\/span>\r\n<span class=\"c0\"> <\/span><\/pre>\n<p class=\"c15\">Note that the &#8220;elif&#8221; clause is actually a clause of the main &#8220;if&#8221; and <span class=\"c43\">not <\/span><span class=\"c0\">a nested if statement. \u00a0Thus, there is only one &#8220;fi&#8221; (end if) required for each opening &#8220;if&#8221; regardless of how many &#8220;elif&#8221; clauses there are.<\/span><\/p>\n<\/div>\n<\/div>\n<p><span><\/p>\n<div id=\"h5p-6\">\n<div class=\"h5p-iframe-wrapper\"><iframe id=\"h5p-iframe-6\" class=\"h5p-iframe\" data-content-id=\"6\" style=\"height:1px\" src=\"about:blank\" frameBorder=\"0\" scrolling=\"no\" title=\"Unix if fi\"><\/iframe><\/div>\n<\/div>\n<p><\/span><\/p>\n<p><span><\/p>\n<div id=\"h5p-7\">\n<div class=\"h5p-iframe-wrapper\"><iframe id=\"h5p-iframe-7\" class=\"h5p-iframe\" data-content-id=\"7\" style=\"height:1px\" src=\"about:blank\" frameBorder=\"0\" scrolling=\"no\" title=\"Unix if elif fi\"><\/iframe><\/div>\n<\/div>\n<p><\/span><\/p>\n<h2 id=\"h.6bd3ulkf9b8a\" class=\"c28 c11\">case<\/h2>\n<p class=\"c15\">Many computer languages support a multi-way branch. Unix is included as one of them.<\/p>\n<div class=\"textbox\">\n<p class=\"c15\"><span class=\"c26\">Simplified syntax:<\/span><\/p>\n<p class=\"c40 c14\"><span class=\"c19 c18\">\u00a0<\/span><\/p>\n<pre id=\"h.r7oy87p532rc\" class=\"c5 c1\"><span class=\"c2\">case $variable <\/span>\r\n<span class=\"c2\">in<\/span>\r\n<span class=\"c2\">  val1) action1;;<\/span>\r\n<span class=\"c2\">  val2) action2;;<\/span>\r\n<span class=\"c2\">  *) default action;;<\/span>\r\n<span class=\"c2\">esac<\/span><span class=\"c19 c18\">\u00a0<\/span><\/pre>\n<p class=\"c15\"><span class=\"c0\">The keywords are case, in, and esac. \u00a0The double semi-colon is a syntactic requirement to separate the inner clauses of the case statement.<\/span><\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<div class=\"textbox textbox--examples\">\n<header class=\"textbox__header\">\n<p class=\"textbox__title\">Example: \u00a0F<span class=\"c26\">lexible command line processing. <\/span><\/p>\n<\/header>\n<div class=\"textbox__content\">\n<p class=\"c15\"><span class=\"c26\">Allow your user to <\/span><span class=\"c0\">run your script in various ways.\u00a0 Accommodate all of the following invocations.<\/span><\/p>\n<pre class=\"c3\"><span class=\"c2\">$ .\/<strong>mysc<\/strong><\/span><span class=\"c2\"><strong>ript<\/strong> <\/span><span class=\"c19 c18\"># user omits filename; give second chance<\/span>\r\n$ .\/<strong><span class=\"c2\">myscript chapter3<\/span><\/strong><span class=\"c19 c18\"> # <\/span><span class=\"c18\">preferred syntax; just proceed<\/span>\r\n$ .\/<strong><span class=\"c2\">myscript chapter3 chapter5 <\/span><\/strong><span class=\"c19 c18\"># multiple arguments not s<\/span><span class=\"c18\">upported; \r\n<\/span><span class=\"c19 c18\">inform user<\/span>\r\n\r\n<span class=\"c2\">Place this code snippet at the beginning of your script like this:<\/span>\r\n<span class=\"c2\">$ <strong>cat myscript<\/strong>\r\ncase $# in<\/span>\r\n<span class=\"c2\">  0) echo Enter file name:<\/span>\r\n<span class=\"c2\">  \u00a0 \u00a0 \u00a0 \u00a0 \u00a0read arg1;;<\/span>\r\n<span class=\"c2\">  1) arg1=$1;;<\/span>\r\n<span class=\"c2\">  *) echo invalid number of arguments<\/span>\r\n<span class=\"c2\">  \u00a0 \u00a0echo \"Syntax: $0 filename\"<\/span>\r\n<span class=\"c2\">  \u00a0 \u00a0exit 1;;<\/span>\r\n<span class=\"c2\">esac<\/span>\r\n<span class=\"c2\"># rest of program continues after esac\r\n$<\/span><\/pre>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"textbox\">\n<p class=\"c15\"><span style=\"text-align: initial;font-size: 1em\">General syntax:<\/span><span class=\"c19 c18\">\u00a0<\/span><\/p>\n<pre><span class=\"c2\">case match_string_expr in<\/span>\r\n<span class=\"c2\">  match_pattern) action1;;<\/span>\r\n<span class=\"c2\">  match_pattern) action2;;<\/span>\r\n<span class=\"c2\">  ...<\/span><\/pre>\n<pre id=\"h.c44v635lr96i\" class=\"c5 c1\">esac<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"textbox textbox--examples\">\n<header class=\"textbox__header\">\n<p class=\"textbox__title\"><span class=\"c0\">Example: \u00a0Demonstrate pattern matching use in case statement. \u00a0Print a message about the length of the current month.<\/span><\/p>\n<\/header>\n<div class=\"textbox__content\">\n<pre>lhiraki@metis:~\/test$ <strong>cat case_month\r\n<\/strong>case $(date '+%m') in\r\n01|03|05|07|08|10|12)\r\n  \u00a0 \u00a0echo This is a long month;;\r\n04|06|09|11)\r\n  \u00a0 \u00a0echo This is a short month;;\r\n02)\r\n  \u00a0 \u00a0echo This is the shortest month;;\r\n*)\r\n  \u00a0 \u00a0echo Something wrong with date command;;\r\nesac\r\n\r\n# Example run in September\r\n\r\nlhiraki@metis:~\/test$ <strong>.\/case_month<\/strong>\r\nThis is a short month\r\nlhiraki@metis:~\/test$\r\n\r\n<\/pre>\n<p class=\"c3\"><span style=\"text-align: initial;font-size: 1em\">The date command is called with an option to return the numerical value of the month (e.g. Jan=01, Feb=02, etc.). \u00a0Depending on the month, a message is printed regarding the length of the month. \u00a0Months with the same number of days are grouped together using a pattern with the OR (vertical bar) syntax.<\/span><\/p>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<h3 class=\"c47 c11\" id=\"h.x0r9zvaxt49t\"><span class=\"c48 c26 c43\">Defensive programming<\/span><\/h3>\n<p class=\"c15\"><span class=\"c0\">Well the previous example is rather trivial and the date command has been well tested over the years, it is considered good practice to always have a default clause even if you think you have covered all possible conditions.<\/span><\/p>\n<h2 class=\"c15\">Summary<\/h2>\n<figure style=\"width: 519px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" alt=\"Entry arrow points to circle labled case; multiple arrows leave case circle to rectangles each with a separate action; each action has one arrow leaving which are all collected to a common exit.\" src=\"http:\/\/pressbooks.library.ryerson.ca\/opsyshiraki\/wp-content\/uploads\/sites\/330\/2022\/03\/image1-1.png\" style=\"width: 519.00px;height: 171.44px;margin-left: 0.00px;margin-top: 0.00px\" width=\"519\" height=\"170\" \/><figcaption class=\"wp-caption-text\">Flow of case statement: one entry point, only one action chosen.<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p class=\"c6\"><span class=\"c19 c23\">\u00a0<\/span>The match_string_expr is matched against each match_pattern in the order coded. \u00a0At the first match, the corresponding action is taken. \u00a0After one action is completed, the case statement terminates and execution continues after the esac (end case). \u00a0There is no &#8220;fall-through&#8221;. \u00a0The case statement will not execute multiple actions.<\/p>\n<p><span><\/p>\n<div id=\"h5p-8\">\n<div class=\"h5p-iframe-wrapper\"><iframe id=\"h5p-iframe-8\" class=\"h5p-iframe\" data-content-id=\"8\" style=\"height:1px\" src=\"about:blank\" frameBorder=\"0\" scrolling=\"no\" title=\"Unix case multi\"><\/iframe><\/div>\n<\/div>\n<p><\/span><\/p>\n<h2 id=\"h.df6xnvncbf1j\" class=\"c28 c11\">s<span class=\"c29 c26\">hift<\/span><\/h2>\n<p class=\"c15\"><span class=\"c0\">When processing multiple command-line arguments, it may be necessary to manipulate them to facilitate processing.<\/span><\/p>\n<p class=\"c15\">The shift command moves all command-line arguments one position to the left. \u00a0For example the second command line argument is moved into the first position; the third command line argument is moved into the second position, and so forth.<\/p>\n<p>&nbsp;<\/p>\n<div class=\"textbox textbox--examples\">\n<header class=\"textbox__header\">\n<p class=\"textbox__title\">Example of\u00a0 shift command<\/p>\n<\/header>\n<div class=\"textbox__content\">\n<pre id=\"h.7hj2ijv33hcw\" class=\"c9 c1\"><span class=\"c2\">lhiraki@thebe:~\/test$ <strong>cat shifter<\/strong><\/span>\r\n\r\n<span class=\"c2\">#!\/bin\/bash<\/span>\r\n<span class=\"c2\">echo The 1st arg is $1<\/span>\r\n<span class=\"c2\">echo The 2nd arg is $2<\/span>\r\n<span class=\"c2\">shift<\/span>\r\n<span class=\"c2\">echo The 2nd arg is $1<\/span>\r\n<span class=\"c2\">shift<\/span>\r\n<span class=\"c2\">echo The 3rd arg is $1<\/span><span class=\"c19 c18\">\u00a0<\/span><\/pre>\n<p class=\"c32 c36\"><span class=\"c19 c18\">Execution results in:<\/span><\/p>\n<pre id=\"h.u6zk61ioj0o6\" class=\"c9 c1\"><span class=\"c2\">lhiraki@thebe:~\/te<\/span><span class=\"c2\">st$ <strong>.\/shifter apple pear grape<\/strong><\/span>\r\n<span class=\"c2\">The 1st arg is apple<\/span>\r\n<span class=\"c2\">The 2nd arg is pear<\/span>\r\n<span class=\"c2\">The 2nd arg is pear<\/span>\r\n<span class=\"c2\">The 3rd arg is grape<\/span>\r\n<span class=\"c2\">lhiraki@thebe:~\/test$<\/span><\/pre>\n<p id=\"h.2myj48q95b02\" class=\"c9 c1 c14\"><span class=\"c2\">\u00a0<\/span><\/p>\n<\/div>\n<\/div>\n<h3 class=\"c11 c47\" id=\"h.n1jlngeudrck\">The exit command<\/h3>\n<p class=\"c15\">The exit command <span class=\"c26\">does <\/span>two<span class=\"c0\">\u00a0things:<\/span><\/p>\n<ol class=\"c35 lst-kix_lp44ssjvz62w-0 start\" start=\"1\">\n<li class=\"c15 c37 li-bullet-0\">It <span class=\"c0\">terminates the current shell (or script), returning control to the calling shell, if any.<\/span><\/li>\n<li class=\"c15 c37 li-bullet-0\">It <span class=\"c26\">sets the return code ($?) for your script<\/span>.<\/li>\n<\/ol>\n<div class=\"textbox textbox--examples\">\n<header class=\"textbox__header\">\n<p class=\"textbox__title\">Example &#8211; exit command usage<\/p>\n<\/header>\n<div class=\"textbox__content\">\n<p><span class=\"c0\">The trivial but illustrative script exit_example shows the exit command setting return code for the script to 3 and then terminating the script.\u00a0 Control returns to the calling program, in this case just back to the operating system prompt.<\/span><\/p>\n<pre>$ <strong>cat exit_example<\/strong>\r\n#!\/bin\/bash\r\nexit 3\r\necho This line never executed.\r\n\r\n$ <strong>.\/exit_example<\/strong>\r\n$ <strong>echo $?<\/strong>\r\n3\r\n$<\/pre>\n<p><span class=\"c0\">Note that one must inspect the return code ($?) immediately after running exit_example.\u00a0 The return code is updated (overwritten) by each Unix command executed. <\/span><\/p>\n<\/div>\n<\/div>\n<p class=\"c15\"><span class=\"c0\">The exit command is typically used to terminate a script midway through often due to an errror condition. The other primary use of the exit command is for a sub-script to communicate information back to the calling script.<\/span><\/p>\n<p>&nbsp;<\/p>\n<div class=\"textbox textbox--examples\">\n<header class=\"textbox__header\">\n<p class=\"textbox__title\">Example &#8211; parent\/child script relation<\/p>\n<\/header>\n<div class=\"textbox__content\">\n<table class=\"grid\" style=\"width: 284px\">\n<thead>\n<tr class=\"c25\">\n<th class=\"c41\" style=\"width: 199.302px\" scope=\"col\">\n<p class=\"c1\"><span class=\"c0\">parent<\/span><\/p>\n<\/th>\n<th class=\"c12\" style=\"width: 57.8646px\" scope=\"col\">\n<p class=\"c1\"><span class=\"c0\">child<\/span><\/p>\n<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr class=\"c25\">\n<td class=\"c41\" style=\"width: 199.302px\">\n<pre class=\"c1\"><span class=\"c2\">.\/child\u00a0\r\n<\/span><span class=\"c2\">ret_val=$?\r\n<\/span><span class=\"c2\">case $ret_val<\/span><\/pre>\n<\/td>\n<td class=\"c12\" style=\"width: 57.8646px\">\n<pre id=\"h.zgjskii3y53y\" class=\"c1 c49\"><span class=\"c2\">...<\/span>\r\n<span class=\"c2\">\u00a0 \u00a0exit 2\r\n<\/span><span class=\"c2\">...\r\n<\/span><span class=\"c2\">exit 0<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"c40\"><span class=\"c0\">Note: parent-child relationship can be used for team project where multiple people can work on the same file at the same time.<\/span><\/p>\n<\/div>\n<\/div>\n<p class=\"c3\"><span class=\"c0\">\u00a0<\/span><\/p>\n<h2 id=\"h.9e9qi4h1u243\" class=\"c11 c28\"><span class=\"c26 c29\">Shebang line<\/span><\/h2>\n<p class=\"c15\"><span class=\"c0\">To specify which interpreter Unix should use when executing your script, as the first line of the file place the path to the interpreter after &#8220;#!&#8221;. \u00a0While the number sign character (#) normally introduces a comment, when used with the exclamation mark at the beginning of a file, Unix will load the interpreter specified in the path to run the rest of the script file.<\/span><\/p>\n<p class=\"c15\"><span class=\"c0\">This is especially important to make your script portable in Unix environments. \u00a0If you write your script in bash, and you give your script for someone else to use who works in a c-shell or Korn shell environment, your script may not work properly. \u00a0To ensure that the script is run under bash, you must specify the shebang line.<\/span><\/p>\n<p class=\"c15\">Careful: \u00a0The shebang line must be the <span class=\"c43\">first line of the file<\/span><span class=\"c0\">, not just the first line of text, or the first line of code. \u00a0A common mistake is to have a blank line as the first line, or some comments above the shebang line. \u00a0Unix does not look beyond the first line of the file in order to identify the expected interpreter.<\/span><\/p>\n<table class=\"grid\">\n<thead>\n<tr class=\"c25\">\n<th class=\"c22\" style=\"width: 168px\" colspan=\"1\" rowspan=\"1\" scope=\"col\">\n<p class=\"c1\"><span class=\"c0\">Right<\/span><\/p>\n<\/th>\n<th class=\"c22\" style=\"width: 130px\" colspan=\"1\" rowspan=\"1\" scope=\"col\">\n<p class=\"c1\"><span class=\"c0\">Wrong<\/span><\/p>\n<\/th>\n<th class=\"c22\" style=\"width: 117px\" colspan=\"1\" rowspan=\"1\" scope=\"col\">\n<p class=\"c1\"><span class=\"c0\">Wrong<\/span><\/p>\n<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr class=\"c25\">\n<td class=\"c22\" style=\"width: 168px\" colspan=\"1\" rowspan=\"1\">\n<pre id=\"h.k9b970rv56o2\" class=\"c1 c11\"><span class=\"c42\">1:#!\/bin\/bash\r\n<\/span><span class=\"c2\">2:#comments\r\n<\/span><span class=\"c2\">3:#comments\r\n<\/span><span class=\"c2\">4:code begins here\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/span><\/pre>\n<\/td>\n<td class=\"c22\" style=\"width: 130px\" colspan=\"1\" rowspan=\"1\">\n<pre id=\"h.gz85vvtz5oro\" class=\"c1 c11\"><span class=\"c2\">1:#comments\r\n<\/span>2:#<span class=\"c4\">comments\r\n<\/span><span class=\"c42\">3:#!\/bin\/bash\r\n<\/span><span class=\"c2\">4:code begins here<\/span><\/pre>\n<\/td>\n<td class=\"c22\" style=\"width: 117px\" colspan=\"1\" rowspan=\"1\">\n<pre id=\"h.u6ke8weax8l\" class=\"c1 c11 c14\"><span class=\"c42\">1:\r\n2:#!\/bin\/bash\r\n3:\r\n4:<\/span><span class=\"c2\">code begins here<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span><\/p>\n<div id=\"h5p-9\">\n<div class=\"h5p-iframe-wrapper\"><iframe id=\"h5p-iframe-9\" class=\"h5p-iframe\" data-content-id=\"9\" style=\"height:1px\" src=\"about:blank\" frameBorder=\"0\" scrolling=\"no\" title=\"Unix shebang placement\"><\/iframe><\/div>\n<\/div>\n<p><\/span><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"author":2,"menu_order":5,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":[],"pb_section_license":""},"chapter-type":[],"contributor":[61],"license":[],"class_list":["post-37","chapter","type-chapter","status-publish","hentry","contributor-lhiraki"],"part":23,"_links":{"self":[{"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/pressbooks\/v2\/chapters\/37","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/wp\/v2\/users\/2"}],"version-history":[{"count":3,"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/pressbooks\/v2\/chapters\/37\/revisions"}],"predecessor-version":[{"id":109,"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/pressbooks\/v2\/chapters\/37\/revisions\/109"}],"part":[{"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/pressbooks\/v2\/parts\/23"}],"metadata":[{"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/pressbooks\/v2\/chapters\/37\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/wp\/v2\/media?parent=37"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/pressbooks\/v2\/chapter-type?post=37"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/wp\/v2\/contributor?post=37"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/pressbooks.library.torontomu.ca\/opsyshiraki\/wp-json\/wp\/v2\/license?post=37"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}