Hi,
I am trying to make a flexible bash script which does the following:
Downloads a URL from a variable
Unzips it
Deletes the original archive
The problem is, the format could be .tar, .tar.gz etc, it wont be constant.
This is what I have currently:
#!/bin/bash
dl_dir="/opt"
function file_download()
{
wget $2 -P "$dl_dir"
tar xzf $1 -C $1
}
function user_create()
{
useradd -s /usr/sbin/nologin -r $2
chown -R $1 /opt/$1
}
# NGINX
name="nginx"
name_url="http://nginx.org/download/nginx-1.1.2.tar.gz"
file_download "$name" "${name}_url"
user_create "$name" "${name}_user"
Is the best approach to grab the filename from the URL and store that as a variable that I can use for the Untar and delete operation?
---------- Post updated at 03:59 PM ---------- Previous update was at 03:44 PM ----------
In an effort to answer my own question I wrote this:
#!/bin/bash
dl_dir="/opt"
function file_download()
{
# Grab filename from URL string
filename={$2##*/}
# Download URL into download directory
wget $2 -P "$dl_dir"
# Untar the filename and move to named folder in download directory
tar xzf $filename -C "${dl_dir}/${name}"
# Remove original tarball
rm "${dl_dir}/${name}"
}
# NGINX
name="nginx"
name_url="http://nginx.org/download/nginx-1.1.2.tar.gz"
file_download "$name" "${name}_url"