You are not logged in.

#1851 2012-07-24 22:56:51

Wey
Member
Registered: 2011-04-22
Posts: 217

Re: Post your handy self made command line utilities

I recently faced the same problem as in this thread: https://bbs.archlinux.org/viewtopic.php?pid=751666, i. e. to organize documents belonging to overlapping categories. This simple script reads a plain-text database of the form

"document.pdf" "category A" "category B"

Then, in the target directory, it creates a folder for each category and places symlinks to the original documents in the input directory there. Also includes a function for matching the input directory against the database to detect un-categorized documents and database entries corresponding to a no-longer existing files. To do: listing stray files in the target directory, linking documents in the database but with no category to the top-level of the target category (that is, a implied "./"-category), maybe nested categories, though I prefer flat hierarchies. (https://bitbucket.org/Wey/symbib)

#!/usr/bin/env python3

import re,os

pattern = re.compile('"([^"]*)"')

class Bibliography:

  def __init__(self, filename):
    self.documents = [ ]
    self.categories = { }

    self._parse_file(filename)

  def _parse_file(self, filename):
    f = open(filename, 'r')

    for line in f:
      tokens = re.findall(pattern, line)

      document = tokens[0]
      categories = tokens[1:]

      index = len(self.documents)
      self.documents.append(document)

      for c in categories:
        if c in self.categories:
          self.categories[c].append(index)
        else:
          self.categories[c] = [index]

    f.close()

  def validate_bib_in(self, bib_in):

    file_set = set(os.listdir(bib_in))
    docs_set = set(self.documents)

    not_in_db = file_set-docs_set
    not_in_fs = docs_set-file_set

    print('Files not in database:\n'+'\n'.join(not_in_db))
    print('Data not in filesystem:\n'+'\n'.join(not_in_fs))

  def validate_bib_out(self, bib_out):

    # XXX

    return

  def symlink(self, bib_in, bib_out):

    for c in self.categories:

      directory = os.path.join(bib_out, c)

      if os.path.exists(directory):
        if not os.path.isdir(directory):
          raise Exception('bib_out is not clear: "{}"'.format(target))
      else:
        os.mkdir(directory)

      for i in self.categories[c]:
        document = self.documents[i]
        source = os.path.join(bib_in, document)
        target = os.path.join(directory, document)

        if os.path.exists(target):
          if os.path.islink(target):
            os.remove(target)
          else:
            raise Exception('bib_out is not clear: "{}"'.format(target))

        os.symlink(os.path.relpath(source, directory), target)

  def __str__(self):
    return '{} documents, {} categories'\
    .format(len(self.documents), len(self.categories))

if __name__ == '__main__':

  import argparse
  parser = argparse.ArgumentParser()

  parser.add_argument('bib',
    help='bibliography database to be parsed')
  parser.add_argument('src',
    help='input directory containing files indexed in bibliography database')
  parser.add_argument('dst',
    help='target directory to create symlinks at')
  parser.add_argument('-v', '--validate', action="store_true",
    help='validate the integrity of the database and exit')

  args = parser.parse_args()

  bib = Bibliography(args.bib)

  if args.validate:
    bib.validate_bib_in(args.src)
    bib.validate_bib_out(args.dst)
  else:
    bib.symlink(args.src, args.dst)

  print(bib)

Offline

#1852 2012-07-24 23:03:56

n1x4
Member
From: Chernobyl
Registered: 2012-02-17
Posts: 149
Website

Re: Post your handy self made command line utilities

Just a bash statistics scripts (much like archey/alsi) with colors output..
tstat.png

#/bin/bash
clear
declare -rx BC="/usr/bin/bc"

if test -f $HOME/.gtkrc-2.0;then
    GTK=$HOME/.gtkrc-2.0
else
    GTK=$HOME/.gtkrc-3.0
fi

if test ! -x $BC; then
    echo -e "\e[0;5;31m$BC is not available, please install before continuing."
    exit 192
fi

host=`cat /proc/sys/kernel/hostname`
mem_total=`free -m | grep "Mem" | awk '{print $2}'`
mem_used=`free -m | grep "buffers/" | awk '{print $3}'`
hd_used=`df | grep "root" | awk '{print $3}'`
hd_total=`df | grep "root" | awk '{print $2}'`
hd1_used=`df | grep "home" | awk '{print $3}'`
hd1_total=`df | grep "home" | awk '{print $2}'`
type=`df -T | grep "root" | awk '{print $2}'`
type1=`df -T | grep "home" | awk '{print $2}'`
idle=`cat /proc/uptime | awk '{print $2}'`
kernel=`cat /proc/version | awk '{print $3}'`
cpu=`cat /proc/cpuinfo | grep "model name" | awk '{print $4,$5,$6, $7}'`
theme=`cat $GTK | grep "gtk-theme-name" | cut -d= -f2 | tr -d '"'`
icon=`cat $GTK | grep "gtk-icon-theme-name" | cut -d= -f2 | tr -d '"'`
font=`cat $GTK | grep "gtk-font-name" | cut -d= -f2 | tr -d '"'`

function uptime() {
	uptime=$(</proc/uptime)
    uptime=${uptime%%.*}

    seconds=$(( uptime%60 ))
    minutes=$(( uptime/60%60 ))
    hours=$(( uptime/60/60%24 ))
    days=$(( uptime/60/60/24 ))
    echo -e $days"-days", $hours"-hrs" $minutes"-mins" $seconds"-s"
}
 echo -e "\e[1;36m
\t\t       _      _  _   
\t\t _ __ / |_  _| || |  
\t\t| '_ \| \ \/ / || |_ 
\t\t| | | | |>  <|__   _|
\t\t|_| |_|_/_/\_\  |_|  
\e[0m"

echo -e "\e[1;33mUptime\e[0m:   $(uptime)"
echo -e "\e[1;33mKernel\e[0m:   $kernel"
echo -e "\e[1;33mHostname\e[0m: $host"
echo -e "\e[1;33mCPU\e[0m:      $cpu"
echo -e "\e[1;33mRAM\e[0m:      $mem_used Mb / $mem_total Mb (\e[0;31m$(($mem_used*100/$mem_total))\e[0m%)"
echo -e "\e[1;33mROOT\e[0m:     $(echo "scale=3; $hd_used/1000000" | bc) Gb / $(echo "scale=3; $hd_total/1000000" | bc) Gb (\e[0;31m$(($hd_used*100/$hd_total))\e[0m%) ($type)"
echo -e "\e[1;33mHOME\e[0m:     $(echo "scale=3; $hd1_used/1000000" | bc) Gb / $(echo "scale=3; $hd1_total/1000000" | bc) Gb (\e[0;31m$(($hd1_used*100/$hd1_total))\e[0m%) ($type1)"
echo -e "\e[1;33mTheme\e[0m:    $theme"
echo -e "\e[1;33mIcons\e[0m:    $icon"
echo -e "\e[1;33mFont\e[0m:     $font"
echo -e "\n"
echo -e "Colors:\n\e[0;30m###### \e[0;31m###### \e[0;32m###### \e[0;33m###### \e[0;34m###### \e[0;35m###### \e[0;36m###### \e[0;37m######\e[0m"
echo -e "\e[1;30m###### \e[1;31m###### \e[1;32m###### \e[1;33m###### \e[1;34m###### \e[1;35m###### \e[1;36m###### \e[1;37m######\e[0m\n"
exit 0

||github||

Offline

#1853 2012-07-24 23:11:22

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

Re: Post your handy self made command line utilities

n1x4 wrote:

mem_total=`free -m | grep "Mem" | awk '{print $2}'`

The grep is redundant - awk can handle that:

mem_total=$(awk '/Mem/ {print $2}' <(free -m))

You could save a few cats as well smile


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#1854 2012-07-24 23:26:37

n1x4
Member
From: Chernobyl
Registered: 2012-02-17
Posts: 149
Website

Re: Post your handy self made command line utilities

jasonwryan wrote:
n1x4 wrote:

mem_total=`free -m | grep "Mem" | awk '{print $2}'`

The grep is redundant - awk can handle that:

mem_total=$(awk '/Mem/ {print $2}' <(free -m))

You could save a few cats as well smile

Exactly what I was hoping for, feedback! Thanks for the tip!


||github||

Offline

#1855 2012-07-24 23:28:00

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

Re: Post your handy self made command line utilities

There are cats in your pipes?  They must hate it when you shower.


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

Offline

#1856 2012-07-24 23:52:40

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

Re: Post your handy self made command line utilities

I hope the furry creatures don't clog the toilet.


I think

awk -F '="|"' '/gtk-font-name/ {print $2}' $GTK 

should do the same as

cat $GTK | grep "gtk-theme-name" | cut -d= -f2 | tr -d '"'

If it doesn't work, please post a sample .gtkrc-2.0 / .gtkrc-3.0 file.

Offline

#1857 2012-07-25 03:11:19

n1x4
Member
From: Chernobyl
Registered: 2012-02-17
Posts: 149
Website

Re: Post your handy self made command line utilities

No cats were harmed in the making of this script... Thanks for the input guys^, appreciate it greatly.
One question about AWK though. I thought it read by LINE not by COLUMN? I had to pipe a little sed action here:

mem_used=$(awk '/cache/ {print $3}' <(free -m) | sed -n 2p)

because I kept getting the line above ("free") as well? I'm not really that familiar with awk yet so pardon the noob question..

#/bin/bash
clear
declare -rx BC="/usr/bin/bc"

if test -f $HOME/.gtkrc-2.0;then
    GTK=$HOME/.gtkrc-2.0
else
    GTK=$HOME/.gtkrc-3.0
fi

if test ! -x $BC; then
    echo -e "\e[0;5;31m$BC is not available, please install before continuing."
    exit 192
fi

host=$(awk '{print $1}' /proc/sys/kernel/hostname)
mem_total=$(awk '/Mem/ {print $2}' <(free -m))
mem_used=$(awk '/cache/ {print $3}' <(free -m) | sed -n 2p)
hd_used=$(awk '/root/ {print $3}' <(df))
hd_total=$(awk '/root/ {print $2}' <(df))
hd1_used=$(awk '/home/ {print $3}' <(df))
hd1_total=$(awk '/home/ {print $2}' <(df))
type=$(awk '/root/ {print $2}' <(df -T))
type1=$(awk '/home/ {print $2}' <(df -T))
kernel=$(awk '{print $3}' /proc/version)
cpu=$(awk '{print $4,$5,$6,$7}' /proc/cpuinfo | sed -n 5p)
theme=$(awk -F '="|"' '/gtk-theme-name/ {print $2}' $GTK)
icon=$(awk -F '="|"' '/gtk-icon-theme-name/ {print $2}' $GTK)
font=$(awk -F '="|"' '/gtk-font-name/ {print $2}' $GTK)

function uptime() {
	uptime=$(</proc/uptime)
    uptime=${uptime%%.*}

    seconds=$(( uptime%60 ))
    minutes=$(( uptime/60%60 ))
    hours=$(( uptime/60/60%24 ))
    days=$(( uptime/60/60/24 ))
    echo -e $days"-days", $hours"-hrs" $minutes"-mins" $seconds"-s"
}
 echo -e "\e[1;36m
\t\t       _      _  _   
\t\t _ __ / |_  _| || |  
\t\t| '_ \| \ \/ / || |_ 
\t\t| | | | |>  <|__   _|
\t\t|_| |_|_/_/\_\  |_|  
\e[0m"

echo -e "\e[1;33mUptime\e[0m:   $(uptime)"
echo -e "\e[1;33mKernel\e[0m:   $kernel"
echo -e "\e[1;33mHostname\e[0m: $host"
echo -e "\e[1;33mCPU\e[0m:      $cpu"
echo -e "\e[1;33mRAM\e[0m:      $mem_used Mb / $mem_total Mb (\e[0;31m$(($mem_used*100/$mem_total))\e[0m%)"
echo -e "\e[1;33mROOT\e[0m:     $(echo "scale=3; $hd_used/1000000" | bc) Gb / $(echo "scale=3; $hd_total/1000000" | bc) Gb (\e[0;31m$(($hd_used*100/$hd_total))\e[0m%) ($type)"
echo -e "\e[1;33mHOME\e[0m:     $(echo "scale=3; $hd1_used/1000000" | bc) Gb / $(echo "scale=3; $hd1_total/1000000" | bc) Gb (\e[0;31m$(($hd1_used*100/$hd1_total))\e[0m%) ($type1)"
echo -e "\e[1;33mTheme\e[0m:    $theme"
echo -e "\e[1;33mIcons\e[0m:    $icon"
echo -e "\e[1;33mFont\e[0m:     $font"
echo -e "\n"
echo -e "Colors:\n\e[0;30m###### \e[0;31m###### \e[0;32m###### \e[0;33m###### \e[0;34m###### \e[0;35m###### \e[0;36m###### \e[0;37m######\e[0m"
echo -e "\e[1;30m###### \e[1;31m###### \e[1;32m###### \e[1;33m###### \e[1;34m###### \e[1;35m###### \e[1;36m###### \e[1;37m######\e[0m\n"
exit 0

||github||

Offline

#1858 2012-07-25 03:17:49

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

Re: Post your handy self made command line utilities

awk does process by line. The term cache appears on both those lines. Try this:

awk '/cache:/ {print $3}' <(free -m)

You could also filter on the start of that line with

awk '/^-/ etc...

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#1859 2012-07-25 03:34:50

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

Re: Post your handy self made command line utilities

No cats were harmed, but there's some serious awk abuse.

Look at those poor little guys ... and all the work you are giving them.

The script runs two processes for "free" half a dozen for "df" and a couple dozen for "awk", when it could just do one free, one df, and two or three awks.

Yes, it would take some learning of awk, but it pays off to grok awk.  wink


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

Offline

#1860 2012-07-25 03:47:50

dodo3773
Member
Registered: 2011-03-17
Posts: 801

Re: Post your handy self made command line utilities

n1x4 wrote:

One question about AWK though. I thought it read by LINE not by COLUMN?

You can cut off lines with awk. Say you want to cut off the top 2 lines you can do it this way:

command | awk 'NR>2 {print $3}'

Offline

#1861 2012-07-25 04:35:50

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

Re: Post your handy self made command line utilities

Just to see how many awks I could save I rewrote the above script.  This might be fun to tinker with if you want to learn more about awk (my second favorite programming language).

This should behave identically to the original.

#/bin/bash
clear
echo -e "\e[1;36m
\t\t       _      _  _   
\t\t _ __ / |_  _| || |  
\t\t| '_ \| \ \/ / || |_ 
\t\t| | | | |>  <|__   _|
\t\t|_| |_|_/_/\_\  |_|  
\e[0m"


# UPTIME, KERNEL, HOSTNAME, CPU:
awk '// {
	sec = $1 % 60;
	min = ($1 / 60) % 60;
	hrs = ($1 / 360) % 24;
	dys = $1 / 86400;
	printf "\033[1;33mUptime\033[0m:   %i-days, %i-hrs %i-mins %i-s\n",
		dys, hrs, min, sec;
}' /proc/uptime
awk '// {printf "\033[1;33mKernel\033[0m:   %s\n", $3}' /proc/version
awk '// {printf "\033[1;33mHostname\033[0m: %s\n", $1}' /proc/sys/kernel/hostname
awk '/^model name/ {
	print "\033[1;33mCPU\033[0m:     ",$4,$5,$6,$7;
	exit;
}' /proc/cpuinfo


# MEM USE:
awk '
/Mem:/ { total=$2; }
/cache:/ {
	printf "\033[1;33mRAM\033[0m:      %i Mb / %i Mb (\033[0;31m%i\033[0m%)\n",
		$3, total, $3/total;
}' <(free -m)


# DISK USE:
awk '
/root/ {
	printf "\033[1;33mROOT\033[0m:     %.1f Gb / %.1f Gb (\033[0;31m%i\033[0m%) (%s)\n",
		$4/1000000,$3/1000000,$4*100/$3,$2;
}
/home/ {
	printf "\033[1;33mHOME\033[0m:     %.1f Gb / %.1f Gb (\033[0;31m%i\033[0m%) (%s)\n",
		$4/1000000,$3/1000000,$4*100/$3,$2;
}' <(/bin/df -T)


# GTK THEME:
GTK=$HOME/.gtkrc-3.0
[[ -f $HOME/.gtkrc-2.0 ]] && GTK=$HOME/.gtkrc-2.0
awk -F '="|"' '
/gtk-theme-name/ {printf "\033[1;33mTheme\033[0m:    %s\n", $2}
/gtk-icon-theme-name/ {printf "\033[1;33mIcons\033[0m:    %s\n", $2}
/gtk-font-name/	{printf "\033[1;33mFont\033[0m:     %s\n\n", $2}
' $GTK


# COLORS:
echo -e "Colors:\n\e[0;30m###### \e[0;31m###### \e[0;32m###### \e[0;33m###### \e[0;34m###### \e[0;35m###### \e[0;36m###### \e[0;37m######\e[0m\n\e[1;30m###### \e[1;31m###### \e[1;32m###### \e[1;33m###### \e[1;34m###### \e[1;35m###### \e[1;36m###### \e[1;37m######\e[0m\n"

Perhaps the most useful lesson is in the disk usage area.  One call to awk can extract various pieces of information from a file (or input stream) and format them for output.  There is no need to rerun awk (and df) a dozen times to get a dozen peices of information on disk use.

Last edited by Trilby (2012-07-25 04:39:05)


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

Offline

#1862 2012-07-25 13:43:32

n1x4
Member
From: Chernobyl
Registered: 2012-02-17
Posts: 149
Website

Re: Post your handy self made command line utilities

Trilby wrote:

Just to see how many awks I could save I rewrote the above script.  This might be fun to tinker with if you want to learn more about awk (my second favorite programming language).

This should behave identically to the original.

#/bin/bash
clear
echo -e "\e[1;36m
\t\t       _      _  _   
\t\t _ __ / |_  _| || |  
\t\t| '_ \| \ \/ / || |_ 
\t\t| | | | |>  <|__   _|
\t\t|_| |_|_/_/\_\  |_|  
\e[0m"


# UPTIME, KERNEL, HOSTNAME, CPU:
awk '// {
	sec = $1 % 60;
	min = ($1 / 60) % 60;
	hrs = ($1 / 360) % 24;
	dys = $1 / 86400;
	printf "\033[1;33mUptime\033[0m:   %i-days, %i-hrs %i-mins %i-s\n",
		dys, hrs, min, sec;
}' /proc/uptime
awk '// {printf "\033[1;33mKernel\033[0m:   %s\n", $3}' /proc/version
awk '// {printf "\033[1;33mHostname\033[0m: %s\n", $1}' /proc/sys/kernel/hostname
awk '/^model name/ {
	print "\033[1;33mCPU\033[0m:     ",$4,$5,$6,$7;
	exit;
}' /proc/cpuinfo


# MEM USE:
awk '
/Mem:/ { total=$2; }
/cache:/ {
	printf "\033[1;33mRAM\033[0m:      %i Mb / %i Mb (\033[0;31m%i\033[0m%)\n",
		$3, total, $3/total;
}' <(free -m)


# DISK USE:
awk '
/root/ {
	printf "\033[1;33mROOT\033[0m:     %.1f Gb / %.1f Gb (\033[0;31m%i\033[0m%) (%s)\n",
		$4/1000000,$3/1000000,$4*100/$3,$2;
}
/home/ {
	printf "\033[1;33mHOME\033[0m:     %.1f Gb / %.1f Gb (\033[0;31m%i\033[0m%) (%s)\n",
		$4/1000000,$3/1000000,$4*100/$3,$2;
}' <(/bin/df -T)


# GTK THEME:
GTK=$HOME/.gtkrc-3.0
[[ -f $HOME/.gtkrc-2.0 ]] && GTK=$HOME/.gtkrc-2.0
awk -F '="|"' '
/gtk-theme-name/ {printf "\033[1;33mTheme\033[0m:    %s\n", $2}
/gtk-icon-theme-name/ {printf "\033[1;33mIcons\033[0m:    %s\n", $2}
/gtk-font-name/	{printf "\033[1;33mFont\033[0m:     %s\n\n", $2}
' $GTK


# COLORS:
echo -e "Colors:\n\e[0;30m###### \e[0;31m###### \e[0;32m###### \e[0;33m###### \e[0;34m###### \e[0;35m###### \e[0;36m###### \e[0;37m######\e[0m\n\e[1;30m###### \e[1;31m###### \e[1;32m###### \e[1;33m###### \e[1;34m###### \e[1;35m###### \e[1;36m###### \e[1;37m######\e[0m\n"

Perhaps the most useful lesson is in the disk usage area.  One call to awk can extract various pieces of information from a file (or input stream) and format them for output.  There is no need to rerun awk (and df) a dozen times to get a dozen peices of information on disk use.

Cool man.. I went to bed last night thinking about how it could be done... I planned on asking you this AM for a link or something to maybe give me some insight on it but this is even better... I see now exactly how it's done.. After reading a LITTLE bit, I didn't know awk was this powerful?!?


||github||

Offline

#1863 2012-07-25 13:58:23

hesse
Member
Registered: 2011-12-08
Posts: 88

Re: Post your handy self made command line utilities

jasonwryan wrote:
awk '/cache:/ {print $3}' <(free -m)

I see you use process substitution whenever possible. Is it somehow faster than piping? Is there a difference between these? As I understand it, piping doesn't need to create a new temporary file descriptor to give the other process. E.g:

free -m | grep foo
grep foo <(free -m)

Last edited by hesse (2012-07-25 14:01:04)

Offline

#1864 2012-07-25 13:59:06

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

Re: Post your handy self made command line utilities

@n1x4, Your version is actually "cleaner" in some senses.  It is more readible and easy to understand what is happening.  For a process you only periodically use, these are good attributes.  But one of awk's strengths is in batch processing large volumes of (text) data.  If you ever start using for that purpose, reducing the number of calls can speed up the whole process substantially.

Last edited by Trilby (2012-07-25 13:59:47)


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

Offline

#1865 2012-07-25 17:39:19

Vain
Member
Registered: 2008-10-19
Posts: 179
Website

Re: Post your handy self made command line utilities

Found Trilby's "pstree_color":

https://github.com/TrilbyWhite/misc/blo … tree_color

I like the idea. I rewrote it using sed:

#!/bin/bash

pstree -U "$@" | sed '
	s/[-a-zA-Z]\+/\x1B[32m&\x1B[0m/g
	s/[{}]/\x1B[31m&\x1B[0m/g
	s/[─┬─├─└│]/\x1B[34m&\x1B[0m/g'

pstree now draws the graph using unicode characters. This makes it easier to color them. Also, it allows you to use it on a Linux VT or in a less pipe. I also colored the curly braces to quickly identify threads.

2012-07-25--colored_pstree.png

(Trilby's version is the one at the bottom. pp is "alias pp='colored_pstree -p | less'".)

Offline

#1866 2012-07-25 18:51:55

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

Re: Post your handy self made command line utilities

hesse wrote:
jasonwryan wrote:
awk '/cache:/ {print $3}' <(free -m)

I see you use process substitution whenever possible. Is it somehow faster than piping? Is there a difference between these? As I understand it, piping doesn't need to create a new temporary file descriptor to give the other process. E.g:

free -m | grep foo
grep foo <(free -m)

In this case, the choice was pedagogic rather than functional: moving the awk operation to the left as that was the focus of my comments.

As I understand it, there is no speed or economy gain using the one over the other in these cases.


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#1867 2012-07-25 21:11:33

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

Re: Post your handy self made command line utilities

Awesome vain.  That was a quick hack of mine.  Sed does seem the better choice in retrospect.  I like the addition of the braces.


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

Offline

#1868 2012-07-26 19:26:31

hesse
Member
Registered: 2011-12-08
Posts: 88

Re: Post your handy self made command line utilities

Trilby wrote:

I also have a c program to set my dwm status.  It uses at least and order of magnitude fewer resources (both memory and cpu) than a bash script.  I do not have it call xsetroot though - if you're writing it in C anyways, just have it set the root window name directly.  My version is up on github at the link to the left.

Thanks! Noticed the speed changed drastically. I rewrote your dwm statusbar using alot of macros to make it easier maintaining and adding new elements (at least for me). It's not quite finished yet, but it's a beginning:

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <X11/Xlib.h>
#include "dwmst.h"

/* limits (%) */
#define BATT_LOW    20
#define CPU_HI      50

/* statusbar update interval */
#define INTERVAL    1

/* files to read status from */
#define CPU_FILE        "/proc/stat"
#define MEM_FILE        "/proc/meminfo"
#define AUD_FILE        "/home/herman/log/alsavol"
#define MAIL_FILE       "/home/herman/log/nmails"
#define BATT_NOW        "/sys/class/power_supply/BAT0/charge_now"
#define BATT_FULL       "/sys/class/power_supply/BAT0/charge_full"
#define AC_FILE         "/sys/class/power_supply/AC/online"

/* statusbar strings */
#define CPU_STR         "CPU %d%% "
#define CPU_HI_STR      "CPU %d%% "
#define MEM_STR         "MEM %ld "
#define VOL_STR         "VOL %d "
#define BAT_STR         "BAT %d "
#define BAT_LOW_STR     " BAT %d  "         
#define AC_STR          "AC %d "                
#define DATE_TIME_STR   " %a %b %d %H:%M"     
#define NET_ON          " NET ON "
#define NO_NET          " NO NET "
#define MAIL_STR        "MAIL %d "

int main(int argc, const char *argv[]) {
    
    init();
    for (;;) {

        /* update CPU percent */
        add((cpuperc >= CPU_HI) ? CPU_HI_STR : CPU_STR, cpuperc);
        
        /* cpu - first snapshot */
        if (has(CPU_FILE) && get("cpu %ld %ld %ld %ld %ld", &user, &nicep, &sysp, &idle, &io)) {
            total = user + nicep + sysp + idle; 
            usleep(500000);
        }

        /* mem */
        if (has(MEM_FILE) && get("MemTotal: %ld kB\nMemFree: %ld kB\nBuffers: %ld kB\nCached: %ld kB\n",
                &memtotal,&memfree,&buffers,&cached)) {
            add(MEM_STR, (memtotal - (memfree + buffers + cached)) / 1000);
        }

        /* vol */
        if (has(AUD_FILE) && get("%d", &num))   { add(VOL_STR, num); }

        /* mail */
        if (has(MAIL_FILE) && get("%d", &num))  { add(MAIL_STR, num); } 

        /* bat/ac */
        if (has(AC_FILE) && get("%d", &ac) && 
            has(BATT_NOW) && get("%ld\n", &batn) &&
            has(BATT_FULL) && get("%ld\n", &batf))  {
            num = batn*100/batf;
            add((ac == 1 ) ? AC_STR : (num < BATT_LOW) ? BAT_LOW_STR : BAT_STR, num);
        }

        /* net */
        add("%s", (system("host google.com &> /dev/null") == 0) ? NET_ON : NO_NET);

        /* date/time */
        time(&current);
        strftime(statnext, 38, DATE_TIME_STR, localtime(&current)); 
        strcat(status, statnext);

        /* cpu - second snapshot */
        if(has(CPU_FILE) && get("cpu %ld %ld %ld %ld %ld", &user, &nicep, &sysp, &iidle, &io)) {
            ttotal = user + nicep + sysp + iidle;
            cpuperc = (100 * ((ttotal-total) - (iidle-idle)) / (ttotal-total));
        }

        /* set title */
        set();
    }
}

https://bitbucket.org/hesse/dwmst/src
Not sure if the header file is needed but if feel it's cleaner that way, Any feedback is nice.

Last edited by hesse (2012-07-27 13:01:57)

Offline

#1869 2012-07-26 19:35:45

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

Re: Post your handy self made command line utilities

Any particular reason for using clang?

Offline

#1870 2012-07-26 19:39:38

hesse
Member
Registered: 2011-12-08
Posts: 88

Re: Post your handy self made command line utilities

Edit: None other than more human-friendly error messages. I don't know much about compilers but I do know I have to understand what they are telling me. wink

Last edited by hesse (2012-07-26 19:44:44)

Offline

#1871 2012-07-30 07:26:24

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

Re: Post your handy self made command line utilities

Quick and dirty wrapper for managing systemd units (emphasis on dirty)...

#!/bin/bash
# wrapper for managing systemd services

arg1=$1

usage () {
    cat <<EOF
    Systemctl options:

    start      |  start a service
    restart    |  reload unit configuration
    stop       |  in the name of love
    enable     |  start at boot
    is-enabled |  check status
    status     |  current state
    disable    |  do not load at boot
    
    list       |  list all running services
    fail       |  list failed services

    reboot     |  restart
    shut       |  poweroff

    help       |  show the unit manual page

EOF
}

actions=("start" "restart" "stop" "enable" "disable")
 
for i in "${actions[@]}"; do
    [[ "$i" = "$arg1" ]] && super="yes"
done


if [[ $# -eq 2 ]] && [[ "$super" == "yes" ]]; then 
    sudo systemctl "$1" "$2".service

elif [[ $# -eq 2 ]]; then
    systemctl "$1" "$2".service
fi

if [[ $# -eq 1 ]]; then
    
    case "$1" in
        list) systemctl list-units ;;
       fail*) systemctl --failed   ;;
      reboot) systemctl reboot     ;;
       shut*) systemctl poweroff   ;;
           *) usage && exit        ;;
   esac
fi

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#1872 2012-08-03 05:01:03

madprops
Member
Registered: 2012-05-26
Posts: 26

Re: Post your handy self made command line utilities

looper
--------------
this script executes commands with
a certain delay and number of executions
arguments: command -d delay -n number of executions
or no arguments for interactive mode

#!/usr/bin/python

import os
import sys
import time
import argparse

command = ""
delay = 60
executions = 0
i = 0

try:

	if len(sys.argv) < 2:
		command = input('Command: ')
		if command == '':
			sys.exit('error: you need to provide a command')
		try:
			idelay = input('Delay (default 60 seconds): ')
			if idelay != '':
				delay = float(idelay)
			iexecutions= input('Number of executions (default infinite): ')
			if iexecutions != '':
				executions = int(float((iexecutions)))
		except:
			sys.exit('error: wrong arguments')
	else:
		parser = argparse.ArgumentParser()
		parser.add_argument('command', type=str)
		parser.add_argument('-d', help='Delay in seconds')
		parser.add_argument('-n', help='Number of executions')
		args = parser.parse_args()

		try:
			if args.command != None:
				command = str(args.command)
			if args.d != None:
				delay = float(args.d)
			if args.n != None:
				executions = int(args.n)
		except:
			sys.exit('error: wrong arguments')


	while True:
		os.system(command)
		if executions: 
			i += 1
			if i >= executions:
				break
		time.sleep(delay)
		print()

except KeyboardInterrupt:
	print()
	pass

Last edited by madprops (2012-08-03 22:34:08)

Offline

#1873 2012-08-03 13:37:50

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

Re: Post your handy self made command line utilities

@madprops
They're not spaces, these are newlines:

$ ./t1 "echo foo" -d 2 -s 2
foo


foo


foo

A simple loop can do this too:

while true; do echo foo && echo -e "\n"; sleep 2; done

Offline

#1874 2012-08-03 22:46:37

wuyingren
Member
From: Catalunya
Registered: 2009-03-18
Posts: 8
Website

Re: Post your handy self made command line utilities

Hi everyone!

I just wanted to share a little bash script I wrote. It's quite simple, but it can be useful to download bulk files unattended (mainly image galleries).

It needs 4 arguments: the initial and final number, and two parts of a link. Quite dirty, I know

#!/bin/bash
if [ $# -eq 4 ]
then
   : do
   COUNTER=$1
   while [ $COUNTER -lt $2 ]; do
       wget $3$COUNTER$4
       let COUNTER=COUNTER+1
   done
else
    printf "4 parameters required\n" >&2
    printf "USAGE: wget_images <initial number> <final number> <first part of link> <last part of link>\n"
    printf "EXAMPLE: wget_images 0 20 http://www.images.com/photo_ .jpg\n"
    exit 1 ## or whatever
fi

Hope somebody finds it useful : )

Offline

#1875 2012-08-05 10:17:54

Barrucadu
Member
From: York, England
Registered: 2008-03-30
Posts: 1,158
Website

Re: Post your handy self made command line utilities

I switched over to systemd yesterday, and one of the things I was doing with inittab was automatically logging in to tty2. Well, I modified the getty@.service file for autologin purposes, and today I wrote a little script to manage which gettys are started and which auto-login.

/usr/lib/systemd/system/autologin@.service

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=Autologin Getty on %I
Documentation=man:agetty(8)
After=systemd-user-sessions.service plymouth-quit-wait.service
After=rc-local.service

# If additional gettys are spawned during boot then we should make
# sure that this is synchronized before getty.target, even though
# getty.target didn't actually pull it in.
Before=getty.target
IgnoreOnIsolate=yes

# On systems without virtual consoles, don't start any getty. (Note
# that serial gettys are covered by serial-getty@.service, not this
# unit
ConditionPathExists=/dev/tty0

[Service]
Environment=TERM=linux
# the VT is cleared by TTYVTDisallocate
ExecStart=-/sbin/agetty --noclear -a barrucadu %I 38400
Type=idle
Restart=always
RestartSec=0
UtmpIdentifier=%I
TTYPath=/dev/%I
TTYReset=yes
TTYVHangup=yes
TTYVTDisallocate=yes
KillMode=process
IgnoreSIGPIPE=no

# Unset locale for the console getty since the console has problems
# displaying some internationalized messages.
Environment=LANG= LANGUAGE= LC_CTYPE= LC_NUMERIC= LC_TIME= LC_COLLATE= LC_MONETARY= LC_MESSAGES= LC_PAPER= LC_NAME= LC_ADDRESS= LC_TELEPHONE= LC_MEASUREMENT= LC_IDENTIFICATION=

# Some login implementations ignore SIGTERM, so we send SIGHUP
# instead, to ensure that login terminates cleanly.
KillSignal=SIGHUP

[Install]
Alias=getty.target.wants/autologin@tty1.service

getty@

#!/bin/zsh
#
# Enable or disable a TTY in multi-user mode.
#

GETTYSERVICE=/usr/lib/systemd/system/getty@.service
AUTOSERVICE=/usr/lib/systemd/system/autologin@.service
GETTYDIR=/etc/systemd/system/getty.target.wants/

function show_help()
{
    echo "USAGE: getty@ <action> <getty1> <getty2> ... <getty2>"
    echo
    echo "Actions:"
    echo "    list      - list the currently enabled gettys (takes no arguments)"
    echo "    enable    - cause a getty to be started (with no autologin)"
    echo "    disable   - stop a getty from being started"
    echo "    autologin - toggle auto-login status for an enabled getty"
}

function show_gettys()
{
    pushd $GETTYDIR
    for file in *; do
        tty=`echo $file | sed -e 's:.*@\(.*\)\.service:\1:'`
        if grep -q autologin $file; then
            echo "$tty [AUTO]"
        else
            echo $tty
        fi
    done
    popd
}

function enable_getty()
{
    getty=$1
    
    if [[ ! -e $GETTYDIR/getty@$getty.service ]] && [[ ! -e $GETTYDIR/autologin@$getty.service ]]; then
        ln -s $GETTYSERVICE $GETTYDIR/getty@$getty.service
    fi
}

function disable_getty()
{
    getty=$1

    rm $GETTYDIR/getty@$getty.service $GETTYDIR/autologin@$getty.service 2>/dev/null
}

function autologin_getty()
{
    getty=$1

    if [[ -e $GETTYDIR/getty@$getty.service ]]; then
        rm $GETTYDIR/getty@$getty.service
        ln -s $AUTOSERVICE $GETTYDIR/autologin@$getty.service
    elif [[ -e $GETTYDIR/autologin@$getty.service ]]; then
        rm $GETTYDIR/autologin@$getty.service
        ln -s $GETTYSERVICE $GETTYDIR/getty@$getty.service
    fi
}

action=$1

if [[ $action == "" ]]; then
    show_help
    exit 1
fi

if [[ $action == "list" ]]; then
    show_gettys
    exit 0
fi

while [[ $2 != "" ]]; do
    getty=$2
    shift

    case $action in
        "enable")
            enable_getty $getty
            ;;   
        "disable")
            disable_getty $getty
            ;;
        "autologin")
            autologin_getty $getty
            ;;
        *)
            show_help
            exit 1
            ;;
    esac
done

So, I can do nice things like:

 >>>  getty@ list
tty2 [AUTO]
tty3
tty4
tty5
tty6

 >>>  sudo getty@ autologin tty3 tty4 ; sudo getty@ disable tty6
Password: 

 >>>  getty@ list
tty2 [AUTO]
tty3 [AUTO]
tty4 [AUTO]
tty5

Offline

Board footer

Powered by FluxBB