在不同目录中运行 shell 脚本
我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如其他人指出的那样,“无法运行”可能意味着很多事情。例如,如果您收到一条消息说
权限被拒绝
,如果您想使用./script调用脚本,则必须使用
chmod a+x script.sh
。嘘。如果您能够使用
/some path with whitespace/script.sh
运行脚本,您可以将其放入~/tinkering/
下的 shell 脚本中。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 usechmod 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/
.该脚本需要可执行并且有一个 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 terminalchmod +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).我想这可能有用。
I guess this might work .