bash 在不同目录中运行 shell 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8572412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
running shell scripts in different directories
提问by Registered User
I have a directory named tinkeringwhich has following subdirectories:
我有一个名为的目录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
Each of these sub directories has a script named script.sh.
这些子目录中的每一个都有一个名为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.shI be able to automate this process. What mistake did I do in the code above?
但是这个脚本无法运行。目的不是去每个子目录并输入./script.sh我能够自动化这个过程。我在上面的代码中犯了什么错误?
EDITPlease 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.
编辑请注意,当我在父目录中时,我在用分号分隔的终端上编写了这些命令,修补所有子目录都有一个不同的脚本,该脚本正在执行不同的工作我想从终端上的父目录调用子目录的所有 shell 脚本.
回答by Roland Illig
for subdir in */; do
cd "$subdir"
./script.sh
cd ..
done
回答by flesk
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.shif you want to invoke your script with ./script.sh.
就像其他人指出的那样,“无法运行”可能意味着很多事情。例如,如果您收到一条消息说Permission denied,您必须使用chmod a+x script.shif 您想用 调用您的脚本./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/.
如果您能够使用 运行您的脚本/some path with whitespace/script.sh,您可以将其放入~/tinkering/.
find -name script.sh -mindepth 2 -maxdepth 2 -exec sh {} \;
回答by Chris Morgan
The script needs to be executable and have a shebang.
该脚本需要是可执行的并且有一个shebang。
Put #!/bin/shon at the start and run in the terminal chmod +x myscript.sh(for whatever you've called your script).
#!/bin/sh在开始时穿上并在终端中运行chmod +x myscript.sh(无论您如何称呼您的脚本)。
If you're trying to do all subdirectories, you could also do it more efficiently with a forloop (I see Roland has provided the answer for that so I'll omit it).
如果您尝试处理所有子目录,您也可以使用for循环更有效地完成(我看到 Roland 已经为此提供了答案,因此我将省略它)。
回答by Ashwin Kumar k
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

