You are not logged in.

#2401 2014-07-05 20:28:05

kira
Member
Registered: 2014-07-05
Posts: 1

Re: Post your handy self made command line utilities

hi

I would like to have opinions on this script to make backups

#!/bin/bash

#initie le 5/14

#conditions initiales pour le bon fonctionnement du script:
#	-sdX -> ddi source
#	-sdY -> dde destinataire
#	-partition DOCUMENTS (inclu dans sdX) est deja montee
#	-partition SAUVEGARDES (inclu dans sdY) n'est pas encore montee
#	-les programmes wmctrl et countdown sont deja installes
#	-on considere que les clefs usb n'ont qu'une partition chacune
#	-plusieurs clef usb peuvent etre connectees a la fois

# methode:
#   -> citer les sources
#   -> pres des fonctionnalitees du script
#   -> horodater les modifs avec description
#   -> erreur > fichier
#   -> ...

# fonctionnalitees:
#   -> eteint la machine à la fin de la sauvegarde
#   -> sauvegarde part docs + sys + 1ere part de clefs clefs_usb
#   -> grace à renice, ne prend pas trop de ressource

#ne sert à rien:
pause()
{
  read -p "$*"
}

restore_mbr()
{
  dd if=backup-sda.mbr of=/dev/sda
  sfdisk /dev/sda < backup-sda.sfdisk
}

check_mount()
{
  if grep -qs '$1' /proc/mounts
  then
    return 0
  else
    return 1
  fi
}
#

check_device()
{
  if [ -z "$(blkid $1)" ]
  then
    return 0
  else
    return 1
  fi
}

save_MBR()
{
  local ddi=${mount_point_source:0:8}
  local dde=${mount_point_dest:0:8}
  
  rm -Rf $dest_sauv/mbr/
  rm -Rf $dest_sauv/table_des_partitions/
  
  check_dir $dest_sauv/mbr/
  check_dir $dest_sauv/table_des_partitions/

  echo "sauvegardes des MBR des dd contenant les partions DOCUMENTS & SAUVEGARDES"
  sudo dd if=$ddi of=$dest_sauv/mbr/ddi_$date bs=512 count=1 > /dev/null 2>&1
  sudo dd if=$ddi of=$dest_sauv/mbr/dde_$date bs=512 count=1 > /dev/null 2>&1
  sudo sfdisk -d $dde > $dest_sauv/table_des_partitions/ddi_$date.dmp > /dev/null 2>&1
  sudo sfdisk -d $dde > $dest_sauv/table_des_partitions/dde_$date.dmp > /dev/null 2>&1
}

mount_part()
{
  echo "creation du dossier qui servira a monter la partition $1 puis montage de cette derniere"
  if [ ! -d $2 ]
  then
    sudo mkdir $2
  fi
  sudo mount $3 $2
}

umount_part()
{
  echo "demontage de la partition $1 puis suppression du dossier qui servit a monter cette derniere"
  sudo umount $2
  sudo rmdir $2
}

check_dir()
{
  if [ ! -d "$1" ]
  then
    mkdir -p $1
  fi
}

save_DOCUMENTS()
{
  local delta=""
  let "delta=60*60*24*14"
  local tmp=""
  local today=$date
  
  check_dir $destinataire/docs_supprimes/$today/
  check_dir $destinataire/logs/
  
  echo "sauvegarde du contenu de la partition DOCUMENTS"
  sudo rsync -hau --delete --backup --backup-dir=$destinataire/docs_supprimes/$today/ --stats --exclude=**/lost+found*/ --exclude=**/*Trash*/ --exclude=**/*trash*/ $source/ $destinataire/documents_perso/ > $destinataire/logs/documents_perso_$today
  
  ls $destinataire/docs_supprimes/ > tmp.txt
  if [ -s tmp.txt ]
  then
    while read dir
    do
      let "tmp=$(date +%s)-${dir##*_}"
      if [ "$tmp" -gt "$delta" ]
      then
	rm -R dir
      fi
    done < tmp.txt
  fi
  rm tmp.txt
}

save_systeme()
{
  check_dir $destinataire/systeme/
  check_dir $destinataire/logs/
  
  echo "sauvegarde du systeme actuel"
  sudo rsync -hau --delete --stats --numeric-ids --exclude=/media --exclude=/tmp --exclude=/dev --exclude=/proc --exclude=/sys --exclude=/mnt --exclude=**/lost+found*/ --exclude=**/*Trash*/ --exclude=**/*trash*/ / $destinataire/systeme >> $destinataire/logs/systeme_$date
  blkid > $destinataire/logs/partitionnement_du_ddi_contenant_le_systeme_sauvegarde.txt
}

