How to Script a "Yes" Response for Installing Programs

How do I script a yes response for installing programs?

The 'yes' command will echo 'y' (or whatever you ask it to) indefinitely. Use it as:

yes | command-that-asks-for-input

or, if a capital 'Y' is required:

yes Y | command-that-asks-for-input

If you want to pass 'N' you can still use yes:

yes N | command-that-asks-for-input

How do I script a yes response for installing programs when I already use echo password to auto pass the authentication

What about

system "( echo \"$password\" ; yes ) | sudo -S ..."

Note that it can break (same as the original code) if the password contains a double quote, dollar sign etc.

How do I write a bash script that reads a list from a text file and takes user interaction for each item?

Because the input for the while; do...done < "$file" code block is handled from the file containing the software names to install; the read -rp "Would you like to install $line? [Y/n]" yn which was not given a specific input handler, just inherits the file input from its outer code block.

Rather than reading user input, it reads (consumes) lines from the software list file.

There need to be distinct input handlers for reading file and reading user input.

Edited with suggestion from John Kugelman

Here it is with a couple other fixes:

#!/usr/bin/env bash

InstallAptSW() {
file='./apps/apt-apps'

# Reads from File Handler 3 which gets input from "$file"
while read -r line <&3; do
# printf before read -r avoid using the read -p bashism
printf 'Would you like to install %s [Y/n]? ' "$line"

# Default file handler is not used by "$file",
# so it takes user input without interference
read -r yn
yn=${yn:-Y}
case $yn in
[Yy]*) echo sudo apt install -y "$line" ;;
[Nn]*)
# Single-quotes are better
# when no expansion occurs within string
printf '\nSkipping'
break
;;
*) echo 'Please answer yes or no.' ;;
esac
# Double quote the "$file" variable to prevent
# word splitting and globing pattern matching
# Get "$file" input at the File-Handler 3
done 3< "$file"
}

InstallAptSW

Continue if response is 'yes'

You've got the code that makes the directory inside an else statement. As long as the directory exists, it will never get to that command. Try this (just replacing else with fi.)

if [ -d /home/philip_lee/csv_dump/$TABLENAME ]; then
echo "Directory '$TABLENAME' already exists. Delete the existing directory?"
select yn in "Yes" "No"; do
case $yn in
Yes ) rm -rf /home/philip_lee/csv_dump/$TABLENAME
break;;
No ) echo "$EXIT"
exit 1;;
esac
done
fi
echo "Please enter the email you would like the results to be sent to:"
read RECIPIENT
mkdir /home/philip_lee/csv_dump/$TABLENAME

For the record, this is how I would do the same bit of script:

if [ -d "/home/philip_lee/csv_dump/$TABLENAME" ]; then
read -n 1 -p "Directory '$TABLENAME' already exists. Delete the existing directory? " YN
if [ $YN != "Y" -a $YN != "y" ]; then
echo "$EXIT"
exit 1
fi
fi
read -p "Please enter the email you would like the results to be sent to: " RECIPIENT
mkdir "/home/philip_lee/csv_dump/$TABLENAME"

Always quote variables; it's unlikely there will be a space in what I assume is a database table name, but it's good practice anyway. read accepts a prompt as part of the command, and doesn't force you into a control structure like select does. case seems like overkill for two options (and personally, I hate the syntax!)

Python - scripting a “yes” response to all questions/prompts

Try using yes:

yes | python ./script.py

If you have a more complex state to manage during interaction, there is also expect.

yes emits y by default, but you can customize it by providing an argument (e.g. yes yes), thanks @tobias_k. If you need a portable way (in Python), go with the suggestion from Jean-François Fabre (or just hack the script).

Making a PowerShell more efficient other than if statements


Is there any way to do this without a sea of if statements?

Sure - organize your applications into an ordered dictionary and loop through the entries:

$applications = [ordered]@{
"App One" = "path\to\application1.exe"
"App Two" = "path\to\application2.exe"
# ...
}

foreach($appName in $applications.Keys){
$response = Read-Host -Prompt "Would you like to install '${appName}'? Please type Yes or No"

if($response -eq 'yes'){
Start-Process -Path $applications[$appName]
Start-Sleep -Seconds 30
} else {
Write-Host "Installation of '${appName}' was skipped"
}
}


Is there anyway to stop the Start- Sleep process when the application is done?

Yes, use Start-Process -Wait instead of sleeping for a set duration:

Start-Process -Path $applications[$appName] -Wait

How do I prompt for Yes/No/Cancel input in a Linux shell script?

The simplest and most widely available method to get user input at a shell prompt is the read command. The best way to illustrate its use is a simple demonstration:

while true; do
read -p "Do you wish to install this program? " yn
case $yn in
[Yy]* ) make install; break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done

Another method, pointed out by Steven Huwig, is Bash's select command. Here is the same example using select:

echo "Do you wish to install this program?"
select yn in "Yes" "No"; do
case $yn in
Yes ) make install; break;;
No ) exit;;
esac
done

With select you don't need to sanitize the input – it displays the available choices, and you type a number corresponding to your choice. It also loops automatically, so there's no need for a while true loop to retry if they give invalid input.

Also, Léa Gris demonstrated a way to make the request language agnostic in her answer. Adapting my first example to better serve multiple languages might look like this:

set -- $(locale LC_MESSAGES)
yesexpr="$1"; noexpr="$2"; yesword="$3"; noword="$4"

while true; do
read -p "Install (${yesword} / ${noword})? " yn
if [[ "$yn" =~ $yesexpr ]]; then make install; exit; fi
if [[ "$yn" =~ $noexpr ]]; then exit; fi
echo "Answer ${yesword} / ${noword}."
done

Obviously other communication strings remain untranslated here (Install, Answer) which would need to be addressed in a more fully completed translation, but even a partial translation would be helpful in many cases.

Finally, please check out the excellent answer by F. Hauri.



Related Topics



Leave a reply



Submit