This are my notes in the fields of computer science and technology. Everything is written with ABSOLUTE NO WARRANTY of fitness for any purpose. Of course, feel free to comment anything.

Showing posts with label shell rc file. Show all posts
Showing posts with label shell rc file. Show all posts

Thursday, December 31, 2009

ssh autocompletion in tcsh

I wrote the following for my .tcshrc to get some autocompletion for ssh.
set hostlist=(`cat ~/.ssh/known_hosts | cut -f 1 -d ' ' | cut -f 1 -d ',' | uniq`)
complete ssh 'p/1/$hostlist/' 'p/2/c/'

Sunday, October 11, 2009

Starting ssh-agent from .tcshrc

While connecting to a server using SSH, you need either to provide a password or to use a public-private key pair to identify yourself. The latter is interesting, as you don't have to write your password each time. A copy of the public key must be saved in the computer acting as ssh server under ".ssh/authorized_keys", the private key stays only on the client computer.

However for security reasons it is strongly reccomended to set a password on the key file. Otherwise anyone who can steal your key somehow (like accessing your computer physically), can steal your identity. But if your key file has a password, then you would have to write the password each time, so it wouldn't be much better than using passwords instead of keys in the first time. Luckily there is a solution: ssh-agent and ssh-add. ssh-agent runs as a deamon and remembers the password to your key. ssh-add allows you to tell ssh-agent the password.

As ssh-agent keep running when you log off your session, you may add some lines to your shell startup script, to test if ssh-agent is running, otherwise to start it and ask you the password (via ssh-add). In the github guides there is a script for bash (). I adapted it to use it in tcsh, which I use as a shell. Here is it (I added these lines at the bottom of my .tcshrc file, only *before* setting the prompt explicitely, as I used it to distinguish interactive sessions from non-interactive ones):

# start ssh agent if not started yet
set SSH_ENV = "$HOME/.ssh/environment"
unset SSH_AGENT_RUNNING
if (-f $SSH_ENV) then
source $SSH_ENV
ps $SSH_AGENT_PID > /dev/null && set SSH_AGENT_RUNNING
endif
if ($?prompt && ! $?SSH_AGENT_RUNNING) then
echo "Initializing SSH agent..."
/usr/bin/ssh-agent | sed 's/^echo/#echo/' >! $SSH_ENV
chmod 600 $SSH_ENV
source $SSH_ENV
echo "done: ssh-agent is now running, PID $SSH_AGENT_PID"
/usr/bin/ssh-add
endif

Saturday, September 12, 2009

Colors in grep

To turn on colors in grep, you may add --color=auto to the GREP_OPTIONS environment variable. The way to do it is of course shell-dependant. E.g. add the following to your shell rc file:
export GREP_OPTIONS='--color=auto'

ls colors

add an alias to your shell rc file:
alias ls "ls --color=auto"

Sunday, August 9, 2009

GIT_PAGER and LESS colors

I was wondering why, working on two different machines, I was able to display git-log and git-diff colors on one machine and not on the other, despite the same git configuration file ~/.gitconfig:

[color]
diff = auto
status = auto
branch = auto
log = auto

and no particular settings regarding colors in <repository>/.git/config

Although I was not using a Mac, the solution was in this blog post: that is, setting the GIT_PAGER variable to cat was already solving the problem; GIT_PAGER sets which program should be used by git to display for example the diff and log output (default less).

The actual problem was in less and how it outputs ANSI color ESC codes. A solution, similarly to what suggested in that post, was to set the LESS variable to -R and leave GIT_PAGER unset, as it originally was. Regarding the other options suggested in the blog post for LESS, -X and -e have no influence on color display, and -R is probably better than -r; in particular, this in an extract of the less manual, at the -r/-R section, explaining why:
-r or --raw-control-chars

Causes "raw" control characters to be displayed.
[...] Warning: when the -r option is used, less cannot
keep track of the actual appearance of the screen
(since this depends on how the screen responds to
each type of control character). Thus, various display
problems may result, such as long lines being split
in the wrong place.

-R or --RAW-CONTROL-CHARS

Like -r, but only ANSI "color" escape sequences are
output in "raw" form. Unlike -r, the screen appearance
is maintained correctly in most cases. [...]

Thursday, October 11, 2007

bash startup

In interactive mode bash executes some startup scripts. First thing to know is if it is a login shell or not a login shell: 

* login shell: (executes more things)
(1) general settings for all users are in: /etc/profile
(2) personal settins may be in: (first one readable)
~/.bash_profile
~/.bash_login
~/.profile
(3) before logout: ~/.bash_logout

* non-login: 
>bash => ~/.bashrc
>bash --norc => nothing
>bash --rcfile filename => specify another rc file

About Me

My photo
Hamburg, Hamburg, Germany
Former molecular biologist and web developer (Rails) and currently research scientist in bioinformatics.