How to handle 2 directories as one on the command line

I have a script that takes a list of directories and compares their number of files. I would like to sometimes group two or more directories that are processed as a script. Of course, I could change the script, but I would prefer to do this grouping on the command line.

$ ./myscript.sh {dir1 dir2} dir3 dir4 ... 

should be considered as if {dir1 dir2} was a (mathematical) combination of elements of both directories.

How to do it?

0
source share
2 answers

You can use an array of directories for this purpose as follows:

 ## declare an array variable declare -a arr=("/home/x/dir1" "/home/x/dir2" "/home/x/dir3") ## now loop through the above array for i in "${arr[@]}" do ./myscript.sh $i dir3 dir4 ... done 
0
source

How to combine the contents of a directory together in a separate script, and then pass this script as a parameter:

 file1=$1 file2=$2 cp -R $file1/* $file2 echo $file2 

Then you can use the script as follows:

 ./myscript "$(./merge dir1 dir2)" dir3 ... 
0
source

Source: https://habr.com/ru/post/980505/


All Articles