From 901270767685a7f7639c919d59ab67e3fb0461ba Mon Sep 17 00:00:00 2001 From: yaswant Date: Fri, 21 Feb 2020 11:55:32 +0000 Subject: [PATCH] minor update and few new scripts --- benchmark.sh | 2 +- desktop-app-generator | 162 ++++++++++++++++++++++++++++++++++++++++++ doy | 27 ++++--- files | 10 +-- restore | 71 ++++++++++-------- rmempty | 94 ++++++++++++++++-------- tarz | 4 +- uptime-checker.sh | 8 +-- wl2wn | 10 +++ wn2wl | 10 +++ xd | 15 ++++ 11 files changed, 332 insertions(+), 81 deletions(-) create mode 100755 desktop-app-generator create mode 100755 wl2wn create mode 100755 wn2wl create mode 100755 xd diff --git a/benchmark.sh b/benchmark.sh index d332a56..7e8010c 100755 --- a/benchmark.sh +++ b/benchmark.sh @@ -13,7 +13,7 @@ cat<&2; exit 1; } +eval set -- "$OPTS" + +version(){ +cat< + +EOF +} + +while true; do + case "$1" in + -h |--help ) usage; exit 0 ;; + -v |--version ) version; exit 0 ;; + -- ) shift; break ;; + * ) break ;; + esac +done +# ---------------------------------------------------------------------------- + + +command -v yad 1>/dev/null || { echo "yad: command not found" && exit 127; } + +# \nSee https://developer.gnome.org/desktop-entry-spec/" --show-uri + # --text="Edit fields below to create and install a custom desktop launcher\nhttps://developer.gnome.org/desktop-entry-spec" \ + +# collect form data +data=$(yad --title="Desktop Entry Generator $vsn" --width=450 --posx=10 --posy=10 \ + --text-align=center --show-uri\ + --form --align=right \ + --field=" Filename" "newapp.desktop" \ + --field=" Type:RO" "Application" \ + --field=" Name" "LauncherName" \ + --field=" Version" "1.0" \ + --field=" Comment" "Generate and install desktop application launcher" \ + --field=" Command" "$HOME/bin/" \ + --field=" Optional Args:CB" "!%f!%F!%u!%U!%c!%k" \ + --field=" Icon:FL" "/usr/share/icons/hicolor/48x48/apps/system-logo-icon.png" \ + --field=" Categories:CBE" "Utility!AudioVideo!Development!Education!Game!Graphics!Network!Office!Science!Settings!System" \ + --field=" In terminal:CHK" FALSE \ + --field=" Install for user: ${USER}:CHK" FALSE) +ret=$? +[[ "$ret" -eq 1 || "$ret" -eq 252 ]] && { echo "Cancelled"; exit $ret; } + + +# retrieve fields array +IFS='|' read -ra fields <<< "$data" + + +# validate command +exe="${fields[5]%% *}" +if command -v "$exe" &>/dev/null; then : +else + yad --image dialog-error --title 'Error' \ + --text " ${exe}: command not found\n\nPlease try again and input a valid Command" \ + --width=450 --posx=10 --posy=10 \ + --button=gtk-cancel:127 + exit $? +fi + + +# parse application file path +if [[ ${fields[10]} = TRUE ]]; then + filepath="${HOME}/.local/share/applications/${fields[0]}" + mkdir -p "${filepath%/*}" +else + filepath="${fields[0]}" +fi + +# prompt overwrite +overwrite=0 +if [ -f "$filepath" ]; then + yad --image dialog-question \ + --title Overwite --width=450 --posx=10 --posy=10 \ + --button=gtk-no:1 \ + --button=gtk-yes:0 \ + --text " ${filepath##*/} exists\n\n Overwrite file?" + overwrite=$? +fi + +# write desktop entry file +(( overwrite == 0 )) && \ +cat > "$filepath" <> Delete these file$s? (y|n): \c"; read -r x read -rp ">> Delete these file$s? (y|n): " x - [[ $x = 'y' ]] && rm $(cut -f 2 $LOG) && echo -e " File$s deleted." #m1 + [[ $x = 'y' ]] && rm $(cut -f 2 "$LOG") && echo -e " File$s deleted." #m1 # [[ $x = 'y' ]] && rm $(cat $LOG |tr '\t' ',' |cut -d',' -f2) #m2 # [[ $x = 'y' ]] && find $LOPT "$SPATH" $OPT $DEL #m3 fi # Clean-up (Remove log file) -------------------------------------------------- -[ -f $LOG ] && [ -z "$KEEP" ] && rm $LOG +[ -z "$KEEP" ] && rm -f "$LOG" diff --git a/restore b/restore index dc6a331..3f865a9 100755 --- a/restore +++ b/restore @@ -1,49 +1,60 @@ #!/bin/bash # syntax: -# restore filepath [snapshot] +# restore [file|dirname] [snapshot] # -# restore files/dirs from snapshots (Limited to data in $HOME only) +# restore files/directories from snapshots (limited to data in $HOME only) # yaswant.pradhan 2014/15 # ----------------------------------------------------------------------------- -rootdir=$(readlink -f $HOME) -SNAPSHOTDIR=$rootdir/.snapshot -rspoint=${2:-'hourly.0'} +rootdir=$(readlink -f "$HOME") +SNAPSHOTDIR=${rootdir}/.snapshot +LAST="$(/bin/ls -t1 "$SNAPSHOTDIR" | head -n 1)" # most recent restore point +recent=${2:-$LAST} # parse recent rsp argument +snapshots=($(/bin/ls -t1 "$SNAPSHOTDIR")) # all available snapshots -function line(){ for i in {1..80}; do echo -e '-\c'; done; echo ""; } -function usage() -{ +list_all(){ more << EOF -$(basename $0) file[dir]name [snapshot] ---------------------- - Available snapshots ---------------------- -$(ls -lt $SNAPSHOTDIR |tail -n+2 |\ - awk '{printf"%10s @%6s on %s-%s\n", $9,$8,$7,$6}') +--------------------------------------------- + Available snapshots +--------------------------------------------- +$(/bin/ls -lt "$SNAPSHOTDIR" | tail -n+2 | nl | \ + awk '{printf"%2s %-22s @%6s on %s-%s\n", $1, $10,$9,$8,$7}') +--------------------------------------------- + EOF } +usage(){ echo "usage: ${0##*/} file|dirname [snapshot]"; list_all; } # ----------------------------------------------------------------------------- [[ "$#" -lt 1 ]] && usage && exit -# Parse file path -fpath=$(readlink -f $1) # Full paths of File/Directory to be restored + +# -- parse file path +fpath=$(readlink -f "$1") # Full paths of File/Directory to be restored rpath=${fpath#$rootdir/} # Path relative to $HOME -if [ ! -e "$SNAPSHOTDIR/$rspoint" ]; then - echo "** $SNAPSHOTDIR/$rspoint doesnot not exist **" - echo "Select a snapshot directory to restore:" - line; ls -t $SNAPSHOTDIR && line || exit $? - read rspoint +if [ -e "${SNAPSHOTDIR}/${recent}" ]; then + list_all; + snapfile="${SNAPSHOTDIR}/${recent}/${rpath}" +else + echo -e "** ERROR: ${SNAPSHOTDIR}/${recent} doesn't not exist **\n" + list_all; + + read -rp "Please select a snapshot 'id' to restore ('0' to exit): " _id + (( _id == 0 )) && { echo "Aborted."; exit; } + (( _id-- )) + snapfile="${SNAPSHOTDIR}/${snapshots[$_id]}/${rpath}" fi -snapfile="$SNAPSHOTDIR/$rspoint/$rpath" -if [ -e "$snapfile" ]; then echo "Restoring from $rspoint" -else - echo "$rpath not found in $rspoint" - exit 1 -fi +# -- check existence of file in snapshot +[ -e "$snapfile" ] || { echo "$snapfile: No such file or directory"; exit 2; } + + +# -- begin restore +echo "Confirm restore from: $snapfile" +# echo "[command] rsync -aiLt --delete $snapfile $(dirname $fpath)" +read -rp "** Once restored, this cannot be undone. Continue y|(n)? " ans -echo "[command] rsync -avi --delete $snapfile $(dirname $fpath)" -read -ep "WARN! Once restored, this cannot be reverted - Do you want to continue? y|[n]: " yn -[[ "$yn" == 'y' ]] && rsync -avi --delete $snapfile $(dirname $fpath) +[[ $(tr '[:lower:]' '[:upper:]' <<<"$ans") == 'Y' ]] \ +&& rsync -ait --delete "$snapfile" "${fpath%/*}" \ +|| echo "Aborted." diff --git a/rmempty b/rmempty index c58efcd..8cde95b 100755 --- a/rmempty +++ b/rmempty @@ -32,28 +32,50 @@ EOF } usage(){ cat< is not present - -V, --verb switch to verbose mode i.e. list deleted files - -x, --broken-link search and remove broken links only - -v, --version print version and exit + -h, --help print this help and exit -Report bugs to + -i, --ignore pattern do not delete empty files matching pattern, which may + be needed (eg __init__.py) + + -l, --levels n descend to n levels (non negative integer) + + -n, --dry-run dry run or Print-only mode. This is used as default if + is not present + + -v, --verbose switch to verbose mode i.e. list deleted files + + -V, --version print version and exit + + -x, --broken-link search and remove broken links only + +Report bugs to + EOF } @@ -66,33 +88,47 @@ TAG='files and directories' while [[ $1 == -* ]]; do case $1 in -atime) - OPT="$OPT -atime $2"; shift 2 ;; + OPT="$OPT -atime $2"; shift 2 + ;; -ctime) - OPT="$OPT -ctime $2"; shift 2 ;; + OPT="$OPT -ctime $2"; shift 2 + ;; -daystart) - OPT="$OPT -daystart"; shift 1 ;; + OPT="$OPT -daystart"; shift 1 + ;; -d|--dir) - TAG='empty directories'; OPT="$OPT -type d"; shift 1 ;; + TAG='empty directories'; OPT="$OPT -type d"; shift 1 + ;; -f|--file) - TAG='empty files'; OPT="$OPT -type f"; shift 1 ;; + TAG='empty files'; OPT="$OPT -type f"; shift 1 + ;; + -h|--help) + usage; exit 0 + ;; -i|--ignore) - OPT="$OPT ! -name $2"; shift 2 ;; + OPT="$OPT ! -name $2"; shift 2 + ;; -l|--level) - LEV=" -maxdepth $2"; shift 2 ;; + LEV=" -maxdepth $2"; shift 2 + ;; -n|--dry-run) - DRY_RUN=1; shift 1 ;; + DRY_RUN=1; shift 1 + ;; -mtime) - OPT="$OPT -mtime $2"; shift 2 ;; - -V|--verb) - OPT2="$OPT2 -print"; shift 1 ;; - -v|--version) - version; exit 0 ;; + OPT="$OPT -mtime $2"; shift 2 + ;; + -v|--verbose) + OPT2="$OPT2 -print"; shift 1 + ;; + -V|--version) + version; exit 0 + ;; -x|--broken-link) - TAG='broken links'; OPT="$OPT -xtype l"; shift 1 ;; + TAG='broken links'; OPT="$OPT -xtype l"; shift 1 + ;; --) - shift; break ;; - -h|--help) - usage; exit 0 ;; + shift; break + ;; -*) echo "invalid option: $1" 1>&2; usage; exit 1;; esac done diff --git a/tarz b/tarz index b8663ea..b47014b 100755 --- a/tarz +++ b/tarz @@ -18,8 +18,8 @@ NDASH=20 # number of progress indicators # Add progress indicator # tar cf $arch_name -I /usr/bin/pigz --checkpoint=.1000 --totals $1 - -BLOCKS=$(du -sB $BLOCKSIZE --apparent-size "$dir_name" | cut -f 1) +# echo "$dir_name" +BLOCKS=$(du -sB $BLOCKSIZE --apparent-size "$1" | cut -f 1) CHECKPOINT=$(( BLOCKS/NDASH )) printf 'Num Blocks: %s\t' "$BLOCKS" diff --git a/uptime-checker.sh b/uptime-checker.sh index 1982f38..0c9c9e6 100755 --- a/uptime-checker.sh +++ b/uptime-checker.sh @@ -7,16 +7,16 @@ # yaswant.pradhan 2019-11-19 # ----------------------------------------------------------------------------- -HOST="${1:-vld401}" +HOST="${1:-$HOSTNAME}" EMAIL="${2:-$(getent passwd "$USER" | awk -F: '{print $5}')@metoffice.gov.uk}" up_since="$(ssh "$HOST" 2>/dev/null uptime --since)" -OVER_WEEK=$(echo "$(date -d "$up_since" +%s)" "$(date +%s)" | \ +OVER_WEEK=$(echo "$(date -d "$up_since" +%s)" "$(date +%s)" |\ awk '{printf "%d", ($2-$1)/604800}') if (( OVER_WEEK >= 1 )); then ssh "$HOST" 2>/dev/null w --short | mail -s "Uptime for $HOST" "$EMAIL" S='week'; (( OVER_WEEK > 1 )) && S+=s - echo "Over $OVER_WEEK $S; Email sent to $EMAIL" + [ -t 1 ] && echo "Over $OVER_WEEK $S; Email sent to $EMAIL" else - echo "OK! Up under a week." + [ -t 1 ] && echo "OK! Up under a week." fi diff --git a/wl2wn b/wl2wn new file mode 100755 index 0000000..e1f4818 --- /dev/null +++ b/wl2wn @@ -0,0 +1,10 @@ +#!/bin/bash +# wl2wn wavelength in um +# returns wavenumber in cm-1 +# yaswant.pradhan + +[[ $# -eq 0 ]] && echo "usage: $(basename $0) wavelength_in_microns" && exit 1 +for i in $@; do + echo $i |awk '{printf "%g\n", 1/$1*10e3}' +done +echo diff --git a/wn2wl b/wn2wl new file mode 100755 index 0000000..5ab975b --- /dev/null +++ b/wn2wl @@ -0,0 +1,10 @@ +#!/bin/bash +# wn2wl wavenumber in cm-1 +# returns wavelength in um +# yaswant.pradhan + +[[ $# -eq 0 ]] && echo "usage: $(basename $0) wavenumber_in_cm-1" && exit 1 +for i in $@; do + echo $i |awk '{printf "%g ", 1/$1*10e3}' +done +echo \ No newline at end of file diff --git a/xd b/xd new file mode 100755 index 0000000..335656f --- /dev/null +++ b/xd @@ -0,0 +1,15 @@ +#!/bin/bash +# xd +# check difference between 2 or 3 files. if 2 directories are given then xd +# will do a recursive diff ignoring files in .svn directories +# yaswant.pradhan + + +[[ $# -lt 2 || $# -gt 3 ]] \ +&& echo -e "usage: ${0##*/} File1|Dir1 File2|Dir2 [File3]\n" && exit +command -v xxdiff >/dev/null 2>&1 || { echo "xxdiff not found" && exit 1; } + +if [[ $# -eq 3 ]]; then xxdiff "$@" & +else + xxdiff -r -e '.svn' -e '.git' "$1" "$2" & +fi