Shell脚本使用脚本中的变量更改为另一个目录

时间:2021-09-09 23:22:47

I am trying to write a simple shell script where on running this script it should change to another directory. I want to make it by allocating a variable to the path. vi loc_change

我正在尝试编写一个简单的shell脚本,在运行此脚本时,它应该更改为另一个目录。我想通过为路径分配变量来实现它。 vi loc_change

#!/bin/bash
change= "/home/oracle/Public"
cd $change
echo "directory changed"

after doing this I have changed the permissions chmod 777 loc_change and executing this source ./loc_change after executing this I am getting following error.

执行此操作后,我已经更改了权限chmod 777 loc_change并在执行此操作后执行此源./loc_change我收到以下错误。

./loc_change: line 2: /home/oracle/Public: is a directory
directory changed

I have seen this type of decleration in tldp. I am very new to Shell scripting.

我在tldp中看到过这种类型的decleration。我是Shell脚本的新手。

2 个解决方案

#1


1  

#!/bin/bash
change="/home/oracle/Public"

remove the space

删除空间

try,

尝试,

 #!/bin/bash
 change="/rooot"
 cd $change > /dev/null > 2&>1
 if [ $? -eq 0 ]
 then
 echo "Directory Changed"
 else
 echo "Directory Not changed"
 fi

#2


0  

There should be no space.

应该没有空间。

#!/bin/bash
    change="/home/oracle/Public"
    cd $change
    echo "directory changed"

#1


1  

#!/bin/bash
change="/home/oracle/Public"

remove the space

删除空间

try,

尝试,

 #!/bin/bash
 change="/rooot"
 cd $change > /dev/null > 2&>1
 if [ $? -eq 0 ]
 then
 echo "Directory Changed"
 else
 echo "Directory Not changed"
 fi

#2


0  

There should be no space.

应该没有空间。

#!/bin/bash
    change="/home/oracle/Public"
    cd $change
    echo "directory changed"