various improvements
[distro-setup] / .bashrc
1 # Copyright (C) 2016 Ian Kelling
2
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6
7 # http://www.apache.org/licenses/LICENSE-2.0
8
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # to debug
16 #set -x
17 # redirect output to log file. this doesn't work. todo figure out why
18 #exec 1>>/a/tmp/bashlog
19 #exec 2>>/a/tmp/bashlog
20
21
22 # By default this file is sourced for ALL ssh commands. This is wonky.
23 # Normally, this file is not sourced when a script is run, but we can
24 # override that by having #!/bin/bash -l. I want something similar for ssh
25 # commands. when a local script runs an ssh command, this file should not be
26 # sourced by default, but we should be able to override that.
27 #
28 # So here we test for conditions of a script under ssh and return if so.
29 # And we don't keep the rest of the code in this file, because even
30 # though we return, we already parsed the whole code, and as I develop
31 # the code, the parsing can have errors, which can screw up cronjobs
32 # etc. To test for an overriding condition, we have a few options. one
33 # is to use an environment variable. env variables sent across ssh are
34 # strictly limited. ssh -t which sets $SSH_TTY, but within a script that
35 # won't work because tty allocation will fail. We could override an
36 # obscure unused LC_var, like telephone, but I don't want to run into
37 # some edge case where that messes things up. we could transfer a file
38 # which we could test for, but I can't think of a way to make that
39 # inherently limited to a single ssh command. I choose to set SendEnv
40 # and AcceptEnv ssh config vars to allow the environment variable
41 # BASH_LOGIN_SHELL to propagate across ssh. This also requires that we
42 # wrap ssh in interactive shells, because, once we export the var, it
43 # will go into scripts and theres no way to automatically set it.
44
45
46 # first conditions show that we are an ssh command without an interactive shell
47 if [[ $SSH_CONNECTION ]] \
48 && [[ $- == *c* ]] \
49 && [[ ! $SSH_TTY ]] \
50 && [[ $- != *i* ]] \
51 && [[ $BASH_LOGIN_SHELL != true ]]; then
52 return 0
53 else
54 if [[ -r /etc/profile ]]; then
55 source /etc/profile
56 fi
57 _x=$(readlink -f $BASH_SOURCE)
58 _x=${_x%/*}/brc
59 if [[ -r $_x ]]; then
60 source $_x
61 fi
62 fi
63 # ensure no bad programs appending to this file will have an affect
64 return 0