Making an Interactive Script
We'll cover the following...
Currently, the script uses the hardcoded value of mysite
for the name of the new website. All of our websites won’t have the same name, so we can make the script more flexible by having it prompt us for the directory we want to create.
Using the $
operator
Let’s modify the variable declaration in our script so it uses the read
command to prompt for the directory and assign the value for the directory
variable. Then, we wrap each use of ${directory}
with double quotes so we can support situations where a user might enter a directory name with spaces.
#!/usr/bin/env bash
set -e
read -p "Enter the name of the directory to create: " directory
mkdir -p "${directory}"/{images,scripts,styles}
touch "${directory}"/styles/style.css
cat << 'EOF' > "${directory}"/index.html
The read
command displays the prompt and pauses the script’s execution. The user can then type the directory they want to create. When they ...