免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2788 | 回复: 1
打印 上一主题 下一主题

[分享]在脚本中使用 alias [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-05-21 09:47 |只看该作者 |倒序浏览
在脚本中是可以使用alias 的,只是bash 在非交互式shell中黙认把alias关闭了,打开就是了

以下是测试代码,bash,zsh,ksh 执行结果一样

  1. sh=$(readlink -f /proc/$/exe)
  2. sh=$(basename $sh)
  3. if [ "$sh" = "bash" ];then
  4.     shopt -s expand_aliases
  5. elif [ "$sh" = "zsh" ];then
  6.     # in my system, this is not needed
  7.     set +o noaliases
  8.     # ksh ,what ?
  9. fi
  10. alias echo='/bin/echo -e'
  11. echo "\n\naaa\n\tbbbbbb"
  12. alias ls='/bin/ls -a'
  13. ls /
  14. alias df='/bin/df -h'
  15. df
复制代码
这是我的shell版本

  1. % bash --version
  2. GNU bash, version 4.0.33(2)-release (i586-mandriva-linux-gnu)
  3. Copyright (C) 2009 Free Software Foundation, Inc.
  4. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

  5. This is free software; you are free to change and redistribute it.
  6. There is NO WARRANTY, to the extent permitted by law.
  7. % zsh --version
  8. zsh 4.3.10 (i586-mandriva-linux-gnu)
  9. % rpm -qf /usr/bin/ksh
  10. pdksh-5.2.14-27mdv2010.0
复制代码

论坛徽章:
0
2 [报告]
发表于 2010-05-21 09:55 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP