Issue
I need to write a bash script that captures user input URL and downloads the URL using wget, saving to user determined folder.
Having trouble with the URL in my if statement
script:
#!/bin/bash
until [[ $REPLY = "exit" ]]
do
read -p "Please type the URL of a file to download or type "exit" to quit: " REPLY
echo
if [[ $REPLY = "$url" ]]; then
read -p "Please type your download location: " user_dir
mkdir -p $user_dir &&
wget $url -o $user_dir
fi
done
echo "Thank you. Goodbye!"
exit 0
Solution
Try this
#!/bin/bash
until [[ $url = "exit" ]]
do
read -p "Please type the URL of a file to download or type exit to quit: " url
if [[ $url == "https://"* ]] || [[ $url == "http://"* ]]; then
read -p "Please type your download location: " user_dir
mkdir -p $user_dir
wget $url -P $user_dir
else
echo "No valid url"
fi
done
echo "Thank you. Goodbye!"
Some changes that I made:
- To save in a dir with
wget
you must use-P
not-o
- In line
[[ $REPLY = "$url" ]];
I assumed that you want to check url schema(I'm not doing a complete schema url validation, but you'll get the idea). - Not sure why you use
mkdir &&
so i removed it, as i think is not necessary here, also I removed aecho
.
Answered By - Joaquin