DirectedSoul
DirectedSoul

Reputation: 91

Is there a way that i can download files from different URL's based on user input?

There are two releases 1. Dev available at https://example.com/foo/new-package.txt 2. GA available at https://example.com/bar/new-package.txt I want the user to enter his choice of Dev or GA and based on that need to download the files, in the shell script is there a better way to do it?

There is a file which has environment variables that I'm sourcing inside another script.

env_var.sh

#!/bin/bash

echo "Enter your release"
export release=''   #either Dev or GA

This file will be sourced from another script as

download.sh

#!/bin/bash

. ./env_var.sh    #sourcing a environment var file

wget https://<Dev or GA URL>/new-package.txt 

My main problem is how to set the wget URL based on the release set in env_var file. Any help is appreciated.

Upvotes: 0

Views: 153

Answers (1)

ThatsWhatSheCoded
ThatsWhatSheCoded

Reputation: 381

Have you considered using read to get the user input?

read -p 'Selection: ' choice

You could then pass ${choice} to a function that has case statements for the urls:

get_url() {
    case $1 in
        'dev' ) wget https://example.com/foo/new-package.txt ;;
        'ga' ) wget https://example.com/bar/new-package.txt ;;
        \? ) echo "Invalid choice" ;;
    esac
}

For more information on read, a good reference is TLDP's guide on user input.

Edit: To source a config file, run the command source ${PATH_TO_FILE}. You would then be able to pass the variable to the get_url() function for the same result.

Upvotes: 1

Related Questions