Sunday, May 19, 2024
10
rated 0 times [  10] [ 0]  / answers: 1 / hits: 65837  / 2 Years ago, tue, november 29, 2022, 9:51:30

I have a bash script install.sh in my current directory and I have a directory apps which contains multiple directories. I want to loop through these sub directories in app folder and execute some script. After executing script in first folder it should come back and enter into next folder. I have tried this but it's skipping one after another. I mean it's entering into all odd folders and not entering into even folders.



Code in install.sh



for f in apps/*;
do
[ -d $f ] && cd "$f" && echo Entering into $f and installing packages
cd ..
done;

More From » command-line

 Answers
2

Use full path of your parent directory(in my case apps directory located in my home directory) and remove one extra command(cd ..)


for dir in ~/apps/*;
do
[ -d "$dir" ] && cd "$dir" && echo "Entering into $dir and installing packages"
done;

See screenshot: with cd .. command and using apps/*


enter image description here


See screenshot: without cd .. command and using ~/apps/*


enter image description here


[#22963] Wednesday, November 30, 2022, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mance

Total Points: 198
Total Questions: 105
Total Answers: 128

Location: South Georgia
Member since Mon, Aug 16, 2021
3 Years ago
mance questions
Wed, Jul 14, 21, 12:04, 3 Years ago
Tue, Jul 20, 21, 10:52, 3 Years ago
Thu, Sep 1, 22, 15:22, 2 Years ago
Sun, Jun 13, 21, 05:50, 3 Years ago
;