save_usb_key()
{
  local dir_key_source="/media/key"
  local dir_key_dest=""
  local id_key=""
  
  if [ -d "/dev/disk/by-id/*usb*" ]
  then
    ls /dev/disk/by-id/*usb* | grep part1 >> mount_point_key.txt
    while read mount_point_key
    do
      id_key=${mount_point_key##*/}
      dir_key_dest="$dest_sauv/clefs_usb/${id_key}/"
      echo "sauvegarde du contenu de la clef usb : $id_key"
      check_dir $dir_key_dest
      mount_part "de la clef usb" "$dir_key_source/" "$mount_point_key"
      rsync -hau $1 --stats --exclude=**/lost+found*/ --exclude=**/*Trash*/ --exclude=**/*trash*/ $dir_key_source/ $dir_key_dest > $dir_key_dest/log_$date
      umount_part "de la clef usb" "$dir_key_source/"
      echo ""
    done < mount_point_key.txt
    rm mount_point_key.txt
  else
    echo "aucune clef usb n'est connectee!"
  fi
}

shutdown()
{
  wmctrl -a Konsole
  echo -e "\033[1;31mextinction de l'ordinateur dans 30 secondes!\033[0m"
  countdown.sh 30
  sudo shutdown -ah now
}

restore_systeme()
{
  local answer=""
  
  check_dir $destinataire/logs/
  
  echo "restauration du systeme sauvegarde sur $1"
  echo "Voulez-vous supprimer le contenu de $1? [y/n]"
  read answer
  if [ "$answer" = "Y" -o "$answer" = "y" ];
  then
    sudo rm -R $1/
    sudo rsync -ha --stats --numeric-ids $destinataire/systeme/ $1 > $destinataire/logs/restore_system_${date}.txt
    cd $1
    sudo mkdir mnt tmp proc media dev sys
  fi
}

show_help()
{
  echo "aide:"
  echo "	-k sauvegarde du contenu des clefs usb"
  echo "	-K idem avec suppressions des fichiers de destinations qui ne sont pas a la source"
  echo "	-m sauvegarde des MBR uniquement"
  echo "	-d sauvegarde des documents uniquement"
  echo "	-s sauvegarde du systeme uniquement"
  echo "	-x execution de shutdown -ah now"
  echo "	-r /dev/sdXY restauration du systeme"
  echo "sinon, tout en meme temps dans l'ordre sauf les parametres -r, -d, -k et undel"
  echo "	undel idem mais avec le parametre -d au lieu de -D"
  echo "	-h cette aide"
}

load_conf()
{
  cd ~
  
  type wmctrl > /dev/null 2>&1 || (print "installation de wmctrl" && sudo apt-get -y install wmctrl)
  
  if [ ! -e ~/.backup_conf.cfg ]
  then
    echo "Voici les partitions disponibles:"
    blkid -o list
    echo -e -n "Entrez l'uuid de la partition DOCUMENTS:\n\t->"
    read -r uuid_source
    echo -e -n "Entrez l'uuid de la partition SAUVEGARDES:\n\t->"
    read -r uuid_dest
    echo -e -n "Entrez l'adresse de la source:\n\t->"
    read -r source
    touch ~/.backup_conf.cfg
    echo "$uuid_source $uuid_dest $source" > ~/.backup_conf.cfg
  else
    read -r uuid_source uuid_dest source < ~/.backup_conf.cfg
  fi
}

manage_saves()
{
  if check_device "$mount_point_dest/"
  then
    mount_part "SAUVEGARDES" "$destinataire/" "$mount_point_dest"
    case $1 in
      DOCS)
	save_DOCUMENTS
	;;
      SYS)
	save_systeme
	;;
      BOTH)
	save_systeme
	save_DOCUMENTS
	;;
    esac;
    umount_part "SAUVEGARDES" "$destinataire/"
  else
    echo "le dd de destination n'est pas connecte!"
  fi
}

date=`date +%Y-%m-%d-%k-%M_%s`
source=""
destinataire="/media/SAUVEGARDES"
file_errors="$destinataire/logs/erreurs.txt"
mount_point_dest=""
mount_point_source=""
uuid_source=""
uuid_dest=""

load_conf

dest_sauv="$source/Documents/backup"
mount_point_dest=$(blkid -U $uuid_dest)
mount_point_source=$(blkid -U $uuid_source)

# echo "$mount_point_dest"
# if check_device "$mount_point_dest/"
# then
#   echo "device monte!"
# else
#   echo "non"
# fi
# exit 1

check_dir $dest_sauv

# On donne une priorité faible au processus pour ne pas dégrader les performances des autres applications
renice +19 $$

