A bit of shell redirection

May 10, 2009 by · 1 Comment
Filed under: technology 

Here is how we normally do shell redirection
$ ./pgm.sh args >out.txt 2>err.txt
I wanted to modify it a bit and run as follows
$ ./pgm.sh args
with the requirement that  output and error should go to some filename computed inside pgm.sh based on args. One illustrative case could be when date is part of args. So you would like stdout to go to say /your/directory/pgm_out_YYYYMMDD.txt 1

The problem with standard way of redirecting N>file.txt i.e, associating file descriptor N to file.txt , is that it works only for the newly forked process and not for the current process.
$ echo hi 1>out.txt ; echo hii will send hi to out.txt but will print hii to stdout.2

This is where exec comes to our rescue. If we add  exec 1>somefile.txt then output from rest of the script will go to somefile.txt

$ cat test.sh
#!/usr/bin/env bash
exec 1>out.txt
echo hi
echo hii
$./test.sh will redirect hi as well as hii to out.txt

Similarly to redirect stdout as well as stderr we’ll do something like this
cat test2.sh
exec 1>out.txt
exec 2>err.txt
echo out text
echo 1>&2 err text
somenoneexitent command
ls -ld /tmp

Now coming back to original point of redirecting to some file from inside the shell, let’s say program computed the filename in some variable  OUTFILE, we could have just done exec 1>$OUTFILE

That solves the current problem. But you may like to go through following example which achieves ‘random access’ of file in shell script. Example is from here
echo 1234567890 > File # Write string to "File".
exec 3<> File # Open "File" and assign fd 3 to it.
read -n 4 <&3 # Read only 4 characters.
echo -n . >&3 # Write a decimal point there.
exec 3>&- # Close fd 3.
cat File # ==> 1234.67890

With comments, this code is self explanatory.

1 It can also be done by $ ./pgm.sh args >pgm_out`date +%Y%m%d` but idea is to generate this file name based on some logic in program itself.
2 1 in 1>out.txt is redundant but it clarifies here that we are redirecting fd 1

Sony VAIO review

April 29, 2009 by · 6 Comments
Filed under: reviews 

A month back I  bought a vaio NS25G and have been planning to write this post since then. This post is  primarily notes to self and review of the same. Let’s get off the mark quickly –

Post has two parts –

I. Review of hardware
II. Installing debian and things to setup

I. Review of hardware

Since I am not a VAIO marketing guy,  I’ll start other way round –

Why you may not like to buy this laptop :

  1. No bluetooth : believe me, in 2009 sony sells laptop without bluetooth. Sure you can use usb stuff but …
  2. Screen size is 15.4 inches :  if you travel a lot then this laptop may not be very handy and also 2.9kg laptop is not exactly the lightest around.
  3. No HDMI output.
  4. No  separate graphics card and the likes.(and no dedicated video memory

Now , why you may like to buy it :

  1. This laptop is a damn good desktop. What I mean is this if your requirement is like me – mostly writing programs and trying to use it 25 out of 24 hrs, then with a big screen , extremely comfortable keyboard layout with spacious palm rest and good compute power , it makes a very good combination.
  2. It L2 cache is 2MB and processor/memory system bus are 800MHz. I don’t play games so can’t say about that but for anything else but a core 2 duo processor with 2GHz freq is good, this is more than enough.
  3. From my usage, I see processor mostly clocks at 1.2GHz, and memory usage ~500MB. That just reinforces previous point.
  4. Battery backup is good, ~2  hrs is what I get.

Just one more thing here – being  Sony,  it will cost you a bit than other laptops with similar configuration,  but that is a call u have to make.

II. Configuring linux (to run out of box ! ) :
It came pre installed with Vista( I think home premium or some fancy name like that). Dungara booted once and created another partition of 150 GB.  Debian testing is what I always install and then up(down?)grade to ‘unstable’ sid.

Things which worked without any problem:

  1. X –  unsurprisingly X server had not problem with starting or resolution.  After one of the upgrades X refused to start though,  but  ‘X -configure’ generated a conf file which worked pretty well.
  2. Audio –  Audio output and external mic work fine. kernel version 2.6.29-1-686 #1 SMP and alsa version  1.0.17. This is what you get from
    $ lspci  -v |grep Audio 00:1b.0 Audio device: Intel Corporation 82801I (ICH9 Family) HD Audio Con1.0.17troller (rev 03)
  3. Video cam – just works
  4. Hibernate to disk and back – no problem
  5. Wireless –  in short  – just works.
    $ lspci |grep -i wireless
    03:13:55 04:00.0 Network controller: Atheros Communications Inc. AR928X Wireless Network Adapter (PCI-Express) (rev 01) .

    I think kernel 2.6..27 onwards it works with the stock kernel source. You dont have to install ndiswrapper or things like t
    hat. As a note, please say good buy gnome-network-manager and welcome wicd, it is just a gui layer but behind the scene they have fixed lot of config stuff like wpasupplient and more.

What doesn’t work –

  1. Somehow internal mic refuses to work :(  External mic works fine though.  Latest alsa sources did not compile cleanly on debian, so for the  time being  I bought external Internal mic, will try to dig into it sometime in future though.

Few more misc hints for deiban:

  • Package for devnagri font- ttf-indic-fonts
  • Package for flash player – flashplugin-nonfree
  • Entry in sources.list for mplayer and likes – deb http://www.debian-multimedia.org sid main
  • Entry in sources.list for e17 – deb http://debian.alphagemini.org/ unstable main
  • Entry in sources.list for skype – http://download.skype.com/linux/repos/debian/ stable non-free

oneliner for increasing and decreasing the brightness –

[root]prashant@vaio09:12 PM$ cat bin/incbrightness.sh
#!/usr/bin/env bash
echo $[1+`cat /sys/class/backlight/acpi_video0/brightness `] >/sys/class/backlight/acpi_video0/brightness

you’ll have to put this in sudo though.
and to decrease change + to – and … you get the idea, right !

A bit more, if you use enlightenment(e17) here are few more hints –

[enlightenment key bindidings]

You can use following key code and mapping

mute :
KEY="Keycode-160"   ACTION="exec" PARAMS="/usr/bin/amixer -q set Master toggle"
hibernate :
KEY="Keycode-165"   ACTION="exec" PARAMS="sudo /usr/sbin/hibernate-disk"
volume decrease :
KEY="Keycode-174"   ACTION="exec" PARAMS="/usr/bin/amixer -q set Master 2-"
volume increase:
KEY="Keycode-176"   ACTION="exec" PARAMS="/usr/bin/amixer -q set Master 2+"
increase brightness :
KEY="Keycode-212"   ACTION="exec" PARAMS="sudo /root/bin/incbrightness.sh"
decrease brightness
KEY="Keycode-101" ACTION="exec" PARAMS="sudo /root/bin/decbrightness.sh"

  • Recent Posts