Skip to content

Instantly share code, notes, and snippets.

@quantumJLBass
Last active December 23, 2015 04:39
Show Gist options
  • Save quantumJLBass/6581582 to your computer and use it in GitHub Desktop.
Save quantumJLBass/6581582 to your computer and use it in GitHub Desktop.
snippets for a command line bash.

General Bash snips

Test internet connectivity

#check to see if there is network connection
has_network(){
    # Capture a basic ping result to Google's primary DNS server to determine if
    # outside access is available to us. If this does not reply after 2 attempts,
    # we try one of Level3's DNS servers as well. If neither of these IPs replies to
    # a ping, then we'll skip a few things further in provisioning rather than
    # creating a bunch of errors.
    
    ping_result=`ping -c 2 8.8.4.4 2>&1`
    if [[ $ping_result != *bytes?from* ]]
    then
        ping_result=`ping -c 2 4.2.2.2 2>&1`
    fi
  [[ $ping_result == *bytes?from* ]] && return 1 || return 0
}

##Cloneing GitHub Repos## ###Managed single GITHUB clone###

install_repo(){
    if [ $2 ]
    then
        echo "just 1"
        git clone $1 -q
    else
        echo "just 1 and 2"
        git clone $1 $2 -q
    fi
    success=$?
    if [[ $success -eq 0 ]];
    then
        echo "Repository successfully cloned."
        echo "cleaning"
        cd $r/
        rm -rf LICENSE.txt STATUS.txt README.md RELEASE_NOTES.txt modman
        cd ../
        cp -af $r/* .
        rm -rf $r/
        
        #eval $3
        php "/srv/www/scripts/mage/install-invoke-app.php"
        
        #if [ -z "$3" ]
        #then
        #    echo "no callback"
        #else
        #    eval $3
        #fi
    else
        echo "Something went wrong!"
    fi
    sleep 1 # slow it down to insure that we have the items put in place.
}

Managed GITHUB clone list (depends on the single function)

#declare -A list = ( [repo]=gitUser )
install_repolist(){
    gitRepos=$1
    for r in "${!gitRepos[@]}" #loop with key as the var
    do
        giturl="git://github.com/${gitRepos[$r]}/$r.git"
        echo "Adding $r From $giturl"
        if [ -z "$r" ];
        then
            echo
        else
            install_repo $giturl $2 $3
        fi
        echo
    done
    return 1
}

###Example Usage###

declare -A gitRepos
    #[repo]=gitUser
    gitRepos=(
        [wsu_admin_base]=jeremyBass
        [wsu_base_theme]=jeremyBass
        [Storeutilities]=jeremyBass
        [webmastertools]=jeremyBass 
        [ldap]=jeremyBass
    )
    cd /srv/www/mage/                   #rebase before cloning
    install_repolist $gitRepos 0 " php /srv/www/scripts/mage/install-invoke-app.php"
    unset gitRepos                      #unset and re-declare to clear associative arrays
    declare -A gitRepos
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment