首页 > 解决方案 > 如果某些必需的文件是不同的目录,我如何在任何地方执行命令?

问题描述

假设命令是my_command

并且此命令必须在当前工作目录中准备特定文件(file1、file2 和 file3)。

因为我经常my_command在许多不同的目录中使用,所以我想将某些文件保留在某个目录中,并my_command在工作目录中没有这三个文件的情况下执行。我的意思是我不想将这三个文件复制到每个工作目录。

例如:

包含三个文件的目录/home/chest

工作目录:/home/wd

如果我执行命令my_command,它会自动识别三个文件/home/chest/

我认为这种方式类似于添加 $PATH 而不是可执行文件,而只是文件。

标签: linuxpathcommandworking-directory

解决方案


似乎文件需要在当前工作目录中才能使vasp_std命令按预期工作,我认为您可以简单地将所有文件添加到主目录的包含文件夹中,然后从您的文件夹中创建指向该文件夹的符号链接脚本。在脚本的最后,符号链接将被删除:

#!/bin/bash

# create a symbolic link to our resource folder
ln -s ~/include src

# execute other commands here

# finally remove the symbolic link from the current directory
unlink src

如果vasp_std命令要求将文件直接放在当前工作目录下,您可以为每个文件创建一个符号链接:

#!/bin/bash

# create link for to all resource files
for file in ~/include/*
do
  ln -s $file `basename $file`
done

# execute other commands here

# remove any previously created links
for file in ~/include/*
do
  unlink `basename $file`
done

推荐阅读