如何使BASH脚本仅在特定目录中工作?

我的Linux作业要求我编写一个只在用户位于〜/ tareas / sesion_3时运行的脚本,所以我假设他首先需要输入cd /〜/ tareas / sesion_3然后脚本命令才会运行,如果没有的话回声“你不在/〜/ tareas / sesion_3”。在脚本中我需要创建更多目录,并且只能在该位置创建它们。 我怎么能做出这样的条件? 我感谢你们提供的每一点帮助!     
已邀请:
您可以使用
$PWD
查看脚本运行的父目录,尽管它已经扩展了
~
。所以你可以这样做:
if [[ "$PWD" == "/home/tareas/session_3" ]]; then
  # do stuff if true
else
  # do stuff if false
fi
    
我的回答是:
#!/bin/sh

TARGET_DIR = "~/tareas/sesion_3" 

function do_something(){
    #do something
}

function do_something_v2(){
    #create some dirs
}

if [ `pwd` == "$TARGET_DIR" ] ; then
    do_something
else 
    do_something_v2
我希望它可以帮助你 ^ _ ^     
如果您需要查看您是否至少在给定目录内,但可能在其中的子目录中,
grep
是一个好朋友:
echo `pwd` | grep ^/starting/directory >/dev/null || {
       echo "You aren't in the proper place .."
       exit 1
}
工作示例:
tpost@tpost-desktop:~$ echo `pwd` | grep ^/home/tpost >/dev/null || echo nope
tpost@tpost-desktop:~$ echo `pwd` | grep ^/home/foo >/dev/null || echo nope
nope
克拉(
^
)告诉grep匹配以您提供的开头的行。     

要回复问题请先登录注册