while getopts "sdmhxr:k" opt
do
  case $opt in
    s)
      echo "-- sauvegarde du systeme uniquement --"
      manage_saves "SYS"
      ;;
    d)
      echo "-- sauvegarde des documents uniquement --"
      manage_saves "DOCS"
      ;;
    m)
      echo "-- sauvegarde des MBR uniquement --"
      save_MBR
      ;;
    h)
      show_help
      exit 1
      ;;
    x)
      shutdown
      ;;
    r)
      nom_part=$(blkid $OPTARG)
      nom_part=${nom_part##*LABEL=\"}
      nom_part=${nom_part%%\"*}
      
      if [[ $OPTARG != /dev/sd* ]]
      then
	echo "l'argument doit etre du type \"/dev/sdXY\", avec X et Y des lettres de l'alphabet!"
	exit 0
      fi
      echo "-- restauration du systeme sur la partition $nom_part --"
      mount_part "SAUVEGARDES" "$destinataire/" "$mount_point_dest"
      mount_part "$nom_part" "/media/RESTORE/" "$OPTARG"
      restore_systeme "/media/RESTORE"
      umount_part "$nom_part" "/media/RESTORE/"
      umount_part "SAUVEGARDES" "$destinataire/"
      unset nom_part
      ;;
    k)
      save_usb_key ""
      ;;
    K)
      save_usb_key "--delete"
      ;;
    \?)
      echo "invalid option: -$OPTARG"
      show_help
      exit 0
      ;;
  esac
done

