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

标签 linux bash shell shebang

所以我已经发现这个问题在这里被问过几次了,但我不太理解答案。

所以我在一个供多人使用的巨型服务器上运行,所以目录有点不稳定,我在弄清楚我应该做什么时遇到了一些问题。

所以我想创建自己的脚本

做了以下事情

$ mkdir myscripts
$ cd myscripts
$ vim dirstatus.sh

在 dirstatus.sh 中我只是写了一个简单的脚本,我更感兴趣的是使脚本从任何目录可执行。

所以

#!/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)"

后来我做了

$ chmod +x dirstatus.sh

当我在 myscripts 目录中执行 ./dirstatus.sh 时它起作用

然而,当我在任何其他目录甚至在 myscripts 目录中执行 $ dirstatus

出现以下错误

~ksh:dirstat:找不到命令

有什么解决办法吗?

最佳答案

问题是包含 dirstatus.sh 文件的 myscripts 目录不在终端 session 的路径中。假设您在家中创建了 myscripts 目录,您可以执行以下操作:

PATH=~/myscripts:$PATH

要使此更改适用于所有新的终端 session ,您可以将以上内容添加到登录时加载的 ~/.profile 或 ~/.bash_profile 文件中。

关于linux - 从任何(非特定)目录运行 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36250300/

相关文章:

linux - 任何包的 Build 和 Devel 文件夹的内容

c - 在 Linux 上用 C 获取每次引导随机种子的可移植方法

php - popen 失败,返回 "sh: <command>: not found"

linux - 将一个文件的所有内容分别加载到各个文件中

macos - 如何将 .txt 文件的内容写入 Mac 上的系统日志中?

linux - 保留备份

bash - 一致地覆盖源脚本中的 DEBUG 陷阱

linux - 确定另一个程序的工作目录

shell - 如何在 tcsh shell 上映射 Delete 和 End 键?

linux - 如何使用 udev 识别设备