SoFunction
Updated on 2025-03-09

Methods of executing commands through shell script loop into directory

The company needs to deploy a new environment. There are many projects in the new environment, and the overall directory structure is: /webserver/*

All projects are in the webserver directory, including the code for the laravel and thinkphp projects.

In the laravel framework, the storage vendor bootstrap directory under the root directory requires 777 permissions to be set.

thinkphp framework, the Runtime directory under the root directory App requires 777 permissions to be set.

Therefore, the corresponding directory structure is as follows;

The directory structure of the laravel framework is: /webserver/aaa/www/storage, vendor, bootstrap

The thinkphp framework directory structure is: /webserver/bbb/www/App/Runtime

There are many laravel and thinkphp projects, and it is impossible to set permissions in the project's directory one by one at a time, so you still need to execute shell scripts to operate.

When performing the operation for the first time, enter /webserver/ and execute the following code. You can set the corresponding directory permissions of the laravel and thinkphp projects to 777.

thinkphp framework Runtime directory batch setting 777 permissions

for dir in `ls`; do     #Go through the ls command to display the directory cd $dir;       #Enter a directory if [ -d "www/App" ];then   
 cd www/App;      #Enter the App directory chmod -R 777 Runtime;   #Set permissions cd ../../../;     #Return to the webserver directory else        # means that if the App directory does not exist, it will directly return to the webserver directory. cd ../;       #Return directly to the webserver directory. fi
done

#Note on the third line: Detect whether there is an App directory. If it exists, continue to execute.  -d parameter is to detect whether it is a directory.  Similarly, the -f parameter detects whether it is a directory, !  -d parameter detection directory if it does not exist, execute.Some comments are the same

laravel framework storage vendor bootstrap directory batch setting 777 permissions

for dir in `ls`; do
 cd $dir;
 if [ -d "www/bootstrap" ];then
 cd www;
 chmod -R 777 storage vendor bootstrap;
 cd ../../;
 else 
 cd ../;
 fi
done

After this operation, there are still certain shortcomings, because you must enter the /webserver directory to execute every time, and there are some improvements, which can be executed anywhere, and save the above code as a shell script to execute.

Therefore, the code is improved as follows:

thinkphp framework Runtime directory batch setting 777 permissions

#/bin/sh
pwd="/webserver"       #Initialize the directoryfor dir in $(ls $pwd); do    #Loop Directory cd $dir;
 if [ -d "www/App" ];then
 cd www/App;
 chmod -R 777 Runtime;
 cd ../../../;
 else 
 cd ../;
 fi
done

laravel framework storage vendor bootstrap directory batch setting 777 permissions

#/bin/sh
pwd="/webserver"       #Initialize the directoryfor dir in $(ls $pwd); do    #Loop Directory cd $dir;
 if [ -d "www/bootstrap" ];then
 cd www;
 chmod -R 777 storage vendor bootstrap;
 cd ../../;
 else 
 cd ../;
 fi
done

The above method of running commands through a shell script loop into the directory is all the content I share with you. I hope you can give you a reference and I hope you can support me more.