无法更改shell脚本中的目录

时间:2018-03-29 02:51:36

标签: linux shell

我正在尝试将shell脚本中的目录更改为命令行参数给出的目录。

#!/bin/bash
chmod 755 findfn.sh
dir=$1

while [ -d "${dir}" ]
do
    cd $HOME/"$1"
done

我看了很多其他帖子并尝试过:

  • cd "dir"
  • cd ~/"$1"
  • cd source "$1"
  • 删除#!bin/bash
  • chmod 755更改为chmod 700

我哪里错了?

EDIT 刚试过这个

alias dirc="cd /home/$1"
    echo $PWD

但它仍然没有改变目录,它只是不断打印出来 “/家”

1 个答案:

答案 0 :(得分:0)

$ cat a.sh
#!/bin/bash
dir=$1

while [ -d "${dir}" ]  #it will check if the argument directory is present where script is executed.
do
    cd $HOME/"$1"  
done

假设/tmp包含上述脚本为a.sh, 并且存在/tmp/abc/home/abc

然后您必须运行如下脚本:

$. a.sh abc

,它将转到/home/abc