Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ideas: [ADD] extract and exesudo functions to shell #20

Open
efargas opened this issue Nov 4, 2015 · 1 comment
Open

Ideas: [ADD] extract and exesudo functions to shell #20

efargas opened this issue Nov 4, 2015 · 1 comment

Comments

@efargas
Copy link

efargas commented Nov 4, 2015

extract function very useful:

function extract(){
    if [ $# -lt 2 ]; then
        if [ -f $1 ] ; then
            case $1 in
                *.tar.bz2)
                    tar xvjf $1
                    ;;
                *.tar.gz)
                    tar xvzf $1
                    ;;
                *.bz2)
                    bunzip2 $1
                    ;;
                *.rar)
                    unrar x $1
                    ;;
                *.gz)
                    gunzip $1
                    ;;
                *.tar)
                    tar xvf $1
                    ;;
                *.tbz2)
                    tar xvf $1
                    ;;
                *.tgz)
                    tar xvf $1
                    ;;
                *.zip)
                    unzip $1
                    ;;
                *.Z)
                    uncompress $1
                    ;;
                *.7z)
                    7z x $1
                    ;;
                *)
                    echo "'$1' cannot be extracted via extract"
                    return 1
                    ;;
            esac
        else
            echo "'$1' is not a valid file"
            return 2
        fi
    fi

    if [ $# -ge 2 ]; then
        if [ -f $1 ] ; then
            case $1 in
                *.tar.bz2)
                    if [ -d $2 ]; then
                        tar xvjf $1 -C $2
                    else
                        mkdir -p $2
                        tar xvjf $1 -C $2
                    fi
                    ;;
                *.tar.gz)
                    if [ -d $2 ]; then
                        tar xvzf $1 -C $2
                    else
                        mkdir -p $2
                        tar xvzf $1 -C $2
                    fi
                    ;;
                *.bz2)
                    bunzip2 $1
                    ;;
                *.rar)
                    if [ -d $2 ]; then
                        unrar x $1 $2
                    else
                        mkdir -p $2
                        unrar x $1 $2
                    fi
                    ;;
                *.gz)
                    gunzip $1
                    ;;
                *.tar)
                    if [ -d $2 ]; then
                        tar xvf $1 -C $2
                    else
                        mkdir -p $2
                        tar xvf $1 -C $2
                    fi
                    ;;
                *.tbz2)
                    if [ -d $2 ]; then
                        tar xvjf $1 -C $2
                    else
                        mkdir -p $2
                        tar xvjf $1 -C $2
                    fi
                    ;;
                *.tgz)
                    if [ -d $2 ]; then
                        tar xvzf $1 -C $2
                    else
                        mkdir -p $2
                        tar xvzf $1 -C $2
                    fi
                    ;;
                *.zip)
                    if [ -d $2 ]; then
                        unzip $1 -d $2
                    else
                        mkdir -p $2
                        unzip $1 -d $2
                    fi
                    ;;
                *.Z)
                    uncompress $1
                    ;;
                *.7z)
                    if [ -d $2 ]; then
                        7z x $1 -o $2
                    else
                        mkdir -p $2
                        7z x $1 -o $2
                    fi
                    ;;
                *)
                    echo "'$1' cannot be extracted via extract"
                    return 1
                    ;;
            esac
        else
            echo "'$1' is not a valid file"
            return 2
        fi
    fi
}
export -f extract

exesudo function:

Allows run shell functions with sudo

#!/usr/bin/env bash
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# EXESUDO
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
#
# Purpose:
# -------------------------------------------------------------------- #
# Execute a function with sudo
#
# Params:
# -------------------------------------------------------------------- #
# $1:   string: name of the function to be executed with sudo
#
# Usage:
# -------------------------------------------------------------------- #
# exesudo "funcname" followed by any param
#
# -------------------------------------------------------------------- #
# Created 01 September 2012              Last Modified 02 September 2012

function exesudo ()
{
    ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##
    #
    # LOCAL VARIABLES:
    #
    ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##

    #
    # I use underscores to remember it's been passed
    local _funcname_="$1"

    local params=( "$@" )               ## array containing all params passed here
    local tmpfile="/dev/shm/$RANDOM"    ## temporary file
    local filecontent                   ## content of the temporary file
    local regex                         ## regular expression
    local func                          ## function source


    ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##
    #
    # MAIN CODE:
    #
    ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##

    #
    # WORKING ON PARAMS:
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    #
    # Shift the first param (which is the name of the function)
    unset params[0]              ## remove first element
    # params=( "${params[@]}" )     ## repack array


    #
    # WORKING ON THE TEMPORARY FILE:
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    content="#!/bin/bash\n\n"

    #
    # Write the params array
    content="${content}params=(\n"

    regex="\s+"
    for param in "${params[@]}"
    do
        if [[ "$param" =~ $regex ]]
            then
                content="${content}\t\"${param}\"\n"
            else
                content="${content}\t${param}\n"
        fi
    done

    content="$content)\n"
    echo -e "$content" > "$tmpfile"

    #
    # Append the function source
    echo "#$( type "$_funcname_" )" >> "$tmpfile"

    #
    # Append the call to the function
    echo -e "\n$_funcname_ \"\${params[@]}\"\n" >> "$tmpfile"


    #
    # DONE: EXECUTE THE TEMPORARY FILE WITH SUDO
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    sudo bash "$tmpfile"
    rm "$tmpfile"
}
export -f exesudo
@efargas
Copy link
Author

efargas commented Nov 4, 2015

both can be added to /etc/profile.d/ script or .bashrc to source it on every login

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant