{"id":244462,"date":"2021-05-07T15:00:27","date_gmt":"2021-05-07T12:00:27","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/automate-inputs-to-linux-scripts-with-expect-cloudsavvy-it\/"},"modified":"2021-05-07T15:00:27","modified_gmt":"2021-05-07T12:00:27","slug":"automate-inputs-to-linux-scripts-with-expect-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/automate-inputs-to-linux-scripts-with-expect-cloudsavvy-it\/","title":{"rendered":"#Automate Inputs to Linux Scripts With expect \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_90 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6b0147e54da88\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6b0147e54da88\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/automate-inputs-to-linux-scripts-with-expect-cloudsavvy-it\/#Automation_Means_Efficiency\" >Automation Means Efficiency<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/automate-inputs-to-linux-scripts-with-expect-cloudsavvy-it\/#Installing_the_expect_Command\" >Installing the expect Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/automate-inputs-to-linux-scripts-with-expect-cloudsavvy-it\/#How_expect_Works\" >How expect Works<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#Automate Inputs to Linux <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">Script<\/a>s With expect \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage alignnone size-full wp-image-4038\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2017\/07\/add8ac45.png?width=1200&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Bash Shell\" width=\"1400\" height=\"600\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The Linux expect command lets you automate interactions with scripts and programs. You can send any kind of response to the script when it is waiting for some text input.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Automation_Means_Efficiency\"><\/span>Automation Means Efficiency<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>When you administer a Linux computer or group of computers you\u2019ll run up against many repetitive tasks. The obvious answer is to write a script to perform the bulk of the work for you. The Bash shell, like all modern shells, provides a rich and flexible scripting language.<\/p>\n<p>Scripting a long-winded or tedious job gives you the flexibility to run that task when the office is closed, or in the quietest periods of a 24\/7 operation. Scripts also give you repeatability. They won\u2019t forget to perform a step, no matter how many times they are asked to perform the same chores.<\/p>\n<p>That\u2019s fine, as far as it goes. But if your script requires user interaction, or if it calls a program that will need human input, what can you do? You don\u2019t want to have to be present when the script runs, or if you are present, to be watching the terminal window ready to jump in and hit a few keys.<\/p>\n<p>Linux has the <code>yes<\/code> command, <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/man7.org\/linux\/man-pages\/man1\/yes.1.html\">which sends a stream of \u201cy\u201d characters<\/a>\u00a0or other user-specified string to the terminal window. If a program is waiting for a response to a yes or no question it\u2019ll be force-fed one of the \u201cy\u201d characters. It\u2019ll accept it as input and will be able to proceed. You need to pipe the output from\u00a0<code>yes<\/code> into the script.<\/p>\n<pre>yes | script.sh<\/pre>\n<p>You might not want to send a \u201cyes.\u201d Perhaps you need to respond with a \u201cno.\u201d You can do that too, using the <code>yes<\/code> command. You can send any phrase you like, you\u2019re not restricted to responses like \u201cy\u201d, \u201cY\u201d, \u201cYes\u201d, \u201cn\u201d, \u201cN\u201d, or \u201cNo.\u201d Perhaps you need to send an \u201cr\u201d for reinstall to an \u201d <code>install\/upgrade\/reinstall [i\/u\/r]<\/code> \u201d prompt.<\/p>\n<pre>yes r<\/pre>\n<p>The <code>yes<\/code> command can\u2019t cope if it needs to provide more than one different type of response. For that situation, we need to use <code>expect<\/code> .<\/p>\n<p><strong>RELATED:<\/strong> <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.howtogeek.com\/415535\/how-to-use-the-yes-command-on-linux\/\"><strong><em>How to Use the yes Command on Linux<\/em><\/strong><\/a><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Installing_the_expect_Command\"><\/span>Installing the expect Command<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>We tested expect on Ubuntu, Fedora, and Manjaro. The package wasn\u2019t bundled with these distributions, so it had to be installed. On Ubuntu type:<\/p>\n<pre>sudo apt install expect<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11021\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/05\/4a47a0db.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"644\" height=\"55\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>On Fedora, the command you need is:<\/p>\n<pre>sudo dnf install expect<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11022\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/05\/fb5c81ed.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"644\" height=\"55\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>On Manjaro we use <code>pacman<\/code>:<\/p>\n<pre>sudo pacman -Sy expect<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11023\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/05\/10fb15c7.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"644\" height=\"55\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"How_expect_Works\"><\/span>How expect Works<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>expect<\/code> command lets you manage the two ends of a conversation between your set of prepared answers and the program or script that you\u2019ll be sending them to. You do this by creating an \u201cexpect\u201d script that watches for prompts in the main script and sends the appropriate response for each one.<\/p>\n<p>Let\u2019s say you have a backup script that asks for a source directory name and a target directory name. it then makes a copy of the source directory in the target directory. Without the bits that do the file copying, your script might look something like this:<\/p>\n<pre>#!\/bin\/bash&#13;\n&#13;\necho \"Directory to backup?\"&#13;\nread source_directory&#13;\n&#13;\necho \"Backup location?\"&#13;\nread target_directory&#13;\n&#13;\necho&#13;\necho \"Target directory:\" $target_directory<\/pre>\n<p>All this script does is ask for the paths to the two directories. It prints the target directory to the terminal window so that we can see that it received a response from <code>expect<\/code> and that it could read it correctly.<\/p>\n<p>To provide the other half of the conversation we create an \u201cexpect script.\u201d By convention, these have a \u201c.exp\u201d extension. This example will work with our \u201cbackup.sh\u201d script.<\/p>\n<pre>#!\/usr\/bin\/expect -f&#13;\n&#13;\nset timeout -1&#13;\n&#13;\nspawn .\/backup.sh&#13;\n&#13;\nexpect \"Directory to backup?r\"&#13;\nsend -- \"\/home\/dave\/Documents\/r\"&#13;\n&#13;\nexpect \"Backup location?r\"&#13;\nsend -- \"\/<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/social-mediaa\/\" data-internallinksmanager029f6b8e52c=\"1\" title=\"Social Media\" target=\"_blank\" rel=\"noopener\">media<\/a>\/dave\/external\/backupr\"&#13;\n&#13;\nexpect eof<\/pre>\n<p>This shows the main three commands in expect scripts, the <code>spawn<\/code>, <code>expect<\/code>, and <code>send<\/code> commands.<\/p>\n<p>The first thing to notice <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Shebang_(Unix)\">is the shebang<\/a> is referring to <code>expect<\/code>, not to Bash. The <code>-f<\/code> (file) flag tells <code>expect<\/code> the responses are coming from a file.<\/p>\n<p>We effectively turn the timeouts off by setting them to infinity with -1.<\/p>\n<p>The <code>spawn<\/code> command launches the backup script.<\/p>\n<p>We know the two questions the \u201cbackup.sh\u201d script will ask us.\u00a0For each question in turn we create an \u201cexpect\u201d line. These contain the text prompt that will be sent to the terminal window by the \u201cbackup.sh\u201d script. This what the <code>expect<\/code> program will watch out for. When <code>expect<\/code> sees the prompt, the text in the <code>send<\/code> line is returned to the \u201cbackup.sh\u201d script.<\/p>\n<p>Make both scripts executable:<\/p>\n<pre>chmod +x backup.sh<\/pre>\n<pre>chmod +x backup.exp<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11028\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/05\/09dd8c26.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"644\" height=\"75\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>And use the \u201cbackup.exp\u201d script to fire off the process.<\/p>\n<pre>.\/backup.exp<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11029\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/05\/8266e4bf.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"644\" height=\"210\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>You can see the two prompts from \u201cbackup.sh\u201d and the responses from \u201cbackup.exp.\u201d<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>\u2013end\u2013\n<\/p><\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/11017\/automate-inputs-to-linux-scripts-with-expect\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#Automate Inputs to Linux Scripts With expect \u2013 CloudSavvy IT&#8221; The Linux expect command lets you automate interactions with scripts and programs. You can send any kind of response to the script when it is waiting for some text input. Automation Means Efficiency When you administer a Linux computer or group of computers you\u2019ll run&#8230;<\/p>\n","protected":false},"author":1,"featured_media":244463,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2017\/07\/add8ac45.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-244462","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/244462","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=244462"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/244462\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/244463"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=244462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=244462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=244462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}