Skip to content

Latest commit

 

History

History
268 lines (196 loc) · 5 KB

sh_functions.org

File metadata and controls

268 lines (196 loc) · 5 KB

Shell function to make my life easier

Introduction

Set of functions to simplyfy my life

Adjust brightness

xrandr –output eDP-1 –brightness 0.5

(don’t set this to zero!!)

Shell function

Proxy

Set proxy automatically depending on hostname..

function set_custom_proxy() {
    export http_proxy='http://proxy.ichr.uwa.edu.au:8081'
    export https_proxy='https://proxy.ichr.uwa.edu.au:8081'
}
function unset_custom_proxy() {
    export http_proxy=
    export https_proxy=
}

function proxy() {
    OPTION=$1

    if [ -n "$OPTION" ];
    then
        case $OPTION in
            stop)
                
                echo "unsetting proxy!"
                unset_custom_proxy
                echo "Done."
                ;;
            *)
                echo "Sorry, I don't understand"
                ;;
        esac
    else
        HOSTNAME=$(hostname)
        
        case $HOSTNAME in
            work)
                echo "at work!"
                set_custom_proxy
                ;;
            SeqNextGen)
                echo "at seqnextgen"
                set_custom_proxy
                ;;
            devel)
                echo "at devel"
                set_custom_proxy
                ;;

            *)
                echo "Sorry, hostname not recognized"
                ;;
        esac        
        echo "Done."
    fi
}

Clean tex

function cleantex(){
    arg=${1:-.}
    exts=("aux" "fls" "bbl" "blg" "brf" "idx" "ilg" "ind" "lof" "log" "lol" "lot" "out" "toc" "synctex.gz" "run.xml" "fdb_latexmk")
    if [ -d $arg ]; then
        for ext in $exts; do
            rm -f $arg/*.$ext
        done
    else
        for ext in $exts; do
            rm -f $arg.$ext
        done
    fi
}

Backup script

Been using this for ages.

    #!/usr/bin/env bash

    #
    # Set parameters below to fit your system
    #

    read_secret()
    {
        stty -echo
        trap 'stty echo' EXIT
        read "$@"
        stty echo
        trap - EXIT
        echo
    }




    INLIST=
    PASSWD=
    function usage()
    {
cat <<EOF
usage: $0  -i <input file list>
EOF
    exit 1;
    }

    while getopts i: opt
    do
    case ${opt} in
    i) INLIST=${OPTARG};;
    *) usage;;
    esac
    done

    if [ "${INLIST}" = "" ]; then usage; fi

    printf "Pwd: "
    read_secret PASSWD
    printf "again: " 
    read_secret PASSWD2

    if [ "$PASSWD" != "$PASSWD2" ]
    then
        printf "\nERROR: passwords do not match!\n\n" ;
        exit 1;
    fi

    if [ "${PASSWD}" = "" ]; then usage; fi

    #
    #   Sanity check 
    #

    programs=(scp tar sha512sum gpg2) 

    printf "Running Sanity checks:\n";

    for item in ${programs[*]}
    do   
        if which $item >/dev/null; then
      printf "%15s found...\n"  $item;
        else
      printf "\nERROR: %s not found!\n\n" $item;
      exit 1;
        fi
    done

    DATE=$(date +%d%m%y)

    OUTFILENAME=$HOME"/backup/archive"$DATE".tar.gz"

    OGPG=$OUTFILENAME".gpg"
    OSHA=$HOME"/backup/SHA256"


    if [ -f $OGPG ]
    then
        printf "\nERROR: the file %s exists!\n\n" $OGPG;
        usage;
    fi

    if [ ! -f $INLIST ]
    then
        printf "\nERROR: the file %s does not exist!\n\n" $INLIST;
        usage;
    fi


    printf "Creating archive: \n";

    printf "%30s\n" $OUTFILENAME ;

    cd
    tar -czf $OUTFILENAME --files-from $INLIST

    if [ "$?" -eq "0" ]
    then
        printf "%30s\n" "Success";
    else
        printf "\nERROR: tar FAILED!\n\n";
        
        usage;
    fi

        


    printf "Encrypting: \n";
    printf "%30s\n" $OGPG ;

    echo $PASSWD | gpg2 --batch --passphrase-fd 0 --symmetric --s2k-cipher-algo AES256 --s2k-mode 3 --s2k-count 65000000 -o $OGPG $OUTFILENAME

    if [ "$?" -eq "0" ]
    then
        printf "%30s\n" "Success";
    else
        printf "\nERROR: gpg FAILED!\n\n";
        usage;
    fi

    #
    # delete unencrypted tar archive... 
    #

    rm $OUTFILENAME;



    printf "sha512: \n";
    printf "%30s\n" $OSHA;

    TARGET=$HOME"/backup"

    cd $TARGET 

    sha512sum --tag  $OGPG >> $OSHA  
    if [ "$?" -eq "0" ]
    then
        printf "%30s\n" "Success";
    else
        printf "\nERROR: sha512sum FAILED!\n\n";
        usage;
    fi

    chmod 600 $OGPG 
    chmod 600 $OSHA

End.