Chinaunix首页 | 论坛 | 博客
  • 博客访问: 214576
  • 博文数量: 42
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 420
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-09 10:55
个人简介

每天改变一点点,生活充满了惊喜。

文章分类

全部博文(42)

文章存档

2016年(8)

2015年(29)

2014年(5)

我的朋友

分类: LINUX

2015-11-06 15:15:44

Linux 下可以将一些常用的操作定义为 Shell 函数,在登录时加载,就可以在终端直接以命令行的方式执行,
这样可以提升操作的效率。
直接看代码:

  1. ##! @TODO : 打印scp、rsync命令路径参数
  2. ##! @IN : $1 => 需copy的文件或路径
  3. ##! @OUT : user@hostname:file_path
  4. function genScpPath(){
  5.     log_init "$HOME" "shTls" "" "2"
  6.     local filePath="$1"
  7.     local hostName="$(hostname)"
  8.     local userName="$(whoami)"
  9.    
  10.     if [[ -n "$filePath" ]]
  11.     then
  12.         if [[ -d "$filePath" ]] || [[ -f "$filePath" ]]
  13.         then
  14.             echo "${userName}"'@'"${hostName}"':'"$(readlink -f ${filePath})"
  15.         else
  16.             log_fatal "${BASH_SOURCE[0]-$0}" "${FUNCNAME}" "$filePath' not a real path.'"
  17.         fi
  18.     else
  19.         echo "${userName}"'@'"${hostName}"':'"$(pwd)"
  20.     fi
  21. }

  22. ##! @TODO : 查找类名定义位置
  23. ##! @IN : $1 类名
  24. ##! @IN : $2 查找目录
  25. function grepClass(){
  26.     local className=$1
  27.     shift 1
  28.     local dirPath="$@"
  29.     [ -z "$dirPath" ] && { dirPath=$(pwd); echo -e $(yellowPrint "NOTICE: ")"\c"; greenPrint "search in ${dirPath}"; }
  30.     fgrep -n -w "${className}" -r ${dirPath} | egrep --color "class *${className}"
  31. }

  32. ##! @TODO : 查找动态库文件位置
  33. ##! @IN : 动态库文件名
  34. ##! @OUT : 动态库文件路径
  35. function getSoPath(){
  36.     local soFilePath="$1"

  37.     local soFileDict="/etc/ld.so.cache"
  38.     if [[ -n "$soFileDict" ]]
  39.     then
  40.         # 先查询 LD_LIBRARY_PATH
  41.         find $(echo $LD_LIBRARY_PATH | sed 's/\:/\n/g') -name "*.so.*" | /bin/grep --color "/${soFilePath}"
  42.         strings "$soFileDict" | /bin/grep --color "/${soFilePath}"
  43.     fi
  44. }

  45. ##! @TODO : Linux下查找宏定义
  46. ##! @IN : $1 查找的目录
  47. ##! @IN : $2 要查找的宏
  48. ##! @OUT : 宏所在的文件
  49. function searchMarco(){
  50.     if (($# != 2))
  51.     then
  52.        echo "Fail,need more arguments."
  53.        exit -1
  54.     fi
  55.     local marco=$2
  56.     local find_dir=$1
  57.     find $1 -name *.h 2>/dev/null | while read file_h
  58.     do
  59.         if grep $2 $file_h
  60.         then
  61.             echo $file_h
  62.         fi
  63.     done
  64.     return 0
  65. }

  66. ##! @TODO : 快速备份文件
  67. ##! @IN : $1 待备份文件
  68. ##! @IN : $2 备份目录,可以不设置,则备份至当前目录
  69. function backupFile(){
  70.     local l_file="$1"
  71.     local l_back_dir="$2"
  72.     ! [ -f "$l_file" ] && { echo "Error: file to backup not exist." ; return 1; }
  73.     tar -zcf "${l_file%/}.tar.gz" ${l_file}
  74.     if [ -n "${l_back_dir}" ]
  75.     then
  76.         mkdir -p ${l_back_dir}
  77.         mv "${l_file}.tar.gz" ${l_back_dir}
  78.     fi
  79.     return 0
  80. }

将以上脚本写入文件 shTls.sh,接下来怎样能使上面脚本中定义的函数在终端中作为命令直接调用?
类似这样:
genScpPath shTls.sh

方法一:

在.bashrc 文件中,加上 source shTls.sh,再source .bashrc 立即生效
当然,如果脚本中调用了另一个脚本文件(我们暂且称它为B脚本)中定义的函数,则需要 source 该脚本。
可以在 .bashrc 文件中source,当然这种方式会使B脚本中定义的函数也能在终端中直接用命令行的形式调用。
如果你不想这样,则可以在shTls.sh脚本中source。
注意:shTls.sh脚本中,使用了我在《Shell 脚本 日志库》一文中讲述的日志库,在执行之前,需要将该日志库的代码也要做source操作。

方法二:
用别名的方式,在 .bashrc 文件中加入以下代码,然后source生效。
alias genScpPath='sh /home/sunny/shell/tools/shTls.sh  genScpPath'
类似的,你可以设置其他函数的别名。

另外,还需要在脚本 shTls.sh 最后加入:
  1. ${funcName}=$1
  2. shif 1
  3. ${funcName} $@
经过上面的设置,你就可以执行:
genScpPath shTls.sh

输出:
sunny@hostname:/home/sunny/shell/tools/shTls.sh
Copy 或 在scp、rsync 命令中直接做 命令替换,是不是很方便。

BTW:
你可能想到了,既然都写脚本了,又何必不将以上的安装方法自动化。
哈哈,你一定是跟我一样比较懒的人^_^,能让机器去做的事情,我们当然不必让它闲着。
你可以在 shTls.sh 中定义一个 install 函数,然后以如下的方式去调用:
sh shTls.sh install 
这里就可以看出,上述的方法二会更适合些。
至于怎么实现 install,你可以试试。
_______________________
END
 

阅读(1706) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~