简体   繁体   English

从任何(非特定)目录运行shell脚本

[英]Running a shell script from any (non-specific) directory

so I've already found this asked here few other times, but I didn't quite comprehend the answer. 因此,我已经在其他地方几次发现了这个问题,但是我不太理解答案。

So I'm running on a giant server for multiple people, so the directories are a bit wonky and I'm having bit of an issue figuring out what I'm supposed to do. 因此,我正在一个可容纳多个人的巨型服务器上运行,因此目录有些古怪,并且在确定我应该做什么时遇到了一些问题。

So I've wanted to created my own script 所以我想创建自己的脚本

did the following 做了以下

$ mkdir myscripts
$ cd myscripts
$ vim dirstatus.sh

Inside the dirstatus.sh I just wrote a simple script, I'm more of interested in making the script executable from any directory. 在dirstatus.sh中,我只是编写了一个简单的脚本,我更感兴趣的是使该脚本可从任何目录执行。

So 所以

#!/usr/bin/env sh
# My own script - Experimenting with Shell
# Setting path export
export PATH=$PATH:~/bin/myscripts
printf "The current directory is $(pwd) \n
Path of this script is $(PATH)"

afterwards I did 之后我做了

$ chmod +x dirstatus.sh

When I do ./dirstatus.sh inside the myscripts directory it works 当我在myscripts目录中执行./dirstatus.sh时,它可以工作

However when I do $ dirstatus in any other directory or even inside the myscripts directory as well 但是,当我在任何其他目录甚至在myscripts目录中执行$ dirstatus

I get the following error 我收到以下错误

~ksh: dirstat: command not found

Any solutions? 有什么办法吗?

The problem is that the myscripts directory that contains the dirstatus.sh file is not in the path for your terminal session. 问题是包含dirstatus.sh文件的myscripts目录不在终端会话的路径中。 Assuming that you made the myscripts directory inside of your home, you can do the following: 假设您在家中建立了myscripts目录,则可以执行以下操作:

PATH=~/myscripts:$PATH

To make this change work on all new terminal sessions, you can add the above to the ~/.profile or ~/.bash_profile files that are loaded on login. 要使此更改适用于所有新的终端会话,可以将以上内容添加到登录时加载的〜/ .profile或〜/ .bash_profile文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM