#!/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
.

一旦我返回外壳。

有帮助吗?

解决方案 2

谢谢你的所有帮助!这是我的解决方案。

#   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 

}
.

其他提示

否,这是不可能的。

相反,您可以进行函数:

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函数在主要过程中运行,与在子过程中运行的脚本不同。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top