Posts

Showing posts with the label bash

Funny BASH snippets

echo howdy /bin/echo howdy /bin/../bin/echo howdy /bin/echo* howdy cd /bin echo* howdy *echo howdy ec*ho howdy ech? howdy ??h? howdy ech[o] howdy cd /tmp ec${howdy}ho howdy echo$howdy howdy ec''ho howdy ec""ho howdy 'ec'ho howdy "ec"ho howdy 'echo' howdy "echo" howdy ech\o howdy ec``ho howdy ec$()ho howdy ec`true`ho howdy ec$(true)ho howdy $yay echo howdy `` echo howdy `true` echo howdy `echo echo` howdy :&echo howdy if echo howdy;then :;else echo howdy;fi ! echo howdy { echo howdy ;} (echo howdy) eval echo howdy xargs echo howdy < /dev/null sh -c 'echo howdy' bash -c 'echo howdy' ssh localhost 'echo howdy' cd /tmp printf "all:\n\techo howdy" > Makefile make cd /tmp echo echo howdy > bash chmod +x bash ./bash cd /tmp cp /bin/bash bash echo echo howdy > bash ./bash . ./bash bash bash PATH=. bash alias howdy='echo howdy...

My programming language usage

In recent months, I've been developing Python (Geany, Netbeans), BASH (gedit, Geany) and a bit of HTML/Javascript (Netbeans, Geany) for work, Java (Eclipse) for coursework, Erlang (Emacs, sometimes Geany) and a bit of Emacs LISP (Geany) for labwork, and Haskell (Geany) for my own needs. I have also refreshed my Logo (Geany) skills recently to refactor two of my homeworks. I will move to C++/Qt/FFmpeg/SQLite (Qt Creator) again in the upcoming days for some time. I have dug into Gnome's Genie (and Vala) and will write a short overview of the language soon. That's 8-10 languages at my fingertips, but a few more aren't deep down below either, like C (you never forget the horror), Ada (hard to forget the beauty), Pascal (need to find a way to convert to Ada), Eiffel (have seen it, but no professional coding done in it yet). Sadly, I'm not allowed to share my Python code, and some of my more interesting BASH scripts. You also can't enjoy some unpublished parts of Refa...

Sorted and aggregated disk usage script

I've been asked for this script many times lately, so I'll publish it. It lists the disk usage of subdirectories in a given directory in descending order, right justified and suffixed with kilobytes or megabytes appropriately. #!/bin/sh display() {  local DIR=$1  du -sk $DIR/..?* $DIR/.[!.]* $DIR/* 2>/dev/null |   sort -n |   awk '    {     size = $1;     sum += size;     name = $2;     if ($1 < 1024){      printf("%9ik %s\n",size,name);     } else {      printf("%9.3fM %s\n",size/1024,name);     }    }    END{     print sum " kiB"    }   ' } main() {  local DIR=$1  if [ "$DIR" = "" ]  then   display $HOME   quota  else   display $DIR  fi } m...

Converting between Bird-literate and plain Haskell

Literate programming is a good idea, and there exist many creative ways in which you could take advantage of it, like one described in Literate Haskell with Markdown and Syntax Highlighting . However, if you're using the text editor Geany version 0.18, you will be faced with a bug that makes it unable to interpret Bird-style literate source. As a simple fix, I provide two programs to convert between simple Bird-literate and non-literate source code. You could also convert your Bird-literate to TeX-style . # bird_lhs2hs sed '   s/^>//   t   s/^ *$//   t   s/^/-- /  ' in.lhs > out.hs # hs2bird_lhs sed '   s/^-- //   t   s/^ *$//   t   s/^/> /  ' in.hs > out.lhs

Unary math Towers of Hanoi in naked (D)ASH and BASH

The following solution assumes neither BASH extensions, nor any external executables. Hence it could be ran from a preboot environment like a frugal initrd. A slight compromise was to use unary math. Did you know, that you can access an animated version from Emacs by typing M-x hanoi ? pop(){  shift  echo $* } top(){  echo $1 } print_towers(){  echo "a=[" $a "], b=[" $b "], c=[" $c "]" } hanoi(){  [ "$n" ] || return  local ln  ln=$(pop $n)  n=$ln  hanoi $1 $3 $2  print_towers  echo "$1 -> $2"  eval $2=\"$(eval top \$$1) $(echo \$$2)\"  eval $1=\"$(eval pop \$$1)\"  n=$ln  hanoi $3 $2 $1 } main(){  a="1 2 3 4 5"  b=  c=  n=$a  hanoi a b c  print_towers } main "$@"

Nice and clean bash scripting for dialyzer dependencies

edit2: New escaper run. edit: Backslash is fixed now. I made a nice script that checks an Erlang source tree for base library dependencies. I needed this in order to speed up the system library PLT building analysis phase of the Dialyzer on my old computer. The trivially implemented algorithm is only an approximation based on sed-processed program text, but according to my review it's should almost never generate a smaller set of packages than a proper parser. Note that even a proper parser would have problems with the pathological cases which this simple one would fail on. The style is, however, much more interesting if you take a look at the attached code. What do you think about it? #!/bin/dash ##### library code ### # unpure! debug(){  [ -n "$NDEBUG" ] &&   echo "DEBUG:" "$@" >> $LOG } # unpure! warning(){  echo "warning:" "$@" >> $LOG } # unpure! error(){  echo "error: ${1}!" >&2  sleep 1  ...

Chopping a long string into smaller ones toy

I made a great tool I can't do without from now on! ;-D Update#1: added null string checking, renamed "i" to "s". Update#2: upgraded output format, added BASH version! Here's the Haskell source code: import System.Environment(getArgs) import Data.List(unfoldr,intersperse) chop k | k>0 = unfoldr f where  f [] = Nothing  f s  = Just $ splitAt k s main = do  [k,s@(_:_)] <- getArgs  let out = show . concat . intersperse " "      res = chop (read k) s  putStrLn $ "filter(/=' ')" ++ out res Typing: ./chop.hs 27 http://bkil.blogspot.com/2009/05/chopping-long-string-into-smaller-ones.html Gives: filter(/=' ')"http://bkil.blogspot.com/20 09/05/chopping-long-string- into-smaller-ones.html" Or if you prefer, you could alternatively opt for the following BASH routine: main(){  printf 'echo "'  printf "$2"|sed -r "s~.{$1}~& ~g"  echo '"|sed "s~ ~~g"' } This one...

Functional programming in SH and Bash?!

I made a few functional oriented and list processing functions for bash. Nothing serious, and it's very slow for practical use. Profiling tells that the head and tail operators are the bottleneck, so forking them off to a different process would help. Though I don't feel an urge to update it that way, as this was made just for the fun of it! Another upgrade would involve updating the free form recursive data structure to something faster, or doing less and/or faster conversion. A last resort would be writing hard-coded implementation for the core functions like map, foldl and filter. I also have the choice of moving to the very powerful (and speedy) Bash syntax. These are all viable, but would degrade the elegance of the library as a whole, so I'm hesitant to apply them. And don't forget, that it's a much more sane solution to embed O/S functions, or even a full-blown bash into a fully capable language, instead of the other way around. I've been thinking along t...