This question already has an answer here:
这个问题在这里已有答案:
- Why doesn't “cd” work in a shell script? 29 answers
为什么“cd”不能在shell脚本中工作? 29个答案
Hi this is my first time writing bash and I'm trying to practice creating a bash script and am attempting to do the following:
嗨,这是我第一次写bash,我正在尝试创建一个bash脚本,并尝试执行以下操作:
- Using absolute path the script changes directory to your home directory.
- Next it verifies whether a subdirectory called project exists. If it does not exist it creates that directory.
- Next it changes directory moving into the project subdirectory using relative path.
使用绝对路径,脚本将目录更改为您的主目录。
接下来,它验证是否存在名为project的子目录。如果它不存在,则创建该目录。
接下来,它使用相对路径更改目录移动到项目子目录中。
I'm already stuck on the first step:
我已经陷入了第一步:
#!/bin/bash
cd
if [ -d ./project ]
then
echo hi it exists!
else
echo it doesn't exist!
cd project/
I would appreciate if someone could help! thank you so much!
如果有人可以帮忙,我将不胜感激!非常感谢!
2 个解决方案
#1
-1
You can use the $HOME
environment variable. Bash also recognizes a tilde (~
) as home.
您可以使用$ HOME环境变量。 Bash还将波浪号(〜)识别为家。
#2
-1
cd by itself changes the current working directory to the home directory. See man cd for other uses of this command.
cd本身将当前工作目录更改为主目录。有关此命令的其他用法,请参阅man cd。
#1
-1
You can use the $HOME
environment variable. Bash also recognizes a tilde (~
) as home.
您可以使用$ HOME环境变量。 Bash还将波浪号(〜)识别为家。
#2
-1
cd by itself changes the current working directory to the home directory. See man cd for other uses of this command.
cd本身将当前工作目录更改为主目录。有关此命令的其他用法,请参阅man cd。