困惑 紧跟指令mv或cp的cd指令无效--创建目录后自动进入相应目录

细节:
这几天写oracle安装脚本的时候遇到个棘手的问题:

mkdir -p /u01/oinstall
cp -R  oracleInstall /u01/oinstall
#mv  oracleInstall /u01/oinstall
#sleep 3
cd /u01/oinstall/oracleInstall
chmod 755 *.sh

上述shell片段中, 使用 cp -R 或 mv 指令拷贝(移动)相关文件到 /u01/oinstall 目录, 然后想马上切换到该目录, 可结果始终无法切换过去。

拷贝的文件也就50M不到。
一开始我怀疑是由于文件太大了,相关的目录还没有创建出来, 于是在两条指令之间 sleep了下, 结果还是不行。。

困惑ing....


============================================================================
更新:
没想到这确实是一个很难的问题, 和 shell的子环境有关。

不过感谢Google, 找到解决办法了, 参考:
http://charlescurley.com/blog/articles/mkcd_mkdir_foo____cd_foo/index.html

使用方法:
1.创建脚本
# vi mkcd 
内容如下:
-----------------------------------------------------------------------------------------------------
# -*- shell-script -*-
#
mkcd: a script which declares a function, mkcd.

#
Time-stamp: <2002-09-03 13:27:08 root mkcd>

#
mkcd creates a directory hierarchy, then cds into it all in one
#
swell foop. To use it in your shell, run it as:

#
. /path/mkcd

#
That will execute it without launching a new shell, so that it
#
changes your current shell's environment.

#
You can then use the function in scripts or from the terminal to
#
create a new directory or directory hierarchy, and cd into it. E.g:

#
mkcd foo/bar/baz

function mkcd
{
dir=$1;
mkdir -p $dir && cd $dir;
}
-----------------------------------------------------------------------------------------------------

2. 使脚本生效
# chmod 755 mkcd
# . /root/test/mkcd
说明:
格式为:  点号 空格 脚本完整路径

3. 使用方法
现在可以直接使用如下指令创建目录了(目录创建后会自动进入刚创建的目录):
# mkdir /test/test2/test2
# pwd
/test/test2/test2
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值