scripts

My personal collection of scripts
git clone git://src.gearsix.net/scripts
Log | Files | Refs | Atom | README | LICENSE

commit 8fc83aef67a991384c4ac33e0916883bb8819399
parent 5461a69217d241eeac4f648ba4d5d702ad5f1102
Author: GeaRSiX <gearsix@tuta.io>
Date:   Wed, 15 Jan 2020 16:35:09 +0000

added install script;
moved scripts to src/;
amend to doc in scripts so info in install works;

Diffstat:
Dcpc.sh | 9---------
Ddepreciated/touchn.sh | 18------------------
Dflac2mp3.sh | 13-------------
Dgfind.sh | 8--------
Ainstall | 134+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dmkdcp.sh | 17-----------------
Dmkdmv.sh | 17-----------------
Dmkdnvim.sh | 12------------
Dopenurl.sh | 12------------
Dshh.sh | 12------------
Asrc/cpc.sh | 9+++++++++
Asrc/depreciated/touchn.sh | 17+++++++++++++++++
Asrc/flac2mp3.sh | 10++++++++++
Asrc/gfind.sh | 7+++++++
Asrc/mkdcp.sh | 16++++++++++++++++
Asrc/mkdmv.sh | 16++++++++++++++++
Asrc/mkdnvim.sh | 10++++++++++
Asrc/openurl.sh | 11+++++++++++
Asrc/shh.sh | 9+++++++++
Asrc/strindex.sh | 17+++++++++++++++++
Asrc/strlen.sh | 21+++++++++++++++++++++
Asrc/xopen.sh | 26++++++++++++++++++++++++++
Dstrindex.sh | 17-----------------
Dstrlen.sh | 22----------------------
Dxopen.sh | 21---------------------
25 files changed, 303 insertions(+), 178 deletions(-)

