2014-04-11 54 views
0
#!/usr/bin/ksh 


if [ $# -ne 1 ]; then 
     echo "[*]\t Please see usage..." 
     echo "[*]\t Usage: $0 <store_number>" 
     exit 1 
fi 


if [ -z "$1" ]; then 
     echo "[*]\t Please see usage..." 
     echo "[*]\t Usage: $0 <store_number>" 
    exit 1 
fi 


Store_Number=$1 
EPS_Directory="/apps/epsadmin_90000"$Store_Number"/EPS" 


cd $EPS_Directory 

我想写一个简单的脚本,将改变我的主目录中的我的目录。 我有工作改变子目录内的目录(如上所示),但显然当脚本完成运行它踢我退到外壳,我回到我的原始目录。ksh - >是否可以在子shell中的父shell(主shell)上运行命令?

是否可以将命令从子shell中传递到外壳?我可以将cd命令传递给外壳吗?

例如,如果我运行:

./cd.sh 2001 

我想我的目录是:

/apps/epsadmin_900002001/EPS 

一旦我回到外壳。

+0

每个* process *(subshel​​l,running script)都有它自己的** *当前目录*。另请参见:[为什么cd不能在bash shell脚本中工作](http://stackoverflow.com/questions/255414/why-doesnt-cd-work-in-a-bash-shell-script) –

回答

0

感谢您的帮助!这是我的解决方案。

# create dj file in /users/(YOUR_NUID) directory 
# paste the dj function into this file. (vi dj) (hit i to enter edit mode) (right click to paste) (hit esc) (type :wq) 
# source the dj file containing dj() functon by adding this to .profile: 
# . $HOME/dj 
# reload .profile by typing . ./.profile 

# then to run the function simply type dj <storenumber> to jump between EPS directory folders. 


dj(){ 

Store_Number=$1 
EPS_Directory="/apps/epsadmin_90000"$Store_Number"/EPS" 

    if [ -e $(echo $EPS_Directory) ]; then 
     cd $EPS_Directory 
     echo "You are now in directory: $EPS_Directory" 
    else 
     echo "Directory $EPS_Directory does not exist." 
    fi 

} 
2

不,这是不可能的。

相反,你可以做一个函数:

mycd() { 
    if [ $# -ne 1 ]; then 
    echo "[*]\t Please see usage..." 
    echo "[*]\t Usage: $0 <store_number>" 
    return 1 
    fi 

    if [ -z "$1" ]; then 
    echo "[*]\t Please see usage..." 
    echo "[*]\t Usage: $0 <store_number>" 
    return 1 
    fi 

    Store_Number=$1 
    EPS_Directory="/apps/epsadmin_90000$Store_Number/EPS" 

    cd "$EPS_Directory" 
} 

...,并将其存储在自己的文件和源是:

. $HOME/.fun/mycd.sh 

壳牌功能,在主进程中运行,不像在子进程中运行的脚本。