在不同目录中运行 shell 脚本

发布于 2024-12-22 01:33:27 字数 1394 浏览 1 评论 0原文

我有一个名为 tinkering 的目录,其中包含以下子目录:

saraswati\ and\ durga\ pooja
64\ yogini\ pooja
52\ guruwar\ ke\ tap\ se\ unemployment\ finish
bajrang\ bali\ har\ lete\ ain\ devote\ dukh
bhoot\ bhagane\ ke\ tareke
bacho\ ko\ gussa\ ane\ ka\ karan
durga\ pooja
khatre\ ke\ nishan\ hanth\ mein
saraswati\ and\ durga\ pooja
seb\ chadhane\ se\ ma\ hinnamasta
bhoot\ bhagane\ ke\ tareke

每个子目录都有一个名为 script.sh 的脚本。

我在终端上写了一个脚本:

cd ~/tinkering/; 
cd saraswati\ and\ durga\ pooja/;  
./script.sh;    
cd ..;  
cd 64\ yogini\ pooja/;  
./script.sh;cd ../;  
cd 52\ guruwar\ ke\ tap\ se\ unemployment\ finish/;  
./script.sh;cd ../;  
cd bajrang\ bali\ har\ lete\ ain\ devote\ dukh/;  
./script.sh;cd ../;  
cd bhoot\ bhagane\ ke\ tareke/;  
./script.sh;cd ..;  
cd bacho\ ko\ gussa\ ane\ ka\ karan/;  
./script.sh;cd ..;  
cd durga\ pooja/;./script.sh;  
cd ..;  
cd khatre\ ke\ nishan\ hanth\ mein/;./script.sh;  
cd ..;cd saraswati\ and\ durga\ pooja/;  
./script.sh;cd ..;  
cd seb\ chadhane\ se\ ma\ hinnamasta/;  
./script.sh;cd ..;  
cd bhoot\ bhagane\ ke\ tareke/;  
./script.sh;cd ..;

但是这个脚本无法运行。目的不是转到每个子目录并输入 ./script.sh 我能够自动化此过程。我在上面的代码中犯了什么错误?

编辑 请注意,当我在父目录中修补所有子目录时,我在终端上编写了这些由分号分隔的命令,所有子目录都有一个不同的脚本,该脚本正在执行不同的工作我想从终端上的父目录调用子目录的所有 shell 脚本。

I have a directory named tinkering which has following subdirectories:

saraswati\ and\ durga\ pooja
64\ yogini\ pooja
52\ guruwar\ ke\ tap\ se\ unemployment\ finish
bajrang\ bali\ har\ lete\ ain\ devote\ dukh
bhoot\ bhagane\ ke\ tareke
bacho\ ko\ gussa\ ane\ ka\ karan
durga\ pooja
khatre\ ke\ nishan\ hanth\ mein
saraswati\ and\ durga\ pooja
seb\ chadhane\ se\ ma\ hinnamasta
bhoot\ bhagane\ ke\ tareke

Each of these sub directories has a script named script.sh.

I wrote a script on the terminal:

cd ~/tinkering/; 
cd saraswati\ and\ durga\ pooja/;  
./script.sh;    
cd ..;  
cd 64\ yogini\ pooja/;  
./script.sh;cd ../;  
cd 52\ guruwar\ ke\ tap\ se\ unemployment\ finish/;  
./script.sh;cd ../;  
cd bajrang\ bali\ har\ lete\ ain\ devote\ dukh/;  
./script.sh;cd ../;  
cd bhoot\ bhagane\ ke\ tareke/;  
./script.sh;cd ..;  
cd bacho\ ko\ gussa\ ane\ ka\ karan/;  
./script.sh;cd ..;  
cd durga\ pooja/;./script.sh;  
cd ..;  
cd khatre\ ke\ nishan\ hanth\ mein/;./script.sh;  
cd ..;cd saraswati\ and\ durga\ pooja/;  
./script.sh;cd ..;  
cd seb\ chadhane\ se\ ma\ hinnamasta/;  
./script.sh;cd ..;  
cd bhoot\ bhagane\ ke\ tareke/;  
./script.sh;cd ..;

But this script could not run. The purpose was rather than going to each subdirectory and typing ./script.sh I be able to automate this process. What mistake did I do in the code above?

EDIT
Please note I wrote these commands on terminal separated by a semi colon while I was in parent directory tinkering all the subdirectories have a different script which is doing a different work I want to invoke all the shell scripts of sub directories from parent directory on terminal.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

獨角戲 2024-12-29 01:33:27
for subdir in */; do
  cd "$subdir"
  ./script.sh
  cd ..
done
for subdir in */; do
  cd "$subdir"
  ./script.sh
  cd ..
done
偏闹i 2024-12-29 01:33:27

正如其他人指出的那样,“无法运行”可能意味着很多事情。例如,如果您收到一条消息说权限被拒绝,如果您想使用./script调用脚本,则必须使用chmod a+x script.sh。嘘。

如果您能够使用 /some path with whitespace/script.sh 运行脚本,您可以将其放入 ~/tinkering/ 下的 shell 脚本中。

find -name script.sh -mindepth 2 -maxdepth 2 -exec sh {} \;

Like others have pointed out, "could not run" could mean a number of things. E.g. if you get a message saying Permission denied, you have to use chmod a+x script.sh if you want to invoke your script with ./script.sh.

If you're able to run your scripts with /some path with whitespace/script.sh, you could put this into a shell script under ~/tinkering/.

find -name script.sh -mindepth 2 -maxdepth 2 -exec sh {} \;
余生再见 2024-12-29 01:33:27

该脚本需要可执行并且有一个 shebang。

在开头放置 #!/bin/sh 并在终端 chmod +x myscript.sh 中运行(无论您如何调用脚本)。

如果您尝试处理所有子目录,您还可以使用 for 循环更有效地完成它(我看到 Roland 已经提供了答案,所以我将省略它)。

The script needs to be executable and have a shebang.

Put #!/bin/sh on at the start and run in the terminal chmod +x myscript.sh (for whatever you've called your script).

If you're trying to do all subdirectories, you could also do it more efficiently with a for loop (I see Roland has provided the answer for that so I'll omit it).

似梦非梦 2024-12-29 01:33:27

我想这可能有用。

for d in */; do
 cp scr.sh "$d"
 chmod +x scr.sh
 done

for subdir in */; do
  cd "$subdir"
  ./scr.sh
  cd ..
done

for d in */; do rm scr.sh "$d"; done

I guess this might work .

for d in */; do
 cp scr.sh "$d"
 chmod +x scr.sh
 done

for subdir in */; do
  cd "$subdir"
  ./scr.sh
  cd ..
done

for d in */; do rm scr.sh "$d"; done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文