Look at this code:
find /Api -mindepth 1 -maxdepth 1 |
while read DependencyPath;
do
DependencyName=$(basename $DependencyPath);
DependencyOrganization="Api";
echo $DependencyOrganization - $DependencyName;
done
This is the output:
- Courses
- Media
- Orders
- Blog
The first variable (DependencyName=$(basename $DependencyPath); is set. However, the second variable (DependencyOrganization="Api") is not set.
I have no clue why it is not set. I also tried it using bash -x /script and this is the log:
+ find /Api -mindepth 1 -maxdepth 1
+ read DependencyPath
++ basename /Api/Courses
+ DependencyName=Courses
+ DependencyOrganization=Api
+ echo - Courses
- Courses
+ read DependencyPath
++ basename /Api/Media
+ DependencyName=Media
+ DependencyOrganization=Api
+ echo - Media
+ read DependencyPath
- Media
I also tried DependencyOrganization=$(echo "Api") but it does not work.
What should I do?
Update
I'm running inside a container. After docker exec -it container_name bash If I run find -mindepth 1 -maxdepth 1 -type d | while read temp; do name=hi; echo $name; done directly inside the bash, it works. But if I save it in a file and run it as ./script then it prints empty.
$DependencyOrganizationso that it's expanding to nothingset -xvto get more details, or underset -uto get an error when using an uninitialised variable.basename "$DependencyPath". Also you might like to know that you can get the last component of a path directly in the shell with${DependencyPath##*/}set -xv?