linux Shell入门:掌握Linux,OS X,Unix的Shell环境

2019-09-23 09:27:24于丽

下面是一些额外的OS X Unix bash shell别名:

# 从bash打开桌面应用
alias preview="open -a '$PREVIEW'"
alias safari="open -a safari"
alias firefox="open -a firefox"
alias chrome="open -a google chrome"
alias f='open -a Finder '
 
# 清理那些.DS_Store文件
alias dsclean='find . -type f -name .DS_Store -delete'

#8: 寡人好色

# 彩色的grep输出 
alias grep='grep --color=auto'
export GREP_COLOR='1;33'
 
# 彩色的ls
export LSCOLORS='Gxfxcxdxdxegedabagacad'
# Gnu/linux的ls
ls='ls --color=auto'
 
# BSD/os x的ls命令
# alias ls='ls -G'

#9: 设定自己喜好的bash函数

# 在屏幕上显示10个最近的历史命令
function ht {
 history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head
}
 
# host和ping命令的替代
# 接受http:// 或 https:// 或 ftps:// 名称用作域或主机名
_getdomainnameonly(){
  local h="$1"
  local f="${h,,}"
  # remove protocol part of hostname
    f="${f#http://}"
    f="${f#https://}"
  f="${f#ftp://}"
  f="${f#scp://}"
  f="${f#scp://}"
  f="${f#sftp://}"
  # remove username and/or username:password part of hostname
  f="${f#*:*@}"
  f="${f#*@}"
  # remove all /foo/xyz.html* 
  f=${f%%/*}
  # show domain name only
  echo "$f"
}
 
 
ping(){
  local array=( $@ )     # get all args in an array
  local len=${#array[@]}     # find the length of an array
  local host=${array[$len-1]}   # get the last arg
  local args=${array[@]:0:$len-1} # get all args before the last arg in $@ in an array 
  local _ping="/bin/ping"
  local c=$(_getdomainnameonly "$host")
  [ "$t" != "$c" ] && echo "Sending ICMP ECHO_REQUEST to "$c"..."
  # pass args and host
  $_ping $args $c
}
 
host(){
  local array=( $@ )
  local len=${#array[@]}
  local host=${array[$len-1]}
  local args=${array[@]:0:$len-1}
  local _host="/usr/bin/host"
  local c=$(_getdomainnameonly "$host")
  [ "$t" != "$c" ] && echo "Performing DNS lookups for "$c"..."
  $_host $args $c
}

#10: 通过shell shopt命令设定bash shell行为

最后,你可以使用set和shopt命令调整bash shell环境:

# 目录拼写纠正
shopt -q -s cdspell
 
# 保证每次终端窗口改变大小后会更新显示
shopt -q -s checkwinsize
 
# 打开高级模式匹配功能
shopt -q -s extglob
 
# 退出时附加命令历史而不是覆盖
shopt -s histappend
 
# 在命令历史使用多行
shopt -q -s cmdhist
 
# 在后台任务结束时立刻通知
set -o notify
 
# 禁用[CTRL-D]来结束shell
set -o ignoreeof

总结

这个帖子不难理解。它简短地将如何定制用户环境从头介绍了一下。要深入了解bash/ksh/zsh/csh/tcsh/的能力,我建议你用下面的命令阅读man文档:

man bash
man zsh
man tcsh
man ksh

这篇文章由Aadrika T. J.贡献;由admin编辑并增加了额外内容。你也可以为nixCraft做出贡献。