0

I'm trying to install pip module based on application directory. root Directory contains multiple sub-directory and inside the sub-directory we have requriement.txt file which contains pip module lets sat below are my directory structure.

├── aws
│   ├── app1
│   │   └── requirement.txt
│   └── app2
│       └── requirement.txt

┌──(palani㉿DESKTOP-1OOLTMI)-[~]
└─$ cat aws/app1/requirement.txt
pymongo
bz2

┌──(palani㉿DESKTOP-1OOLTMI)-[~]
└─$ cat aws/app2/requirement.txt
base64

Now i have read the requirement.txt file each directory (app1 and app2) then i have to create pip package without installing it. But using find command i have collected all the *.txt and stored under result variable.

result=`(find /home/user/aws -maxdepth 2 -type f -name "*.txt" | while read txtfile; do echo $txtfile; done)`
echo $result

now using for loop i need read the each txt file and i need make zip without install something like this.

pip download --dest $destination
Gowmi
  • 559
  • 2
  • 22
  • Put a valid [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)) and paste your script at https://shellcheck.net for validation/recommendation. – Jetchisel May 04 '23 at 05:40

2 Answers2

2

One option is to invoke pip from find

find /home/user/aws -maxdepth 2 -type f -name 'requirements.txt' -exec pip download -r {} \;
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • This gets my upvote as the simplest and most succinct solution. My answer discusses the syntax problems and common solutions in some more detail. – tripleee May 04 '23 at 06:38
  • This is working fine but I'm searching how we can zip archive instead of downloading as whl extension – Gowmi May 04 '23 at 15:41
0

Your code needlessly creates a subshell inside the backticks. You want to avoid the obsolescent backtick syntax anyway.

Also, the while loop is not doing anything useful.

A common solution is to use xargs to pass the output of one command as arguments to another.

find /home/user/aws -maxdepth 2 -type f -name "*.txt" |
xargs cat |
xargs pip download --dest "$destination"

Of course, with find, it already lets you run processes on the results with -exec.

find /home/user/aws -maxdepth 2 -type f -name "*.txt" -exec cat {} + |
xargs pip download --dest "$destination"

The use of + with -exec, like xargs, takes care to run as few processes as possible.

A more traditional solution, which should work fine in this limited case, is to use a command substitution;

pip download --dest "$destination" $(find /home/user/aws -maxdepth 2 -type f -name "*.txt" -exec cat {} +)

This will fail in cases where the output from the command substitution can contain whitespace or shell metacharacters, but as long as the contents of the files are simple, this should work. For more details, please see https://mywiki.wooledge.org/BashFAQ/020

Notice also When to wrap quotes around a shell variable

tripleee
  • 175,061
  • 34
  • 275
  • 318