*NIX Tricks

[shell] Repoint an existing symbolic link (symlink) to a different target

Posted in bash, cli, csh, linux, shell, unix, zsh by kousik on August 29, 2011

$  ln -nsf <TARGET>  <LINK>

Credit: Commandline-fu.

Tagged with: , , ,

[perl] Access shell environmental variables in your Perl script

Posted in perl by kousik on August 26, 2011

The environmental variables are stored in %ENV hash (“associative array”). Now if you want to access a shell variable, say $HOME, in your Perl script, then you need to refer to it as $ENV{'HOME'}:

#!/usr/bin/perl
$my_home_dir = "$ENV{'HOME'}" ;
$my_shell = "$ENV{'SHELL'}" ;
$my_editor = "$ENV{'EDITOR'}" ;

Don’t forget the curly braces and the single quotes!

Reference: Devdaily blog.

[zsh] Histroy substitution

Posted in linux, osx, unix, zsh by kousik on January 29, 2010
#For Previous Command (for comparison)
!-1     repeat whole command
!!      repeat (shortcut)
!:0     command
!^      first parameter
!:1     first parameter
!:1-4   first 4 parameters
!$      last parameter
!*      all parameters
!!:s/bash/zsh (or ^bash^zsh)
!^:t    just file name of first parameter
!$:h    just path of last parameter
!-2$:r  just file name without extension of first parameter

For last but one command
!-2     repeat last but one command
!-2^    first parameter last but one command
!-2$    last parameter last but one command
!-2:2   second parameter of second but last command
!-2:s/bash/zsh substitute bash by zsh in the last but one command


Most of them are applicable for bash as well.

Credit: copied from here.

Tagged with: , ,

[cli] Find out success status of a command in BASH/ CSH

Posted in bash, cli, csh by kousik on November 20, 2009

To know the return value of a command, in BASH run
$ echo $?
and in CSH/ TCSH run
$ echo $status
which will give you the return value (0 or 1) of the last command (the one issued just before the echo).

In both the shells, if the above spits out 0 (zero), it means the previous command was successfully completed, otherwise it’ll give rise to 1 (unsuccessful!) — quite contrary to our concept of 0 being false and 1 being true, eh!

Credit: Oracle Spin.

[unix] Change default login shell

Posted in unix by kousik on October 26, 2009

I just stumbled upon a few ways to change the default login shell to bash:

The easiest one is

$ chsh -s /bin/bash username

On some machines you may have to use ypcsh instead of chsh. The full path of the shell executable must be specified.

But if you have root access you may have the option to use either of the following two as well:

  • $ usermod -s bash username
  • Change it by editing the entry for the user in /etc/passwd file, manually.

If none of the above works, here’s a workaround!

N.B. Make sure that the desired shell is listed in /etc/shells file.

Follow

Get every new post delivered to your Inbox.