[cli] Find out success status of a command in BASH/ CSH
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
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/passwdfile, 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.
[awk] Add another column from a second file
The problem: I have two files, file-1 and file-2, each of which has two columns; and I want to add the second column of the second file as the third column in the first file.
Let’s say the following is the content of file-1:
A 1
B 2
C 3
and that of file-2:
D 4
E 5
F 6
and I want to have something like this in file-3
A 1 4
B 2 5
C 3 6
Of course, the actual data are not as simple as the above!
Solution:
$ awk '{str1=$1; str2=$2; getline < "file-2"; print str1" \t "str2" \t "$2 > "file-3"}' file-1
I inserted the tab characters (\t) just to make file-3 look nice (scientists don’t care about white spaces, do they?)!
Reference: here.
[bash] SSH to remote servers accessible only through a central server
We have several Red hat servers node001 through node065 which are only accessible from outside via a central server, central.com. I can think of three different solutions to connect to a node, say node010, via ssh:
1. First SSH to central.com then to node010
2. Forward port 22 of node010 to local machine’s port 2200 via central.com
$ ssh kousik@central.com -L 2200:node010:22
Then ssh to localhost’s port 2200
$ ssh localhost -p 2200
3. $ ssh -YC -t kousik@central.com ssh -YC kousik@node$num
It’s the last one that caught my attention recently — so much so that I put a whole function in the ~/.bash_aliases on my local machine:
#
# A function to ssh to a node: put this in the ~/.bash_aliases file
# The input is the node number
# Example: to ssh to node010 type go2 followed by either 10, 0010 or 0000010 .....
# ..... which is `arithmetically' equal to 10.
#
go2() { num="$*";
echo "you entered '"$num"' ";
num="${num#[Nn]ode}"; # Ignore "node", if entered, and consider the rest
if [ "$num" -ge 1 2>& /dev/null ] && [ "$num" -le 65 2>&/dev/null ]; then
num=`echo $num|bc` # for correct arithmetic conversion to a decimal integer
if [ $num -lt 10 ]; then
num="00$num";
else
num="0$num";
fi;
echo "Logging on to node$num in 1s";
sleep 1;
ssh -YC -t kousik@central.com ssh -YC kousik@node$num;
else
echo "Please enter a valid integer (1--65)"
fi;
}
The first conditional statement checks to see if the input is really an integer and is within the correct range: 0-65. The redirections of STDERR to
/dev/null are there to suppress appearance of error messages due to non-integer inputs. The reason for using bc for correct arithmetic expansion may be clear from my comment here.
[bash] Directory stack and tilde expansion
First, some basics about directory stack:
1. Show directory stack with indices:
$ dirs -v
The top directory, i.e. the one with index=0, is the current working directory. On the other hand,
$ dirs [-N | +N]
where -N (+N) will show the Nth directory from bottom (top) where the entry at bottom (top) has index=0.
2. Add a directory, dir, to the stack
$ pushd -n dir
which puts dir (index=1) just below the current working directory (index=0) but above all others in the stack. If the -n is missing, pushd will change the working directory to dir thus causing dir to have index 0 and all other directories to be pushed down the stack.
3. Go to the Nth directory from the top without changing the relative positions of the entries in the stack:
$ pushd +N
“rolls up” the stack to bring the directory with index N on top so that now it has index 0 (the indices of other directories in the stack change accordingly so as to keep the relative positions fixed).
4. Effect of cd: substitute the top directory with another_directory:
$ cd another_directory
will replace the top direcotry (index=0) with another_direcotory, as well as you’ll be taken to another_directory.
5. Remove an entry from the directory stack:
$ popd +N
will remove directory with index N counting from top (first is always 0) from the stack. If you remove the top directory (i.e. the working directory, i.e. the one with index 0) you’ll be automatically taken to the next directory in the stack, whose index then will become 0.
Tilde Expansion [link]
$ echo ~N # same as: echo `dirs +N`
$ echo ~+N # same as: echo `dirs +N`
$ echo ~-N # same as: echo `dirs -N`
Example
$ cd ~N
will replace the top directory with the Nth directory (without any other change in the stack). The same effect may be invoked by
$ pushd -n +N

leave a comment