简体   繁体   English

在不同目录的当前目录中运行脚本

[英]running script in current directory on diffrent directory

I run a script which compresses certain files and rename them.我运行一个脚本来压缩某些文件并重命名它们。 I don't want to copy it to each directory and operate it from inside不想复制到每个目录里面去操作

how can I run it from outside the target directory to act on it, tried我怎样才能从目标目录之外运行它来对它进行操作,试过了

'bash compress.sh target_dir/' 'bash compress.sh target_dir/'

In a bash script you can use input via $1 for the first input ($2 for second,..).在 bash 脚本中,您可以通过 $1 输入第一个输入(第二个输入 $2,..)。 You could write your script such that it is working with this.您可以编写您的脚本,使其与此一起使用。 Easiest way might be to store pwd最简单的方法可能是存储密码

#!/usr/bin/env bash
baseDir=$PWD
cd "$1" || echo 'Check input'; exit 1
# ..your stuff..
cd "$baseDir" || :

To do this for all sub directories you can use find and a loop:要对所有子目录执行此操作,您可以使用查找和循环:

#!/usr/bin/env bash
baseDir=$PWD # or any other path
allSubDirectories=$(find "$baseDir" -type d)
for d in $allSubDirectories
do
  cd "$d" || :
  # ..your stuff..
  cd "$baseDir" || :
done

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

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