if [ $# -eq 0 ]
then
  echo "-- sauvegarde generale --"
  save_usb_key "--delete"
  save_MBR
  manage_saves "BOTH"
  shutdown
fi

exit 1

# le programme countdown.sh:
# #!/bin/bash
# 
# beginning=$(date +%s)
# diff="0"
# rest="$1"
# tmp="0"
# now="0"
# 
# echo -n "$1"
# while [ "$diff" -lt "$1" ]
# do
#   let "now=$(date +%s)"
#   let "diff=now-beginning-1"
#   let "tmp=$1-diff"
#   if [ "$tmp" -lt "$rest" ]
#   then
#     let "rest=$1-diff"
#     echo -n ", $rest"
#   fi
# done
# 
# echo ""

if not, would you know a site that allows you to publish bash scripts?

Thanks!
Bye.

Offline

#2402 2014-07-05 20:31:09

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: Post your handy self made command line utilities

You can post them here, or to http://codereview.stackexchange.com/, but as a courtesy, you should post them in English https://wiki.archlinux.org/index.php/Fo … s_and_Code


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#2403 2014-07-09 06:38:39

theodoreward
Member
Registered: 2013-04-05
Posts: 20

Re: Post your handy self made command line utilities

Its not beautiful, but I've been using this script for a few years to interact with pacman and packer so I don't have to remember the various options for things I don't do frequently.
Also, to make things short and sweet, the script is named "r" and the options are each one character.

Common commands below:
$ r up  # Update pacman
$ r ua  # Update pacman, then AUR (with packer)
$ r c    # Remove cached packages over 2 versions old
$ r ip <package> # Install using pacman
$ r ua <aur_package> # Install using packer
$ r sp <package> # Search official repository for package
etc....

Note: I forgot I began using pacmatic at some point, change back to pacman if not using pacmatic: s/pacmatic/pacman/g

#!/bin/sh

howto()
{
	echo $1
	echo '
Usage: repo [ACTION]<REPOSITORY> <options> <target>

    ACTION: One of [sirunfcol] for Search, Install, Remove, Update,
    iNformation, Files, Clean, Owner or List(by size)
    REPOSITORY: One of [pad] for Pacman or AUR (d for updating devel)
        Not specified for remove operation
    TARGET: For "s,i,r,n,f" a package name must be supplied, for "o" 
        a path to a file must be given, "c" takes no target.
        
        If not specified for update, update will apply to both repositories 
        (not yet implemented).
        
All additional strings are passed to the command.

Examples:
    Search pacman for the term "dialog"
    $ repo sp dialog 
    Install cairo-dock from AUR
    $ repo ia cairo-dock
    
    When removing pass "-s" if wishing to remove dependencies as well.
'
}

ACTIONS=("s", "i", "r", "u", "n", "f", "c", "o", "l")

# TLW -- Load pacmatic.
. /usr/bin/pacmatic --as-lib

# --needed PACMAN ONLY, don't re-install if package is already present
# --noedit PACKER ONLY
STDOPTS="--noconfirm"

if [ $# -lt 1 ]; then
	howto "Not enough arguments"
	exit
fi

# Substring extraction: ${string:position:length}
initial=$1
act=${initial:0:1}
where=${initial:1:1}

shift
what=$*

SU="sudo"

# Search, Install, Remove, Update
if [ "$act" == "s" ]; then
    opt="-Ss"
    unset SU    # Not needed for searching
elif [ "$act" == "i" ]; then
    opt="-S"
elif [ "$act" == "r" ]; then
    opt="-R"
    cmd="pacman"
elif [ "$act" == "u" ]; then
    opt="-Syu"

elif [ "$act" == "n" ]; then
	cmd="pacman"
	opt="-Qi"
	unset STDOPTS
	unset SU
elif [ "$act" == "o" ]; then
	cmd="pacman"
	opt="-Qo"
	unset STDOPTS
	unset SU
elif [ "$act" == "f" ]; then
	cmd="pacman"
	opt="-Qql"
	unset STDOPTS
	unset SU

# Remove cached and orphaned packages then exit.
elif [ "$act" == "c" ]; then
    sudo paccache -rv -k 2 
    sudo pacman -Rns $(pacman -Qtdq)
    exit

# Show list of packages sorted by size.
elif [ "$act" == "l" ]; then
	expac '%m\t%n' | sort -h
	exit
else
	howto "$act is an invalid option"
	exit
fi

# Some commands don't require specifying this.
if [ -z $cmd ]; then
    # Pacman or Packer?
    if [ "$where" == "p" ]; then
        #cmd="pacman"
        cmd="pacmatic"

    elif [ "$where" == "a" ]; then
        unset SU
        cmd="packer"

    elif [ "$where" == "d" ] && [ "$act" == "u" ]; then
	cmd="packer"
	opt="$opt --devel"

	# Turn off "noconfirm" for development packages.
	# This way we can see if they are up to date before
	# reinstalling them.
	unset STDOPTS

    else
        howto "Invalid repository \"$where\" specified"
        exit
    fi
fi

#echo "$SU $cmd $STDOPTS $opt $what"
$SU $cmd $STDOPTS $opt $what

Last edited by theodoreward (2014-07-09 06:42:30)

Offline

#2404 2014-07-09 19:24:10

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: Post your handy self made command line utilities

Zebra striping terminal output

#!/bin/bash

if [[ -t 1 ]]; then
  cols=$(tput cols)
  # apply zebra striping
  for ((r=0;;r=1-r)); do
    IFS='' read -r line || break
    (( r )) && printf '\x1b[40m'
    printf "%-${cols}s\n" "${line:0:$cols}"  # trim or pad
    (( r )) && printf '\x1b[m'
  done
else
  cat
fi

# vim:ts=2:sw=2:et:

feed any text to its standard input and see for yourself smile


This silver ladybug at line 28...

Offline

#2405 2014-07-09 21:22:11

stevenhoneyman
Member
From: England
Registered: 2014-05-25
Posts: 241

Re: Post your handy self made command line utilities

lolilolicon wrote:

Zebra striping terminal output

#!/bin/bash

if [[ -t 1 ]]; then
  cols=$(tput cols)
  # apply zebra striping
  for ((r=0;;r=1-r)); do
    IFS='' read -r line || break
    (( r )) && printf '\x1b[40m'
    printf "%-${cols}s\n" "${line:0:$cols}"  # trim or pad
    (( r )) && printf '\x1b[m'
  done
else
  cat
fi

# vim:ts=2:sw=2:et:

feed any text to its standard input and see for yourself smile

It's not working for me sad
I'm using rxvt-unicode (256 color)

Offline

#2406 2014-07-09 21:37:03

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Post your handy self made command line utilities

Run

for i in {1..9}; do echo $i; done | ./<zebra script>

Offline

#2407 2014-07-09 21:47:05

stevenhoneyman
Member
From: England
Registered: 2014-05-25
Posts: 241

Re: Post your handy self made command line utilities

karol wrote:

Run

for i in {1..9}; do echo $i; done | ./<zebra script>

Same output as without the script. I've found it works in vte3 based terminals at least (tilda). Wonder why urxvt doesnt like it

Offline

#2408 2014-07-09 22:31:57

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Post your handy self made command line utilities

It works on my urxvt.
Tweak the '\x1b[40m', change it to e.g. '\x1b[35m'.

Offline

#2409 2014-07-09 23:45:30

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: Post your handy self made command line utilities

Yeah, you may want to change the color codes to suit your colorscheme. Using color 0 as bg looks good if your colorscheme is Solarized Dark.
The color codes are pretty standard, so should work in most terminals I believe. Maybe using `tput` would be more portable? And also, you can specify RGB values directly if your terminal supports true colors https://gist.github.com/XVilka/8346728

EDIT: a sure fire way to see the effect is to reverse the color, so instead of \x1b[40m, use \x1b[7m

Last edited by lolilolicon (2014-07-10 00:19:30)


This silver ladybug at line 28...

Offline

#2410 2014-07-18 09:09:38

mid-kid
Member
Registered: 2013-05-07
Posts: 177

Re: Post your handy self made command line utilities

Not really handy, but I find this funny:

ponysay -f $(curl pkgbuild.com 2> /dev/null | grep '<title>' | cut -d '>' -f 2 | cut -d '<' -f 1) 'OBEY!'

Last edited by mid-kid (2014-07-18 09:11:00)


If it ain't broke, you haven't tweaked it enough.

Offline

#2411 2014-07-18 09:44:48

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Post your handy self made command line utilities

ponysay should come with Surgeon General's warning ...


http://www.reddit.com/r/commandline/com … tils_2040/
I found csplit pretty useful, but I had to un-escape the '^':

csplit -f foo-part. foo.txt '/^\[.*\]$/' '{*}'

Offline

#2412 2014-07-19 07:57:16

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: Post your handy self made command line utilities

#!/usr/bin/awk -f
# print uptime for tmux

BEGIN {
  while ("uptime" | getline)
    gsub(/,/,"")
    if (NF==10)
      print $3
    else if (NF==11)
      print $3,$4
    else if (NF==12)
      print $3,$4,$5
    else
      print $3,$4,$5,$6
  close("uptime")
}

# vim:set ts=2 sts=2 sw=2 et:

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#2413 2014-07-19 11:40:34

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: Post your handy self made command line utilities

I've never muched like the mutt-sidebar patches.  I only have a couple folders/mailboxes I use frequenty, and for a long time I have been looking for a mutt-tabbed patch where a few important mailboxes or folders will have tabs and keybindings.

The bindings are easy - but then I realized I could expand the xtitle script I already use to display a "tab bar" in the status bar.  So here you have Mutt tabbed:

# muttrc exccerpts:
set status_format="/home/username/.mutt/xtitle '%f %> [%n new / %m total] ' |"

macro index <f1> "c=INBOX<return>" "change to inbox"
macro index <f2> "c=INBOX.other<return>" "change to other"
macro index <f3> "c=INBOX.Sent<return>" "change to sent"
macro index <f4> "c=../other_account/INBOX<return>" "change to other_account"
#!/bin/bash
# /home/username/.mutt/xtitle
_in="${1/=/}"
_box="${_in%% *}"
_num="[${_in##*[}"
_len=$(( ${#_in} - ${#_num} ))

case $_box in
	INBOX)
		mbox="Inbox"
		str="[ F1: Inbox ]  F2: Storage    F3: Sent    F4: Acnt Name  "
		;;
	INBOX.other)
		mbox="Storage"
		str="  F1: Inbox  [ F2: Storage ]  F3: Sent    F4: Acnt Name  "
		;;
	INBOX.Sent)
		mbox="Sent"
		str="  F1: Inbox    F2: Storage  [ F3: Sent ]  F4: Acnt Name  "
		;;
	*)
		mbox="Other Account"
		str="  F1: Inbox    F2: Storage    F3: Sent  [ F4: Acnt Name ]"
		;;
esac

printf "\033]0;Mutt: $mbox\007" > /dev/tty

printf "%-*s %s" "$_len" "$str" "$_num"

"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#2414 2014-07-30 01:21:22

grandtheftjiujitsu
Member
Registered: 2013-07-27
Posts: 91

Re: Post your handy self made command line utilities

Here's an RPG dice rolling script a friend and I made [Python 2.7].  I'm still pretty new to the programming world, so any feedback would be greatly appreciated.

  • Choose the number of dice to roll

  • Choose the number of sides on each die

  • Roll the same type (number of sides) dice at once

  • Split roll with assorted combination of dice types

#!/bin/python2.7
# RPG Dice Roller 
# by grandtheftjiujitsu & Senglis3

import random

diff_dice = raw_input("Do all the dice you want to roll have the same number of sides? (y/n)   ")
if diff_dice == "Y" or diff_dice == "y":
	dice = input("Enter the number of dice:  ")
	sides = input("Enter number of sides on each die:  ")
	def roll():
		result = random.randint(1,sides)
		print result
	count = 0
	while count < dice:
		roll()
		count = count + 1
elif diff_dice == "N" or diff_dice == "n":
	dice4 = input("Number of 4-sided die:  ")
	dice6 = input("Number of 6-sided die:  ")
	dice8 = input("Number of 8-sided die:  ")
	dice10 = input("Number of 10-sided die:  ")
	dice12 = input("Number of 12-sided die:  ")
	dice20 = input("Number of 20-sided die:  ")
	def multi_dice():
		dice4list = []
		dice6list = []
		dice8list = []
		dice10list = []
		dice12list = []
		dice20list = []
		if dice4 == 0:
			pass
		else:
			for x in range(0, dice4):
				term1 = random.randint(1,4)
				dice4list.append(term1)
			print "4-sided Die Results:  %s" % (dice4list)
		if dice6 == 0:
			pass
		else:
			for x in range(0, dice6):
				term2 = random.randint(1,6)
				dice6list.append(term2)
			print "6-sided Die Results:  %s" % (dice6list)
		if dice8 == 0:
			pass
		else:
			for x in range(0, dice8):
				term3 = random.randint(1,8)
				dice8list.append(term3)
			print "8-sided Die Results:  %s" % (dice8list)
		if dice10 == 0:
			pass
		else:
			for x in range(0, dice10):
				term4 = random.randint(1,10)
				dice10list.append(term4)
			print "10-sided Die Results:  %s" % (dice10list)
		if dice12 == 0:
			pass
		else:
			for x in range(0, dice12):
				term5 = random.randint(1,12)
				dice12list.append(term5)
			print "12-sided Die Results:  %s" % (dice12list)
		if dice20 == 0:
			pass
		else:
			for x in range(0, dice20):
				term6 = random.randint(1,20)
				dice20list.append(term6)
			print "20-sided Die Results:  %s" % (dice20list)
	multi_dice()
else:
	quit()

Last edited by grandtheftjiujitsu (2014-07-30 02:42:41)

Offline

#2415 2014-08-11 19:25:15

karkhaz
Member
Registered: 2014-01-25
Posts: 79

Re: Post your handy self made command line utilities

Script to colourise the output from the ocaml compiler. Errors and warnings get highlighted, so I can easily locate them when I scroll back up the cascade of compiler output. I know I could just use make -s, but sometimes I do want all the output but with the interesting bits highlighted.

2014_08_11_201221_4480x2224_scrot.jpg

make $@ | sed \
    -e "s\
/File \"\(.*\)\"\
/File \"$(tput setab 2)$(tput setaf 8)\1$(tput sgr0)\"\
/g" -e "s\
/, line \([0-9]*\)\
/, line $(tput setab 2)$(tput setaf 8)\1$(tput sgr0)\
/g" -e "s\
/, characters \([0-9]*\)-\([0-9]*\):\
/, characters $(tput setab 2)$(tput setaf 8)\1$(tput sgr0)-\
$(tput setab 2)$(tput setaf 8)\2$(tput sgr0):\                                            
/g" -e "s\
/Error:\(.*\)\
/$(tput setab 1)$(tput setaf 8)Error$(tput sgr0)$(tput setaf 1):\1$(tput sgr 0)\
/g" -e "s\
/Warning\(.*\)\
/$(tput setab 3)$(tput setaf 8)Warning\1$(tput sgr 0)\
/g"

I am aware that this is hideous, I am a newb at sed.

One day I will write a more general program that can handle the outputs of different compilers, if such a thing doesn't already exist.

Offline

#2416 2014-08-12 09:46:35

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: Post your handy self made command line utilities

karkhaz wrote:

Script to colourise the output from the ocaml compiler. Errors and warnings get highlighted, so I can easily locate them when I scroll back up the cascade of compiler output. I know I could just use make -s, but sometimes I do want all the output but with the interesting bits highlighted.

http://s28.postimg.org/vly2w6xq1/2014_08_11_201221_4480x2224_scrot.jpg

make $@ | sed \
    -e "s\
/File \"\(.*\)\"\
/File \"$(tput setab 2)$(tput setaf 8)\1$(tput sgr0)\"\
/g" -e "s\
/, line \([0-9]*\)\
/, line $(tput setab 2)$(tput setaf 8)\1$(tput sgr0)\
/g" -e "s\
/, characters \([0-9]*\)-\([0-9]*\):\
/, characters $(tput setab 2)$(tput setaf 8)\1$(tput sgr0)-\
$(tput setab 2)$(tput setaf 8)\2$(tput sgr0):\                                            
/g" -e "s\
/Error:\(.*\)\
/$(tput setab 1)$(tput setaf 8)Error$(tput sgr0)$(tput setaf 1):\1$(tput sgr 0)\
/g" -e "s\
/Warning\(.*\)\
/$(tput setab 3)$(tput setaf 8)Warning\1$(tput sgr 0)\
/g"

I am aware that this is hideous, I am a newb at sed.

One day I will write a more general program that can handle the outputs of different compilers, if such a thing doesn't already exist.

I'll not got through the script, but I will point out one thing.
You don't need all those -e options.

make $@ | sed 'all the commands go in here' 

The '...' can include newlines, so you can put each command on a separate line, and comments are allowed:

make $@ | sed '
# describe cmd1
cmd1
# describe cmd2
cmd2
# describe cmd3
cmd3
...
# describe cmdn
cmdn'

With the version of sed that comes with Arch (I'm not sure how portable this, so be aware), you can use other separator chars in the commands:

sed 's|findwith\init|replacewith\init|g'

The first char after the command is the one sed assumes to be the separator.

Finally to embed shell variables (should you wish), you just break the '...':

make $@ | sed 's/'$FIND'/'$REPLACE/'

Hopefully, this should make the code much more readable. smile


If you want to write a more generalised program, you can have multiple files containing the sed commands (say, one for each compiler) and reference those.


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#2417 2014-08-17 18:06:52

karkhaz
Member
Registered: 2014-01-25
Posts: 79

Re: Post your handy self made command line utilities

skanky wrote:

I'll not got through the script, but I will point out one thing.

Thanks for all the tips!

When I write a more general version, I doubt it will be just sed and bash though, I'll probably use a heavier scripting language. It would be nice, for example, to autodetect the compiler that is being used, and use that information to colourise the output accordingly.

For compilers that don't output an informational string when you invoke them, before the compiler output proper, I would just let the user specify the name of the ocmpiler as a command line argument or whatever.

Offline

#2418 2014-08-19 11:42:10

x33a
Forum Fellow
Registered: 2009-08-15
Posts: 4,587

Re: Post your handy self made command line utilities

skanky wrote:

Finally to embed shell variables (should you wish), you just break the '...':

make $@ | sed 's/'$FIND'/'$REPLACE/'

Isn't this missing a single quote, one before the last forward slash, like

make $@ | sed 's/'$FIND'/'$REPLACE'/'

Offline

#2419 2014-08-19 12:38:54

Rob_H
Member
Registered: 2012-06-19
Posts: 72

Re: Post your handy self made command line utilities

Before a potentially risky upgrade, I use these 3 LVM aliases to take a snapshot, remove the snapshot (if things go well), or revert back to the snapshot (if things go poorly).

alias mksnap='sudo lvcreate -s /dev/caprica_vg/root -L 200G -n snappy'
alias rmsnap='sudo lvremove /dev/caprica_vg/snappy'
alias snapback='sudo lvconvert --merge /dev/caprica_vg/snappy'

Change the logical volume path to meet your needs. Obviously, this only works if you use LVM for your root filesystem. I also stick this near the end of my ~/.bashrc to remind me when I have a snapshot open.

# warn if there's a backup snapshot in use
if [ -e /dev/caprica_vg/snappy ]; then
    echo 'LVM snapshot "snappy" exists. Are you done with it yet?'
fi

Offline

#2420 2014-08-21 09:12:02

KyleBytes
Member
Registered: 2014-08-15
Posts: 3

Re: Post your handy self made command line utilities

A small script for use after "photorec" to sort recovered files based on extension and remove recup.dir* directories:
/usr/bin/recup

#!/usr/bin/bash
if [ $(basename $PWD) != recup ]; then
        echo \
        "Please cd into recup"; exit
fi
direct=$(find . -type f -exec basename {} \; | grep -Po '[[:alnum:]]*(?=$)' | sort -u)
mkdir $direct
for i in $direct; do
        mv **/*$i $i
done
rmdir *
chown -R aaron:users .

note: photorec must have dumped files into a directory named "recup"

Last edited by KyleBytes (2014-08-21 09:13:18)

Offline

#2421 2014-08-22 17:53:18

Jristz
Member
From: America/Santiago
Registered: 2011-06-11
Posts: 1,022

Re: Post your handy self made command line utilities

The most unefficient way to update my PKGBUILD/src files...

#/usr/bin/env sh
for i in $HOME/Proyectos/Jristz/PKGBUILD/*
do
        cd "$i"
        makepkg -sc -d --nobuild -e --verifysource
        cd $HOME/Proyectos/Jristz/PKGBUILD
done

concidering that was my first atempt to try do a script since I start using Arch... 4 years ago... I think I need a more bashing life


Well, I suppose that this is somekind of signature, no?

Offline

#2422 2014-08-24 23:58:32

Theta91
Member
Registered: 2014-08-21
Posts: 13

Re: Post your handy self made command line utilities

I just wrote this script so that if I change my terminal colorscheme, I can keep up with it in my various configs by running only a single command. I chose python, so I could learn a little something along the way, though I think this would've been a bit easier in *sh.

You can view an example directory with a paths file and some config files @ my github: https://github.com/Theta91/dotfiles/tre … ncolorized

#!/usr/bin/env python3

"""This script colorizes a config file with uncolorized placeholders based on the
result of 'xrdb -query'. It requires a file (in this example, 'paths.txt') with
pairs of filenames and the full path of where they should should be placed,
separated by an '='. For example:
    xmonad.hs=/home/user/.xmonad/xmonad.hs
The uncolorized files must be in the same directory as 'paths.txt'.

This scripts assumes that you've set your colors as '*color0: #a-f0-9', i.e.
lowercase. It supports the standard 16 ANSI colors; bright colors are prefixed
with 'b'. Complete list of values available for substitution:
    $fg $bg
    $black $red $green $yellow $blue $magenta $cyan $white
    $bblack $bred $bgreen $byellow $bblue $bmagenta $bcyan $bwhite
"""

import argparse
import os
import re
import subprocess
import sys

def retrieveColors():
    xrdb = subprocess.check_output(['xrdb', '-query']).decode('utf-8')
    xrdb = xrdb.replace('\t', '')
    xrdb = re.sub(r'\*\.', '', xrdb)
    xrdb = re.findall(r'(?:background|color[\d]+|foreground):#[\da-z]{6}', xrdb)
    xrdb = [x.split(':') for x in xrdb]

    i = iter(xrdb)
    colors = dict(i)

    return colors

def retrieveFilePaths():
    with open(sys.argv[1]) as f:
        parentDir, whoCares = os.path.split(sys.argv[1])
        fileList = dict(x.strip().split('=', 1) for x in f if x.strip())
    source = []
    dest = []
    for k, v in fileList.items():
        source.append(os.path.join(parentDir, k))
        dest.append(v)

    return source, dest

def colorSubstitution(colors, contents):
    contents = contents.replace('$fg', colors['foreground'])
    contents = contents.replace('$bg', colors['background'])
    contents = contents.replace('$black', colors['color0'])
    contents = contents.replace('$red', colors['color1'])
    contents = contents.replace('$green', colors['color2'])
    contents = contents.replace('$yellow', colors['color3'])
    contents = contents.replace('$blue', colors['color4'])
    contents = contents.replace('$magenta', colors['color5'])
    contents = contents.replace('$cyan', colors['color6'])
    contents = contents.replace('$white', colors['color7'])
    contents = contents.replace('$bblack', colors['color8'])
    contents = contents.replace('$bred', colors['color9'])
    contents = contents.replace('$bgreen', colors['color10'])
    contents = contents.replace('$byellow', colors['color11'])
    contents = contents.replace('$bblue', colors['color12'])
    contents = contents.replace('$bmagenta', colors['color13'])
    contents = contents.replace('$bcyan', colors['color14'])
    contents = contents.replace('$bwhite', colors['color15'])
    return contents

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description=__doc__, formatter_class = argparse.RawDescriptionHelpFormatter)
    parser.add_argument('/path/to/paths.txt')
    args = parser.parse_args()
    colors = retrieveColors()
    source, dest = retrieveFilePaths()
    for i in range(len(source)):
        with open(source[i], 'r') as f:
            sourceContents = f.read()
        destContents = colorSubstitution(colors, sourceContents)
        with open(dest[i], 'w') as f:
            f.write(destContents)

    sys.exit(0)

edit: updated to teach myself a little argparse smile

Last edited by Theta91 (2014-08-25 23:20:23)

Offline

#2423 2014-08-25 11:14:57

x33a
Forum Fellow
Registered: 2009-08-15
Posts: 4,587

Re: Post your handy self made command line utilities

Jristz wrote:

The most unefficient way to update my PKGBUILD/src files...

#/usr/bin/env sh
for i in $HOME/Proyectos/Jristz/PKGBUILD/*
do
        cd "$i"
        makepkg -sc -d --nobuild -e --verifysource
        cd $HOME/Proyectos/Jristz/PKGBUILD
done

concidering that was my first atempt to try do a script since I start using Arch... 4 years ago... I think I need a more bashing life

Let me jump start your bashing life by bashing your script wink

First, you are missing  a bang (!) after the hash (#).
Second, since you are using bash, don't invoke the script as sh.

Last edited by x33a (2014-08-25 11:15:38)

Offline

#2424 2014-08-27 04:50:08

Theta91
Member
Registered: 2014-08-21
Posts: 13

Re: Post your handy self made command line utilities

Could very well perform poorly on v. large directories, but aside from that:

#!/usr/bin/env python3

"""Small wrapper for sxiv that opens all images in a directory. Input can be
either an image (in which case sxiv will open to it) or a directory (in which
case sxiv will open to the first image in the directory).

This script is a modification of loop's python replacement for rifle_sxiv.sh:
https://bbs.archlinux.org/viewtopic.php?pid=1232114#p1232114
"""

from os import listdir
from os import path
from re import search
from sys import argv
from subprocess import Popen

if path.isdir(path.abspath(argv[1])):
    dirname = path.abspath(argv[1])
else:
    dirname, basename = path.split(path.abspath(argv[1]))

images = [f for f in listdir(dirname) if search(r'.*.(png|jpg|jpeg|jpe|gif)$', f)]
images.sort(key=str.lower)

try:
    count = str(images.index(basename) + 1)
except NameError:
    count = str(1)

images = [path.join(dirname, f) for f in images]
Popen(['sxiv', '-afqn', count] + images)

Last edited by Theta91 (2014-08-27 05:11:19)

Offline

#2425 2014-08-27 12:18:03

steve___
Member
Registered: 2008-02-24
Posts: 452

Re: Post your handy self made command line utilities

I am using sxiv v1.1.1, how does this differ from:

'-r   Search the given directories recursively for images to view.' ?

Offline

Board footer

Powered by FluxBB