Then, we can test the autogenerated script by running. Once this process is completed, it will produce a script in the local directory called script.exp. Password: => This script will install: /usr/local/bin/brew /usr/local/share/doc/homebrew /usr/local/share/man/man1/brew.1 /usr/local/share/zsh/site-functions/_brew /usr/local/etc/bash_completion.d/brew /usr/local/Homebrew Press RETURN/ENTER to continue or any other key to abort: => /usr/bin/sudo /usr/sbin/chown -R admin:admin /usr/local/Homebrew => Downloading and installing Homebrew. When we do, we’ll be prompted as we normally would when installing Homebrew, which will look like the following: ~ % autoexpect /Users/admin/install_brew.sh autoexpect started, file is script.exp => Checking for `sudo` access (which may request your password). Then, with autoexpect installed, we can create our autogenerated expect script by running the following: autoexpect /path/to/install_brew.sh First, we’ll create a script called install_brew.sh, which will need to contain the following: # install_brew.sh /bin/bash -c "$(curl -fsSL )" To demonstrate this process, let’s create a pair of scripts that install Homebrew without requiring an interactive user session. NOTE: the filepath to our script.sh is hardcoded into the script.exp, so it’s important to put it where you’d like it to live in the filesystem before passing the filepath as an argument to autoexpect, as shown above. You can then execute the original process defined in your script.sh file by calling. It then produces an expect script called script.exp that – when called – will call your targeted process once more, but this time it will enter the same input you passed manually, but in an automated fashion. This command tells expect to call the process you’d like to automate, and then walks you through the actual interactive prompts associated with the script.sh process. Then, we’ll need to wrap our targeted process in a bash script that we can pass as an argument to autoexpect, like so: autoexpect /path/to/our/script.sh To do so, we’ll need to install autoexpect, which we can achieve on macOS by running the following: brew install expect These scripts can be written manually, but today, we’ll look at a simple, automated way of creating the expect scripts themselves. This could be user credentials, specific keystrokes or anything a user might be prompted for in an interactive CLI. Scripts written with expect work by “expecting” a specific prompt or response from the interactive process being called, and passing the appropriate response when prompted. Expect is a Unix based utility that is a common means of automating processes that require user input.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |