背景
每个人linux开发工程师,都应该有属于自己的bashrc文件,这样会提高开发效率。
什么是bashrc
linux一般有系统级的bashrc(/etc/bash.bashrc)和profile(/etc/profile)以及个人用户的
HOME/.bashrc和
HOME/.profile
这里不再重复描述bashrc和profile的区别,自行去stackoverflow查看。
个人bashrc的一个实践
一般来说,个人需要添加有export, alias, function三种内容,一般有区分的分布在bashrc和profile中。(具体可以参考stackoverflow)
这里给出另一种更实用的实践:
将自己的内容单独出来一个文件:
.bash_yanyongwen
然后在$HOME/.bashrc中引用这个文件。
这样最大的好处就是:你的文件可以纳入代码管理(我放在公司的gitlab中管理),在不同的服务器上可以极快地部署。
同时,linux升级也不会影响你的文件(因为解耦)。
操作
只需要在$HOME/.bashrc中加入
if [ -f "$HOME/.bash_yanyongwen" ]; then
. "$HOME/.bash_yanyongwen"
fi
将.bash_yanyongwen放入$HOME目录即可
bashrc实例
下面是我的实例,已经删除一下敏感的信息
#export
export P=~/Codes/ProjectA-V3/platform
export C=~/Codes/ProjectC-GERRIT/CORE/SOURCE
export D=~/Codes/docker
export WORKSPACE=$P
export DOCKER="docker.my_company.com.cn:5000"
#device ip
export csu_ip=xx
export ci_ip=xx
export wuhan_server_ip=xx
#personal data
export PASSWORD=xx
export jenkins_token=xx
export GERRIT_PASSWORD=xx
export JJW_USERNAME=my_hr_id
export JJW_PASSWORD=${PASSWORD}
#alias
#develop csu
alias copy_key_csu="ssh-copy-id -i ~/.ssh/mania.pub root@$csu_ip"
alias ssh_csu="ssh -i ~/.ssh/mania root@${csu_ip}"
alias sftp_csu="sftp -i ~/.ssh/mania root@${csu_ip}"
alias find_csu_ip=xx
alias sshfs_csu="sshfs root@${csu_ip}:/ ~/csu"
#copy the output at the Uboot, remember to change the ip
alias samba_csu='echo "setenv autoload no && dhcp && setenv serverip ${ci_ip} &&setenv folder "pmsc/latest/" && tftp 80000000 ${folder}debrick.scr && source 80000000"'
alias ssh_wuhan_server="ssh -i ~/.ssh/mania my_name@${wuhan_server_ip}"
alias sftp_wuhan_server="sftp -i ~/.ssh/mania my_name@${wuhan_server_ip}"
#git
alias ssh_agent="eval `ssh-agent -s`"
#docker
alias docker_rm_all_exited="docker ps -a | grep Exit | cut -d ' ' -f 1 | xargs docker rm"
alias docker_pid="docker inspect --format '{{.State.Pid}}'"
alias docker_ip="docker inspect --format '{{ .NetworkSettings.IPAddress }}'"
#alias docker_rm_all_exited="docker rm $(docker ps -f status=exited -q)"
alias svn_add_x="svn propset svn:executable on"
alias Pip='sudo -H pip install -i http://mirrors.my_company.com.cn/pypi/simple --trusted-host mirrors.my_company.com.cn'
alias count_files="ls -lR|grep '^-'|wc -l"
#internet
alias ssh_vpn='ssh -i /home/mania/.ssh/mania power@${vpn_ip}'
alias get_internet_ip='curl http://members.3322.org/dyndns/getip'
function docker_rmi(){
docker rmi $(docker images | grep "$1" | awk '{print $3}')
}
function docker_remote_rmi(){
rmi -u my_hr_id -p ${PASSWORD} -i $1
}
#function
function port_forward(){
iptables -t nat -A PREROUTING -p tcp --dport $1 -j REDIRECT --to-port $2
}
#develop
function rsync_files_csu(){
#$1:local folder or files
#$2:csu remove folder
rsync -av $1 root@${csu_ip}:$2
}
function serial_over_ip(){
#prepare:csu should install socat
#$1:local tty
#$2:csu remote tty
socat -d -d PTY,link=${HOME}/dev/$1,raw,echo=0 SYSTEM:"ssh root@${csu_ip} /usr/bin/socat - -d -d 'PTY,link=/dev/$2,nonblock,raw,echo=0'"
}
#internet
#git
function ssh_add_key(){
ssh_agent
ssh-add ~/.ssh/mania
}
#docker
function docker_open(){
#$1:docker image
docker run --rm --name=mania -i -t --net=host --privileged -v ~:/share $1 bash
}
#svn
function svn_del_unversion_files(){
svn status | grep ^?|awk '{print $2}'|xargs -i rm -r {}
}
function svn_fix_database(){
sqlite3 .svn/wc.db "pragma integrity_check"
sqlite3 .svn/wc.db "reindex nodes"
sqlite3 .svn/wc.db "reindex pristine"
#check again to see whether it is ok or not
sqlite3 .svn/wc.db "pragma integrity_check"
}
#jenkins
function jenkins_del_build(){
curl -u mania:${jenkins_token} -X POST http://${ci_ip}:8080/job/$1/[$2-$3]/doDelete
}
function jenkins_trigger_ccn_job(){
curl -X POST http://${ci_ip}:8080/job/pmsc_ccn_diff/build
--data token=${jenkins_token}
--data-urlencode json='{"parameter":[{"name":"StartDate","value":"$1"}, \
{"name":"EndDate","value":"$2"},{"name":"SvnFolder","value":"esmu/src"}]}'
}
function jenkins_trigger_build(){
curl -X POST http://${ci_ip}:8080/view/ProjectB/job/ProjectB_BUILD/build?token=${jenkins_token}
}
#tools
function find_str(){
#$1 is target_str, $2 is target_folder
grep $1 $2 -r
}
function kill_port(){
#$1:port
pid=`lsof -t -i :$1`
echo $pid
kill -9 $pid
}
function upload_artifacts(){
curl -u my_hr_id:${PASSWORD} -T $1 "http://artifacts.my_company.com.cn/artifactory/pmsc-generic-local/$2"
}
function upload_artifacts_third_party(){
curl -u my_hr_id:${PASSWORD} -T $1 "http://artifacts.my_company.com.cn/artifactory/pmsc-generic-local/third-party/$1"
}
function download_artifacts(){
curl -u my_hr_id:${PASSWORD} -O "http://artifacts.my_company.com.cn/artifactory/pmsc-generic-local/$1"
}
function jenkins-job-export(){
jjwrecker -s http://${ci_ip}:8080 -n $1
}
function jenkins-job-import(){
jenkins-jobs --conf ~/jenkins_jobs.ini update $1
}
function jenkins-job-transfer(){
jenkins-job-export $1
jenkins-job-import output/$1.yml
}
#the implementation refs from https://github.com/jpetazzo/nsenter/blob/master/docker-enter
function docker-enter() {
#if [ -e $(dirname "$0")/nsenter ]; then
#Change for centos bash running
if [ -e $(dirname '$0')/nsenter ]; then
# with boot2docker, nsenter is not in the PATH but it is in the same folder
NSENTER=$(dirname "$0")/nsenter
else
# if nsenter has already been installed with path notified, here will be clarified
NSENTER=$(which nsenter)
#NSENTER=nsenter
fi
[ -z "$NSENTER" ] && echo "WARN Cannot find nsenter" && return
if [ -z "$1" ]; then
echo "Usage: `basename "$0"` CONTAINER [COMMAND [ARG]...]"
echo ""
echo "Enters the Docker CONTAINER and executes the specified COMMAND."
echo "If COMMAND is not specified, runs an interactive shell in CONTAINER."
else
PID=$(docker inspect --format "{{.State.Pid}}" "$1")
if [ -z "$PID" ]; then
echo "WARN Cannot find the given container"
return
fi
shift
OPTS="--target $PID --mount --uts --ipc --net --pid"
if [ -z "$1" ]; then
# No command given.
# Use su to clear all host environment variables except for TERM,
# initialize the environment variables HOME, SHELL, USER, LOGNAME, PATH,
# and start a login shell.
#sudo $NSENTER "$OPTS" su - root
sudo $NSENTER --target $PID --mount --uts --ipc --net --pid su - root
else
# Use env to clear all host environment variables.
sudo $NSENTER --target $PID --mount --uts --ipc --net --pid env -i $@
fi
fi
}
function docker_push(){
remote=${DOCKER}/power/$1
docker tag $1 ${remote}
docker push ${remote}
}
function docker_pull(){
remote=${DOCKER}/power/$1
docker pull ${remote}
docker tag ${remote} $1
}
SSH_ENV="$HOME/.ssh/environment"
function start_agent {
#echo "Initialising new SSH agent..."
/usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
#echo succeeded
chmod 600 "${SSH_ENV}"
. "${SSH_ENV}" > /dev/null
#CHANGE ME
#/usr/bin/ssh-add;
/usr/bin/ssh-add ~/.ssh/mania;
}
# Source SSH settings, if applicable
if [ -f "${SSH_ENV}" ]; then
. "${SSH_ENV}" > /dev/null
#ps ${SSH_AGENT_PID} doesn't work under cywgin
ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
start_agent;
}
else
start_agent;
fi