I've been working on some script, and took a simple short snapshot out and wanted to share to see if someone could help out,. I am basically using prompt to execute my script. First prompt will ask me if I want to continue, which works fine, second prompt will ask me if I want to write my output into txt file, which works fine too. However my question is if there's a way to tell the script somehow to write the output into txt file when I hit Yes, but more likely if there's a way to do it without duplicating my commands? I know I could just write all commands into the output prompt, and depending if I hit yes or no it would write or skip writing.
#!/bin/bash
# Disclaimer
read -p "[Y] for Yes or [N] for GTFO: " prompt
if [[ $prompt == "y" || $prompt == "" ]]
then
# Output save
read -p "[Y] to save output to txt [N] don't save: " prompt
if [[ $prompt == "y" || $prompt == "" ]]
then
touch /root/Desktop info.txt
ifconfig >> /root/Desktop info.txt
fi
printf "\n"
printf "Executing script, and will scan the system for information \n"
sleep 1.4
printf "\n"
# Will Check for IP
printf "IP Addresses: \n"
ifconfig
else
printf "\n"
printf "Exiting, Bye \n"
fi