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

我正在尝试编写一个简单的脚本来更改我的主 shell 中的目录。我让它在子外壳中更改目录(如上所示),但显然当脚本运行完成时,它会将我踢回外壳,我又回到原来的目录中。

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

例如,如果我运行:

./cd.sh 2001

我希望我的目录是:

/apps/epsadmin_900002001/EPS

一旦我回到外壳。

4

2 回答 2

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

Shell 函数在主进程中运行,与在子进程中运行的脚本不同。

于 2014-04-11T23:09:52.173 回答
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 

}
于 2014-04-16T16:24:39.913 回答