Issue
I'm trying to create a new directory based on user input and then creating a new .txt file in the directory created that has a content from user input
I managed to create the directory as following:
echo "Enter directory name"
read dirname
if [ ! -d "/root/test1/test2/$dirname" ]
then
echo "File doesn't exist. Creating now"
mkdir /root/test1/test2/$dirname
echo "File created"
else
echo "File exists"
fi
now i want to take a user input to put into the domains.txt file for example
the script the script should ask:
add your domains:
the user will add a domains like
domain.com
domain2.com
asdf.com
then the domains that taken from the user input will be added to domains.txt file and separated every domain in a new line
the final domains.txt file should look like :
cat domain.txt
domain.com
domain2.com
asdf.com
Solution
Here is an example of how you can do it:
echo "Enter domains (empty line to stop):"
read i
while [ ! -z "$i" ]
do
echo $i >> domains.txt # replace domains.txt with your filename
read i
done
echo "Result:"
cat domains.txt
This code will take domains from the user input and appends them at the end of the file. To stop the loop just enter an empty line.
Small explanation:
[ ! -z "$i" ]
checks ifi
is not empty. The loop breaks on emptyi
;echo $i >> domains.txt
appends value ofi
at the end of thedomains.txt
Answered By - Pavlo Myroniuk Answer Checked By - Robin (WPSolving Admin)