Category

scripts

  • 02.22.11
    Learning to write (GNU) makefiles automated, scripts, shell | pfortuny | (0)
    I am a Mathematician and usually write more LaTeX than C but programming has been an active hobby since I owned my first Spectrum back in the eighties. What I am commenting in this post has been plaguing me for years and I have just learnt it (from laziness, no more): using pattern rules in makefiles. Caveat: I do not know whether this works for non-GNU Makes or not. I had already read a lot of pro makefiles, which contained endless funny codes like $@, $%, $< etc. Today I have learnt the meaning of some of these. I am supervising a ...
  • 02.18.11
    Moving between encodings with emacs automated, scripts, shell | pfortuny | (0)
    I live in Spain. I work at Academia. This is a problem. My School's computers are plagued with Windows (XP!), my pupils most certainly use Windows (I bet it is 7) and I use (obviously) an MBPro with Snow Leopard. Even more, my School's servers are Linux. This gives rise to latin-1, UTF-8 collisions. While writing some text-only documents for use with MATLAB (another problem, by the way) I realized I was going to need to convert them from my Mac's UTF-8 to Windows latin-1 every time I edited them. Time for a makefile. I am not commenting this though, only the ...
  • 05.20.10
    echo -n woes scripts, shell | pfortuny | (2)
    It took me quite a while to realize that the following line does not do what you think it does: $ echo '$1$CSmo96nX$G0PL/Cs/of5qDN2vMnyHp0' | openssl base64 | tr -d '\n' You should always use the -n option if you want to make sure there is no spurious trailing newline: $ echo -n '$1$CSmo96nX$G0PL/Cs/of5qDN2vMnyHp0' | openssl base64 | tr -d '\n' (By the way, the encrypted message says just 'patata0' and it is not my password). Or... is it? Tested on two Linux systems (Fedora & Ubuntu) and one Snow Leopard.
  • 04.07.10
    Setting a Timer scripts | rafacas | (3)
    Sometimes I need a timer to focus on something and to alert me when to stop. Remember, we are real commandliners, so we do not want those fancy applications with a lot of features, we need a script ;-) so here it is: #!/bin/bash usage() { name=`basename $0` echo "Usage: $name hh:mm:ss" echo "Example: $name \"00:15:30\"" } if [ $# != 1 ] then usage exit fi IFS=: set -- $* secs=$(( ${1#0} * 3600 + ${2#0} * 60 + ${3#0} )) while [ $secs -gt 0 ] do sleep 1 & printf "\r%02d:%02d:%02d" ...
  • 03.12.09
    nohup: put your computer to work and leave cmd, scripts | pfortuny | (0)
    $ nohup R -q -f batch & That was how I made my Department's computer run a lot of computations in R from a remote shell, while being able to log out without killing the process (it runs on even if you log out). You might redirect the output somewhere (it gets saved in a file called nohup.out by default). Nohup stands for "Do not comply with HUP signals".
  • 03.06.09
    Accents everywhere (to be removed, obviously) scripts, shell | pfortuny | (2)
    Accents have again (I still live in Spain and do some work for the Spanish Administration) crept into my terminal. This time it was a group of University professors which had to create a lot of files and directories concerning a historical catalog and even though I remember telling the coordinator not to use accents or spaces or any funny characters, they did it. I should have known better... The problem was then to take away all spaces and non-ASCII characters from directory and file names. After thinking about it a bit, I came up with the solution below. There may be ...
  • 02.22.09
    Perl directory traversal scripts | pfortuny | (0)
    The following chunk of code sub traverse { my ($dir, $callback) = @_; my $present; opendir $present, $dir or return(); for (grep { ! /^(\.|\.\.)$/ } readdir $present) { my $path = "$dir/$_"; &$callback($path); traverse($path, $callback) if -d $path; } closedir $present; return(); } is an easy implementation (without error-checking) of a generic directory-traversing function in Perl. It should be called traverse(dir, ...
  • 02.16.09
    Perl for accents and spaces in html hrefs automated, scripts, shell | pfortuny | (0)
    Despite the title looking like spam, it uses all the relevant keywords in a problem which I guess is more common than it looks like, especially in non-English speaking countries. I had created (automatically, not by hand) a set of html, pdf and rtf files of the 477 items of a regional catalogue of industrial estate (sorry, no references because it belongs to other people and they have not published it yet). Then I made the index.html page listing and linking to all those files. It looked like a list of lines like the following one (notice that what follows looks ...
  • 02.14.09
    Is your site up? scripts | rafacas | (0)
    When I cannot reach one of my sites, I do not know if it is my problem, my ISP's or my hosting provider's. This is why I use the service down for everyone or just me. I have written a simple script which uses that service. Its usage is: $ ./is_up.sh commandliners.com commandliners.com is up and the script is: #!/bin/bash # is_up.sh # 2009 - Rafa Casado - rafacas@commandliners.com numargs=$# if [ $numargs -ne 1 ] then echo "Usage: $0 domain" echo "Ex: $0 commandliners.com" exit 1 fi DOMAIN=$1 IS_UP=`wget -nv -O ...
  • 02.10.09
    Some xargs scripts, shell | pfortuny | (1)
    Rafacas has already mentioned it, but xargs is sometimes much more useful than what it looks like. Two examples come to mind: Way too many files for rm or ls. It may well happen that a script has generated more than 10000 files in the same directory (it was your friend, not you, I know). If you try and rm * in there, you will be in trouble. Ditto if you simply want to count them with ls | wc -l. However, the following works: $ find . -type f -print0 | xargs -0 ls | wc -l (or rm instead of ...