shell程序分析--qcd

这是国嵌中的一个小应用,目的在于我们能快速的在各个目录之间进行切换。
参考资料:
1、http://pan.baidu.com/s/1dEd1ZFz linux+命令行+shell脚本编程宝典
2、http://www.linuxidc.com/Linux/2014-03/97826.htm 为了解决shell子进程的环境不能传给父进程(!)
下面我将进行详细的源码分析:
qcde:
首先有这几个地方需要注意:
1、脚本开始第一句,用#!+解析器
2、多处用到类似:[ ! $3 ] || pos=$3
,这个的通用命令为:command1 || command2 [|| command3..]意为,仅在前一个命令输出为false时,执行后一个命令,以此类推。
3、关于重定向 str > $filename ,分清>和>>的区别
4、关于管道 command1 | command2。将前一个命令的输出作为后一个命令的参数。例子有(1)、(2)、(3)等,其中grep处理后得到的是行,awk处理后得到的是列。
5、关于正则表达式,参考上面所给资料
6、子进程不能将环境传给父进程。在(4)、(5)处,#cd "$enter_dir" (4)如果直接用cd 到指定目录时,发现并未反应,原因是,这是所在父进程的一个子进程shell,子进程中是cd到指定目录了,但由于父进程不能继承子进程的环境,所以,父进程的环境依然没有改变。转而,我们先将enter_dir存在QD中。
7、对于问题6怎么解决了,想到了两个思路:既然在执行脚本时,默认会新建一个子进程shell来执行脚本 那我用source file.sh,或者. file.sh不就行了?我试过,直接这样是有问题的,原因大概是,里面涉及到孙进程等。参考2中给了一个解决方法,即给命令,命个别名。alias cdd=’cdd(){ >/tmp/qcd_temp;qcde;if [ $? -eq 0 ];then cd cat /tmp/qcd_temp;fi;};cdd’,然后将这个命令加入到/root/.bashrc中。就OK了

若想使它永久生效,只要将其复制到/root/.bash_alias文件中即可

#!/bin/sh
#Quick enter your directory. 
#qcd version is 1.00.
#Writen by xiewei 2004. 

setup_content=/etc/qcd
history_dir=$setup_content/history_dir

usage()
{
    echo "qcd(quick cd) version 1.01 command :"
    echo "<qcd -s dir [position]>  add a directory to $history_dir."
    echo "if content = ./, then add current directory to  $history_dir."
    echo "if position(1-20) is not, default value equal 1."
    echo ""
    echo "<qcd -d [position]> then delete a directory from $history_dir."
    echo "if position is not, default value equal last."
    echo ""    
    echo "<qcd -l [position]> then list directory from $history_dir."
    echo  "if position is not, default value equal 1, else list all directory,
          then choice one position's content and enter it."
    echo ""   
    echo "<qcd -e [position]> then enter a directory from $history_dir."
    echo "if position is not, default value equal 1."
    echo ""    
    echo "<qcd -c> then clear $history_dir."
    exit 1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值