diff --git a/cpc.sh b/cpc.sh @@ -1,9 +0,0 @@ -#!/bin/bash -# cpc (cp contents) -# copies the contents of file $1 to file $2 -# -# e.g.$ cpc file1.txt file2.txt - -touch $2 -echo "$(cat $1)" > $2 - diff --git a/depreciated/touchn.sh b/depreciated/touchn.sh @@ -1,18 +0,0 @@ -#DEPREICATED -# found out you could just use "touch ./file{1..13}" - -#!/bin/bash -# ~/scripts -# create $2 files name $1 appended with 1 to $2 -# e.g$ touchn "song" 13 # creates song1...song13 in curr_dir - -# if no $2 -if [ -z $2 ]; then - echo "ERROR! touchn: Specify number of files to touch in \$2"; - return 1; -fi - -for i in $(seq 0 $2); do - touchn "$1$i"; -done - diff --git a/flac2mp3.sh b/flac2mp3.sh @@ -1,13 +0,0 @@ -#!/bin/bash -# flac2mp3 -# converts .flac files ($1...$n) to .mp3 files -# -# DEPENDENCIES: -# ffmpeg -# -# e.g.$ flac2mp3 ./album - -for file in $1/*.flac; do - ffmpeg -i "$file" -qscale:a 0 "${file[@]/%flac/mp3}" -done - diff --git a/gfind.sh b/gfind.sh @@ -1,8 +0,0 @@ -#!/bin/bash -# gfind (grep find) -# find files containing string $2 in directory/file $1 -# -# e.g.$ gfind dogs/ "shades" - -grep -rnw $1 -e $2 #recursive, print line #, match whole word - diff --git a/install b/install @@ -0,0 +1,134 @@ +#!/bin/bash +#=========== +# g6scripts +#=========== +# file: install +# created: 2020-01-15 +# updated: 2020-01-16 +# description: loops through files in ./src & installs them to configured destination + +#--------- +# GLOBALS +#--------- +ln=0 +dir=0 +all=0 +destination=/usr/bin # default install directory +source=./src/*.sh +depreciated_src=./src/depreciated/*.sh + +#----------- +# FUNCTIONS +#----------- +## print_help = prints the help dialog and exits +print_help () { + echo "usage: ./install [ARGUMENTS]" + echo "" + echo "ARGUMENTS" + echo "-d, --dest pre-define the destination to install the scripts to" + echo "-a, --all install ALL the scripts, will answer \"y\" automatically" + echo "-l, --link will \"ln -s\" the script (not copy it), note if location of source changes then links will break" + echo "-h, --help print this dialog" +} +## install_yn = installs $1 to $2, prints $3 if user asks for "info" +install_yn () { + src=$1 # path of the file to install + dest=$2 # path to install the file to + depr=$3 # if installing a depreciated script or not + + ask=0 + while [[ ask -eq 0 ]]; do + printf "install %s [yes/no/info]? " $src + if [[ $all -eq 0 || $depr -eq 1 ]]; then + read yn + else + yn="y" + echo "y" + fi + + if [[ $yn == "y" || $yn == "Y" || $yn == "yes" ]]; then + if [[ $ln -eq 0 ]]; then + echo "install $src -> $dest" + install -pDm755 $src $dest + else # ln the file + echo "ln -s $src -> $dest" + chmod +x $src + mkdir -p $(dirname $dest) + ln -sf $(readlink -f $src) $dest + fi + ask=1 + elif [[ $yn == "n" || $yn == "N" || $yn == "no" ]]; then + echo "skipping..." + ask=1 + elif [[ $yn == "i" || $yn == "I" || $yn == "info" ]]; then + cat $script | grep "description\|DEPENDENCIES\|ARGUMENTS\|DEPRECIATED" + fi + done +} + +#------ +# MAIN +#------ +## parse args +while [[ $# -gt 0 ]]; do + case $1 in + -l|--link) + ln=1 + echo "will only create symolic links to scripts, this will make the scripts in ./src/ executable" + shift + ;; + -d|--dest) + dir=1 + destination=$2 + shift + shift + ;; + -a|--all) + all=1 + echo "installed ALL scripts (not including depreciated)" + shift + ;; + *) # invalid arg found, print help & exit + print_help + exit + ;; + esac +done +echo "===========" +echo " g6scripts " +echo "===========" +echo "" +echo "installation" +echo "------------" +## check install destination +if [[ dir -eq 0 ]]; then + printf "install destination (default = $destination): " + read destination +else + echo "install destination = $destination" +fi +## install scripts from source +for script in $source; do + if [[ $script != "src/depreciated" ]]; then # skip depreciated + install_yn $script ${destination//"~"/$HOME}/$(basename $script .sh) + fi +done +## install depreciated scripts +ask=0 +while [[ ask -eq 0 ]]; do + printf "install depreciated scripts (default = no) [yes/no/info]? " + read yn + if [[ $yn == "y" || $yn == "Y" || $yn == "yes" ]]; then + for script in $depreciated_src; do + install_yn $script ${destination//"~"/$HOME}/$(basename $script .sh) 1 + done + ask=1 + elif [[ $yn == "n" || $yn == "N" || $yn == "no" ]]; then + ask=1 + elif [[ $yn == "i" || $yn == "I" || $yn == "info" ]]; then + echo "depreciated scripts have been found redundant, details are provided in the \"info\" for each script" + fi +done +## end +echo "done" +echo "make sure $destination is in your \$PATH and you should be able to call any of these scripts from the terminal" diff --git a/mkdcp.sh b/mkdcp.sh @@ -1,17 +0,0 @@ -#!/bin/bash -# mkdcp (mkdir&cp) -# make a directory and copy files into it -# -# e.g.$ mkdcp ~/new-dir /tmp/file* - -#mkdir $1 -dir=$1; -mkdir -p $dir -shift; - -#cp files into $1 -for file in $@; do - cp $file $dir; - shift; -done - diff --git a/mkdmv.sh b/mkdmv.sh @@ -1,17 +0,0 @@ -#!/bin/bash -# mkdmv (mkdir&mv) -# make directory $1 and move files ($2...$n) into it -# -# e.g.$ mkdmv ~/new-dir ~/var/tmp/file* - -#mkdir $1 -dir=$1; -mkdir -p $dir -shift; - -#cp files ($2...$n) into $1 -for file in $@; do - mv $file $dir; - shift; -done - diff --git a/mkdnvim.sh b/mkdnvim.sh @@ -1,12 +0,0 @@ -#!/bin/bash -# mkdnvim (mkdir&nvim) -# Makes directory $1 and opens file $2 using nvim -# -# mkdnvim <new_dir> <filename> -# e.g.$ mkdnvim ~/new file #creates ~/new/file and nvim into it - -mkdir $1; -cd $1; -touch $2; -nvim $2; - diff --git a/openurl.sh b/openurl.sh @@ -1,12 +0,0 @@ -#!/bin/bash -# openurl -# script for opening .url filetype -# -# e.g.$ openurl link.url -# -# SEE: http://www.danielbrice.net/blog/opening-url-file-like-a-pro/ - -URL=$(cat "$1" | grep "URL=" | cut -d= -f2) -echo -e "xdg-open $URL" -xdg-open "$URL" &> /dev/null - diff --git a/shh.sh b/shh.sh @@ -1,12 +0,0 @@ -#!/bin/bash -# shh -# run a terminal command and send stdout to /dev/null -# -# shh cmd -# -# @TODO autocomplete for $PATH files? - -for bin in $@; do - $bin &>/dev/null & -done; - diff --git a/src/cpc.sh b/src/cpc.sh @@ -0,0 +1,9 @@ +#!/bin/bash +# cpc (cp contents) +# description: copies the contents of file $1 to file $2 +# +# e.g.$ cpc file1.txt file2.txt + +touch $2 +echo "$(cat $1)" > $2 + diff --git a/src/depreciated/touchn.sh b/src/depreciated/touchn.sh @@ -0,0 +1,17 @@ +# DEPRECIATED: found out you could just use "touch ./file{0..13}" + +#!/bin/bash +# ~/scripts +# description: create $2 files name $1 appended with 1 to $2 +# e.g$ touchn "song" 13 # creates song1...song13 in curr_dir + +# if no $2 +if [ -z $2 ]; then + echo "ERROR! touchn: Specify number of files to touch in \$2"; + return 1; +fi + +for i in $(seq 0 $2); do + touchn "$1$i"; +done + diff --git a/src/flac2mp3.sh b/src/flac2mp3.sh @@ -0,0 +1,10 @@ +#!/bin/bash +# flac2mp3 +# description: converts .flac files ($1...$n) to .mp3 files +# e.g.$ flac2mp3 ./album +# DEPENDENCIES: ffmpeg + +for file in $1/*.flac; do + ffmpeg -i "$file" -qscale:a 0 "${file[@]/%flac/mp3}" +done + diff --git a/src/gfind.sh b/src/gfind.sh @@ -0,0 +1,7 @@ +#!/bin/bash +# gfind (grep find) +# description: find files containing string $2 in directory/file $1 +# e.g.$ gfind dogs/ "shades" + +grep -rnw $1 -e $2 #recursive, print line #, match whole word + diff --git a/src/mkdcp.sh b/src/mkdcp.sh @@ -0,0 +1,16 @@ +#!/bin/bash +# mkdcp (mkdir&cp) +# description: make a directory and copy files into it +# e.g.$ mkdcp ~/new-dir /tmp/file* + +#mkdir $1 +dir=$1; +mkdir -p $dir +shift; + +#cp files into $1 +for file in $@; do + cp $file $dir; + shift; +done + diff --git a/src/mkdmv.sh b/src/mkdmv.sh @@ -0,0 +1,16 @@ +#!/bin/bash +# mkdmv (mkdir & mv) +# description: mkdir $1 && mv ($2...$n) 1to it +# e.g.$ mkdmv ~/new-dir ~/var/tmp/file* + +#mkdir $1 +dir=$1; +mkdir -p $dir +shift; + +#cp files ($2...$n) into $1 +for file in $@; do + mv $file $dir; + shift; +done + diff --git a/src/mkdnvim.sh b/src/mkdnvim.sh @@ -0,0 +1,10 @@ +#!/bin/bash +# mkdnvim (mkdir&nvim) +# description: makes directory $1 and opens file $2 using nvim +# e.g.$ mkdnvim ~/new file #creates ~/new/file and nvim into it + +mkdir $1; +cd $1; +touch $2; +nvim $2; + diff --git a/src/openurl.sh b/src/openurl.sh @@ -0,0 +1,11 @@ +#!/bin/bash +# openurl +# description: script for opening .url filetype +# e.g.$ openurl link.url +# SEE: http://www.danielbrice.net/blog/opening-url-file-like-a-pro/ +# NOTE: see http://www.danielbrice.net/blog/opening-url-file-like-a-pro/ for .url spec + +URL=$(cat "$1" | grep "URL=" | cut -d= -f2) +echo -e "xdg-open $URL" +xdg-open "$URL" &> /dev/null + diff --git a/src/shh.sh b/src/shh.sh @@ -0,0 +1,9 @@ +#!/bin/bash +# shh = quiet for commands that don't include it +# description: run a terminal command ($@) and send stdout to /dev/null +# e.g.$ shh chromium & + +for bin in $@; do + $bin &>/dev/null +done; + diff --git a/src/strindex.sh b/src/strindex.sh @@ -0,0 +1,17 @@ +#!/bin/bash +# strindex (string index) +# description: finds the starting index of substring ($1) in a string ($2) +# e.g.$ strindex "cat" "the cat sat on the mat" #returns 4 +# +# NOTES: index returned ranges from 0 - (string length) + +#swaps argument (strindex "cat on mat" "mat") +if [[ -z $3 && $3 == "-r" ]]; then + x="${1%%$2*}" + [[ "$x" = "$1" ]] && echo "ERROR! strindex(): Could not find $2 in $1" || echo "${#x}" +#standard usage (see e.g.$) +else + x="${2%%$1*}" + [[ "$x" = "$2" ]] && echo "ERROR! strindex(): could not find $1 in $2" || echo "${#x}" +fi + diff --git a/src/strlen.sh b/src/strlen.sh @@ -0,0 +1,21 @@ +#!/bin/bash +# strlen (string length) +# description: echo's the length of a string ($1) +# e.g.$ strlen "ma di-" +# +# SEE: https://stackoverflow.com/questions/17368067/length-of-string-in-bash + +strU8DiffLen () { + local bytlen oLang=$LANG oLcAll=$LC_ALL + LANG=C LC_ALL=C + bytlen=${#1} + LANG=$oLang LC_ALL=$oLcAll + return $(( bytlen - ${#1} )) +} + +for string in $1; do + strU8DiffLen "$string" + printf " - %-$((14+$?))s is %2d chars length, but use %2d bytes\n" \ + "'$string'" ${#string} $((${#string}+$?)) +done + diff --git a/src/xopen.sh b/src/xopen.sh @@ -0,0 +1,26 @@ +#!/bin/bash +# xopen = xdg-open FILE +# description: open file in xserver (doesn't support multiple files at once yet) +# ARGUMENTS: -q, --quiet send stdout to /dev/null + +# variables +quiet=0 +args=() + +# parse args +while [[ $# -gt 0 ]]; do + case $1 in + -q|--quiet) + quiet=1 + shift + ;; + esac +done + +# main +args=( "$@" ) +if [[ $quiet -eq 0 ]]; then + xdg-open $args +else + xdg-open $args &>/dev/null +fi diff --git a/strindex.sh b/strindex.sh @@ -1,17 +0,0 @@ -#!/bin/bash -# strindex (string index) -# finds the starting index of a substring in a string -# index returned ranges from 0 - (string length) -# -# e.g.$ strindex "cat" "the cat sat on the mat" #returns 4 - -#swaps argument (strindex "cat on mat" "mat") -if [[ -z $3 && $3 == "-r" ]]; then - x="${1%%$2*}" - [[ "$x" = "$1" ]] && echo "ERROR! strindex(): Could not find $2 in $1" || echo "${#x}" -#standard usage (see e.g.$) -else - x="${2%%$1*}" - [[ "$x" = "$2" ]] && echo "ERROR! strindex(): could not find $1 in $2" || echo "${#x}" -fi - diff --git a/strlen.sh b/strlen.sh @@ -1,22 +0,0 @@ -#!/bin/bash -# strlen (string length) -# echo's the length of a string -# -# e.g.$ strlen "ma di-" -# -# SEE: https://stackoverflow.com/questions/17368067/length-of-string-in-bash - -strU8DiffLen () { - local bytlen oLang=$LANG oLcAll=$LC_ALL - LANG=C LC_ALL=C - bytlen=${#1} - LANG=$oLang LC_ALL=$oLcAll - return $(( bytlen - ${#1} )) -} - -for string in $1; do - strU8DiffLen "$string" - printf " - %-$((14+$?))s is %2d chars length, but use %2d bytes\n" \ - "'$string'" ${#string} $((${#string}+$?)) -done - diff --git a/xopen.sh b/xopen.sh @@ -1,21 +0,0 @@ -#!/bin/bash -# xdg-open -# open file in xserver (doesn't support multiple files at once) -# -# xopen file [-shh, -shhh] -# -shh, -shhh # send stdout to /dev/null -# -# $lazymode=xo - -for last_arg; do true; done; -# check for shhh -if [[ $last_arg == "-shh" || $last_arg == "-shhh" ]]; then - args=( "$@" ) # get all args - unset "args[${#args[@]}-1]" # remove last arg - - # all remaining args = files to open (shh mode) - xdg-open "$args" &>/dev/null -#standard -else - xdg-open "$@" -fi