Bash array with folder paths and wildcards The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsWhy is printf better than echo?Why does my shell script choke on whitespace or other special characters?When is double-quoting necessary?List of top level folders with contents no younger than 30 daysBash script error with strings with paths that have spaces and wildcardsfind delete shows “No such file or directory”Exit bash when find gets to a folder with permission deniedHow to set up a pre-defined variable with multiple directory paths to use with the find commandregarding recursive deleting of a specific file typeHow to store a path built with wildcards and containing with spaces into a variableFind and delete folders within directory that are older than x daysCheck if files are deletedCleanup Script for macOS

Can the prologue be the backstory of your main character?

"... to apply for a visa" or "... and applied for a visa"?

Does Parliament need to approve the new Brexit delay to 31 October 2019?

How to split my screen on my Macbook Air?

Would an alien lifeform be able to achieve space travel if lacking in vision?

Cooking pasta in a water boiler

Working through the single responsibility principle (SRP) in Python when calls are expensive

Am I ethically obligated to go into work on an off day if the reason is sudden?

How are presidential pardons supposed to be used?

What information about me do stores get via my credit card?

How can I protect witches in combat who wear limited clothing?

Windows 10: How to Lock (not sleep) laptop on lid close?

Difference between "generating set" and free product?

How is simplicity better than precision and clarity in prose?

Create an outline of font

Did God make two great lights or did He make the great light two?

The variadic template constructor of my class cannot modify my class members, why is that so?

Wall plug outlet change

Semisimplicity of the category of coherent sheaves?

University's motivation for having tenure-track positions

The following signatures were invalid: EXPKEYSIG 1397BC53640DB551

Can a novice safely splice in wire to lengthen 5V charging cable?

Mortgage adviser recommends a longer term than necessary combined with overpayments

Arduino Pro Micro - switch off LEDs



Bash array with folder paths and wildcards



The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election ResultsWhy is printf better than echo?Why does my shell script choke on whitespace or other special characters?When is double-quoting necessary?List of top level folders with contents no younger than 30 daysBash script error with strings with paths that have spaces and wildcardsfind delete shows “No such file or directory”Exit bash when find gets to a folder with permission deniedHow to set up a pre-defined variable with multiple directory paths to use with the find commandregarding recursive deleting of a specific file typeHow to store a path built with wildcards and containing with spaces into a variableFind and delete folders within directory that are older than x daysCheck if files are deletedCleanup Script for macOS



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















enter image description here



Given the above folder paths, I am trying to write a script that will delete files older than a certain amount of days. I am trying to use some more advanced techniques than just hard coding the paths (my real life examples has ALOT more folders). Here is what I have so far:



#!/bin/bash

FILEAGE=15

#Array of folders to clean
dir_array=(
"/srv/*/folderA"
"/srv/level1D/*/folderA"
)


#function to be used for deleting files. Needs to be called with a path
function dir_delete()
echo "Deleting:"
find $1 -type f -mindepth 1 -maxdepth 1 -mtime +$FILEAGE -delete -print
echo ""


echo "##### Looping through dir_array array and using dir_delete function to delete files older than $FILEAGE days #####"
for d in "$dir_array[@]";do
if [ -d $d ];then

echo "##### Deleting all files older than $FILEAGE in $d #####"
dir_delete $d

else
echo "##### Did not find directory: $d #####"
echo ""
fi

done


My script is returning that it can't find the directories like so:



Did not find directory: /srv/*/folderA


Note: There will be files throughout these folders under 15 days that can't be deleted.



Update



Using @Kusalananda suggestion '/srv/'*'/folderA' fixed it for me. I'm leaving the wrong code in my original post above should anyone want to see what I was doing wrong.










share|improve this question
























  • I don't think you can provide multiple root paths with e. g. find /path/*/to/places. The easiest option for me off the top of my head would be for rootpath in /srv/*/folderA; do find "$rootpath" [...]; done.

    – DopeGhoti
    yesterday











  • * will not be expanded when it's quoted. Do you know that the mtime of a directory only updates when a file is deleted or created in it?

    – Kusalananda
    yesterday






  • 1





    @DopeGhoti find does support multiple search paths, but the * is expanded in the wrong place. It should be expanded in the array, not in the execution of the find command.

    – Kusalananda
    yesterday

















1















enter image description here



Given the above folder paths, I am trying to write a script that will delete files older than a certain amount of days. I am trying to use some more advanced techniques than just hard coding the paths (my real life examples has ALOT more folders). Here is what I have so far:



#!/bin/bash

FILEAGE=15

#Array of folders to clean
dir_array=(
"/srv/*/folderA"
"/srv/level1D/*/folderA"
)


#function to be used for deleting files. Needs to be called with a path
function dir_delete()
echo "Deleting:"
find $1 -type f -mindepth 1 -maxdepth 1 -mtime +$FILEAGE -delete -print
echo ""


echo "##### Looping through dir_array array and using dir_delete function to delete files older than $FILEAGE days #####"
for d in "$dir_array[@]";do
if [ -d $d ];then

echo "##### Deleting all files older than $FILEAGE in $d #####"
dir_delete $d

else
echo "##### Did not find directory: $d #####"
echo ""
fi

done


My script is returning that it can't find the directories like so:



Did not find directory: /srv/*/folderA


Note: There will be files throughout these folders under 15 days that can't be deleted.



Update



Using @Kusalananda suggestion '/srv/'*'/folderA' fixed it for me. I'm leaving the wrong code in my original post above should anyone want to see what I was doing wrong.










share|improve this question
























  • I don't think you can provide multiple root paths with e. g. find /path/*/to/places. The easiest option for me off the top of my head would be for rootpath in /srv/*/folderA; do find "$rootpath" [...]; done.

    – DopeGhoti
    yesterday











  • * will not be expanded when it's quoted. Do you know that the mtime of a directory only updates when a file is deleted or created in it?

    – Kusalananda
    yesterday






  • 1





    @DopeGhoti find does support multiple search paths, but the * is expanded in the wrong place. It should be expanded in the array, not in the execution of the find command.

    – Kusalananda
    yesterday













1












1








1


0






enter image description here



Given the above folder paths, I am trying to write a script that will delete files older than a certain amount of days. I am trying to use some more advanced techniques than just hard coding the paths (my real life examples has ALOT more folders). Here is what I have so far:



#!/bin/bash

FILEAGE=15

#Array of folders to clean
dir_array=(
"/srv/*/folderA"
"/srv/level1D/*/folderA"
)


#function to be used for deleting files. Needs to be called with a path
function dir_delete()
echo "Deleting:"
find $1 -type f -mindepth 1 -maxdepth 1 -mtime +$FILEAGE -delete -print
echo ""


echo "##### Looping through dir_array array and using dir_delete function to delete files older than $FILEAGE days #####"
for d in "$dir_array[@]";do
if [ -d $d ];then

echo "##### Deleting all files older than $FILEAGE in $d #####"
dir_delete $d

else
echo "##### Did not find directory: $d #####"
echo ""
fi

done


My script is returning that it can't find the directories like so:



Did not find directory: /srv/*/folderA


Note: There will be files throughout these folders under 15 days that can't be deleted.



Update



Using @Kusalananda suggestion '/srv/'*'/folderA' fixed it for me. I'm leaving the wrong code in my original post above should anyone want to see what I was doing wrong.










share|improve this question
















enter image description here



Given the above folder paths, I am trying to write a script that will delete files older than a certain amount of days. I am trying to use some more advanced techniques than just hard coding the paths (my real life examples has ALOT more folders). Here is what I have so far:



#!/bin/bash

FILEAGE=15

#Array of folders to clean
dir_array=(
"/srv/*/folderA"
"/srv/level1D/*/folderA"
)


#function to be used for deleting files. Needs to be called with a path
function dir_delete()
echo "Deleting:"
find $1 -type f -mindepth 1 -maxdepth 1 -mtime +$FILEAGE -delete -print
echo ""


echo "##### Looping through dir_array array and using dir_delete function to delete files older than $FILEAGE days #####"
for d in "$dir_array[@]";do
if [ -d $d ];then

echo "##### Deleting all files older than $FILEAGE in $d #####"
dir_delete $d

else
echo "##### Did not find directory: $d #####"
echo ""
fi

done


My script is returning that it can't find the directories like so:



Did not find directory: /srv/*/folderA


Note: There will be files throughout these folders under 15 days that can't be deleted.



Update



Using @Kusalananda suggestion '/srv/'*'/folderA' fixed it for me. I'm leaving the wrong code in my original post above should anyone want to see what I was doing wrong.







bash shell-script shell wildcards variable






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday







JuanD

















asked yesterday









JuanDJuanD

2616




2616












  • I don't think you can provide multiple root paths with e. g. find /path/*/to/places. The easiest option for me off the top of my head would be for rootpath in /srv/*/folderA; do find "$rootpath" [...]; done.

    – DopeGhoti
    yesterday











  • * will not be expanded when it's quoted. Do you know that the mtime of a directory only updates when a file is deleted or created in it?

    – Kusalananda
    yesterday






  • 1





    @DopeGhoti find does support multiple search paths, but the * is expanded in the wrong place. It should be expanded in the array, not in the execution of the find command.

    – Kusalananda
    yesterday

















  • I don't think you can provide multiple root paths with e. g. find /path/*/to/places. The easiest option for me off the top of my head would be for rootpath in /srv/*/folderA; do find "$rootpath" [...]; done.

    – DopeGhoti
    yesterday











  • * will not be expanded when it's quoted. Do you know that the mtime of a directory only updates when a file is deleted or created in it?

    – Kusalananda
    yesterday






  • 1





    @DopeGhoti find does support multiple search paths, but the * is expanded in the wrong place. It should be expanded in the array, not in the execution of the find command.

    – Kusalananda
    yesterday
















I don't think you can provide multiple root paths with e. g. find /path/*/to/places. The easiest option for me off the top of my head would be for rootpath in /srv/*/folderA; do find "$rootpath" [...]; done.

– DopeGhoti
yesterday





I don't think you can provide multiple root paths with e. g. find /path/*/to/places. The easiest option for me off the top of my head would be for rootpath in /srv/*/folderA; do find "$rootpath" [...]; done.

– DopeGhoti
yesterday













* will not be expanded when it's quoted. Do you know that the mtime of a directory only updates when a file is deleted or created in it?

– Kusalananda
yesterday





* will not be expanded when it's quoted. Do you know that the mtime of a directory only updates when a file is deleted or created in it?

– Kusalananda
yesterday




1




1





@DopeGhoti find does support multiple search paths, but the * is expanded in the wrong place. It should be expanded in the array, not in the execution of the find command.

– Kusalananda
yesterday





@DopeGhoti find does support multiple search paths, but the * is expanded in the wrong place. It should be expanded in the array, not in the execution of the find command.

– Kusalananda
yesterday










2 Answers
2






active

oldest

votes


















1














The shell globbing pattern * is not expanded within double quotes. This means that your loop



for d in "$dir_array[@]";do


is looping over patterns. In your call to dir_delete, you use the patterns unquoted, so they would be expand there (but it never gets there). The function would however only use the first word of whatever matches the pattern in the call to find.



The real show-stopper is that the patterns will also be expanded in the [ -d $d ] test, which is awkward since the -d test only ever takes a single pathname. This is, in the end, why the script fails.



Instead, make sure that the patterns are properly expanded when assigning to dir_array:



dir_array=(
/srv/*/folderA
/srv/level1D/*/folderA
)


If folderA or some other part of the pathnames contains spaces etc., that part of the pathname should be quoted, but the * should not be quoted.



Also remember to double quote the expansions of all variables, unless you know the contexts in which they don't need to be quoted.



There may be quoting issues with the following lines:



  • if [ -d $d ];then

  • find $1 -type f -mindepth 1 -maxdepth 1 -mtime +$FILEAGE -delete -print

  • dir_delete $d

Also consider using printf rather than echo when outputting variable data.



Related:



  • Why does my shell script choke on whitespace or other special characters?

  • When is double-quoting necessary?

  • Why is printf better than echo?





share|improve this answer

























  • Your suggestion of using '/srv/'*'/folderA' worked for me. Thanks again.

    – JuanD
    yesterday



















1














You can do this with the extended glob option which will allow you to expand wildcards within a variable with *()



shopt -s extglob
dir_array=(
"/srv/*(*)/folderA"
"/srv/level1D/*(*)/folderA"
)


The expansion would happen at the for d in ... which I think is the correct point.






share|improve this answer




















  • 1





    No, just use /srv/*/folderA unquoted (or '/srv/'*'/folderA'). It will expand correctly at the time of assigning to dir_array.

    – Kusalananda
    yesterday












  • @XrXca your suggestion gave me the following message: [: /srv/level1A/folderA: binary operator expected

    – JuanD
    yesterday











  • @Kusalananda I think your solution is working. Testing a few more scenarios now. Thank you much. I dont know if I can accept a sub comment as an answer.

    – JuanD
    yesterday











  • @Kusalananda: Either should work, I tend to use the () construct when I'm using extglobs just because it's obvious it's not "normal", when a block of code is copied from one script to another, and I use the @(A|B|C).construct quite a bit.so it's "similar' in my mind.

    – XrXca
    yesterday











  • @XrXca I'm just looking at the code and noticing that the only thing the script is using bash for (and not /bin/sh) is that single array. Changing it to set -- patterns would allow it to run under /bin/sh (the loop would loop over "$@" instead). My personal preference is to use /bin/sh, which is why I commented the way I did. Apart from that, you may well be right.

    – Kusalananda
    yesterday












Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f512148%2fbash-array-with-folder-paths-and-wildcards%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














The shell globbing pattern * is not expanded within double quotes. This means that your loop



for d in "$dir_array[@]";do


is looping over patterns. In your call to dir_delete, you use the patterns unquoted, so they would be expand there (but it never gets there). The function would however only use the first word of whatever matches the pattern in the call to find.



The real show-stopper is that the patterns will also be expanded in the [ -d $d ] test, which is awkward since the -d test only ever takes a single pathname. This is, in the end, why the script fails.



Instead, make sure that the patterns are properly expanded when assigning to dir_array:



dir_array=(
/srv/*/folderA
/srv/level1D/*/folderA
)


If folderA or some other part of the pathnames contains spaces etc., that part of the pathname should be quoted, but the * should not be quoted.



Also remember to double quote the expansions of all variables, unless you know the contexts in which they don't need to be quoted.



There may be quoting issues with the following lines:



  • if [ -d $d ];then

  • find $1 -type f -mindepth 1 -maxdepth 1 -mtime +$FILEAGE -delete -print

  • dir_delete $d

Also consider using printf rather than echo when outputting variable data.



Related:



  • Why does my shell script choke on whitespace or other special characters?

  • When is double-quoting necessary?

  • Why is printf better than echo?





share|improve this answer

























  • Your suggestion of using '/srv/'*'/folderA' worked for me. Thanks again.

    – JuanD
    yesterday
















1














The shell globbing pattern * is not expanded within double quotes. This means that your loop



for d in "$dir_array[@]";do


is looping over patterns. In your call to dir_delete, you use the patterns unquoted, so they would be expand there (but it never gets there). The function would however only use the first word of whatever matches the pattern in the call to find.



The real show-stopper is that the patterns will also be expanded in the [ -d $d ] test, which is awkward since the -d test only ever takes a single pathname. This is, in the end, why the script fails.



Instead, make sure that the patterns are properly expanded when assigning to dir_array:



dir_array=(
/srv/*/folderA
/srv/level1D/*/folderA
)


If folderA or some other part of the pathnames contains spaces etc., that part of the pathname should be quoted, but the * should not be quoted.



Also remember to double quote the expansions of all variables, unless you know the contexts in which they don't need to be quoted.



There may be quoting issues with the following lines:



  • if [ -d $d ];then

  • find $1 -type f -mindepth 1 -maxdepth 1 -mtime +$FILEAGE -delete -print

  • dir_delete $d

Also consider using printf rather than echo when outputting variable data.



Related:



  • Why does my shell script choke on whitespace or other special characters?

  • When is double-quoting necessary?

  • Why is printf better than echo?





share|improve this answer

























  • Your suggestion of using '/srv/'*'/folderA' worked for me. Thanks again.

    – JuanD
    yesterday














1












1








1







The shell globbing pattern * is not expanded within double quotes. This means that your loop



for d in "$dir_array[@]";do


is looping over patterns. In your call to dir_delete, you use the patterns unquoted, so they would be expand there (but it never gets there). The function would however only use the first word of whatever matches the pattern in the call to find.



The real show-stopper is that the patterns will also be expanded in the [ -d $d ] test, which is awkward since the -d test only ever takes a single pathname. This is, in the end, why the script fails.



Instead, make sure that the patterns are properly expanded when assigning to dir_array:



dir_array=(
/srv/*/folderA
/srv/level1D/*/folderA
)


If folderA or some other part of the pathnames contains spaces etc., that part of the pathname should be quoted, but the * should not be quoted.



Also remember to double quote the expansions of all variables, unless you know the contexts in which they don't need to be quoted.



There may be quoting issues with the following lines:



  • if [ -d $d ];then

  • find $1 -type f -mindepth 1 -maxdepth 1 -mtime +$FILEAGE -delete -print

  • dir_delete $d

Also consider using printf rather than echo when outputting variable data.



Related:



  • Why does my shell script choke on whitespace or other special characters?

  • When is double-quoting necessary?

  • Why is printf better than echo?





share|improve this answer















The shell globbing pattern * is not expanded within double quotes. This means that your loop



for d in "$dir_array[@]";do


is looping over patterns. In your call to dir_delete, you use the patterns unquoted, so they would be expand there (but it never gets there). The function would however only use the first word of whatever matches the pattern in the call to find.



The real show-stopper is that the patterns will also be expanded in the [ -d $d ] test, which is awkward since the -d test only ever takes a single pathname. This is, in the end, why the script fails.



Instead, make sure that the patterns are properly expanded when assigning to dir_array:



dir_array=(
/srv/*/folderA
/srv/level1D/*/folderA
)


If folderA or some other part of the pathnames contains spaces etc., that part of the pathname should be quoted, but the * should not be quoted.



Also remember to double quote the expansions of all variables, unless you know the contexts in which they don't need to be quoted.



There may be quoting issues with the following lines:



  • if [ -d $d ];then

  • find $1 -type f -mindepth 1 -maxdepth 1 -mtime +$FILEAGE -delete -print

  • dir_delete $d

Also consider using printf rather than echo when outputting variable data.



Related:



  • Why does my shell script choke on whitespace or other special characters?

  • When is double-quoting necessary?

  • Why is printf better than echo?






share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday

























answered yesterday









KusalanandaKusalananda

141k18263439




141k18263439












  • Your suggestion of using '/srv/'*'/folderA' worked for me. Thanks again.

    – JuanD
    yesterday


















  • Your suggestion of using '/srv/'*'/folderA' worked for me. Thanks again.

    – JuanD
    yesterday

















Your suggestion of using '/srv/'*'/folderA' worked for me. Thanks again.

– JuanD
yesterday






Your suggestion of using '/srv/'*'/folderA' worked for me. Thanks again.

– JuanD
yesterday














1














You can do this with the extended glob option which will allow you to expand wildcards within a variable with *()



shopt -s extglob
dir_array=(
"/srv/*(*)/folderA"
"/srv/level1D/*(*)/folderA"
)


The expansion would happen at the for d in ... which I think is the correct point.






share|improve this answer




















  • 1





    No, just use /srv/*/folderA unquoted (or '/srv/'*'/folderA'). It will expand correctly at the time of assigning to dir_array.

    – Kusalananda
    yesterday












  • @XrXca your suggestion gave me the following message: [: /srv/level1A/folderA: binary operator expected

    – JuanD
    yesterday











  • @Kusalananda I think your solution is working. Testing a few more scenarios now. Thank you much. I dont know if I can accept a sub comment as an answer.

    – JuanD
    yesterday











  • @Kusalananda: Either should work, I tend to use the () construct when I'm using extglobs just because it's obvious it's not "normal", when a block of code is copied from one script to another, and I use the @(A|B|C).construct quite a bit.so it's "similar' in my mind.

    – XrXca
    yesterday











  • @XrXca I'm just looking at the code and noticing that the only thing the script is using bash for (and not /bin/sh) is that single array. Changing it to set -- patterns would allow it to run under /bin/sh (the loop would loop over "$@" instead). My personal preference is to use /bin/sh, which is why I commented the way I did. Apart from that, you may well be right.

    – Kusalananda
    yesterday
















1














You can do this with the extended glob option which will allow you to expand wildcards within a variable with *()



shopt -s extglob
dir_array=(
"/srv/*(*)/folderA"
"/srv/level1D/*(*)/folderA"
)


The expansion would happen at the for d in ... which I think is the correct point.






share|improve this answer




















  • 1





    No, just use /srv/*/folderA unquoted (or '/srv/'*'/folderA'). It will expand correctly at the time of assigning to dir_array.

    – Kusalananda
    yesterday












  • @XrXca your suggestion gave me the following message: [: /srv/level1A/folderA: binary operator expected

    – JuanD
    yesterday











  • @Kusalananda I think your solution is working. Testing a few more scenarios now. Thank you much. I dont know if I can accept a sub comment as an answer.

    – JuanD
    yesterday











  • @Kusalananda: Either should work, I tend to use the () construct when I'm using extglobs just because it's obvious it's not "normal", when a block of code is copied from one script to another, and I use the @(A|B|C).construct quite a bit.so it's "similar' in my mind.

    – XrXca
    yesterday











  • @XrXca I'm just looking at the code and noticing that the only thing the script is using bash for (and not /bin/sh) is that single array. Changing it to set -- patterns would allow it to run under /bin/sh (the loop would loop over "$@" instead). My personal preference is to use /bin/sh, which is why I commented the way I did. Apart from that, you may well be right.

    – Kusalananda
    yesterday














1












1








1







You can do this with the extended glob option which will allow you to expand wildcards within a variable with *()



shopt -s extglob
dir_array=(
"/srv/*(*)/folderA"
"/srv/level1D/*(*)/folderA"
)


The expansion would happen at the for d in ... which I think is the correct point.






share|improve this answer















You can do this with the extended glob option which will allow you to expand wildcards within a variable with *()



shopt -s extglob
dir_array=(
"/srv/*(*)/folderA"
"/srv/level1D/*(*)/folderA"
)


The expansion would happen at the for d in ... which I think is the correct point.







share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday

























answered yesterday









XrXcaXrXca

813




813







  • 1





    No, just use /srv/*/folderA unquoted (or '/srv/'*'/folderA'). It will expand correctly at the time of assigning to dir_array.

    – Kusalananda
    yesterday












  • @XrXca your suggestion gave me the following message: [: /srv/level1A/folderA: binary operator expected

    – JuanD
    yesterday











  • @Kusalananda I think your solution is working. Testing a few more scenarios now. Thank you much. I dont know if I can accept a sub comment as an answer.

    – JuanD
    yesterday











  • @Kusalananda: Either should work, I tend to use the () construct when I'm using extglobs just because it's obvious it's not "normal", when a block of code is copied from one script to another, and I use the @(A|B|C).construct quite a bit.so it's "similar' in my mind.

    – XrXca
    yesterday











  • @XrXca I'm just looking at the code and noticing that the only thing the script is using bash for (and not /bin/sh) is that single array. Changing it to set -- patterns would allow it to run under /bin/sh (the loop would loop over "$@" instead). My personal preference is to use /bin/sh, which is why I commented the way I did. Apart from that, you may well be right.

    – Kusalananda
    yesterday













  • 1





    No, just use /srv/*/folderA unquoted (or '/srv/'*'/folderA'). It will expand correctly at the time of assigning to dir_array.

    – Kusalananda
    yesterday












  • @XrXca your suggestion gave me the following message: [: /srv/level1A/folderA: binary operator expected

    – JuanD
    yesterday











  • @Kusalananda I think your solution is working. Testing a few more scenarios now. Thank you much. I dont know if I can accept a sub comment as an answer.

    – JuanD
    yesterday











  • @Kusalananda: Either should work, I tend to use the () construct when I'm using extglobs just because it's obvious it's not "normal", when a block of code is copied from one script to another, and I use the @(A|B|C).construct quite a bit.so it's "similar' in my mind.

    – XrXca
    yesterday











  • @XrXca I'm just looking at the code and noticing that the only thing the script is using bash for (and not /bin/sh) is that single array. Changing it to set -- patterns would allow it to run under /bin/sh (the loop would loop over "$@" instead). My personal preference is to use /bin/sh, which is why I commented the way I did. Apart from that, you may well be right.

    – Kusalananda
    yesterday








1




1





No, just use /srv/*/folderA unquoted (or '/srv/'*'/folderA'). It will expand correctly at the time of assigning to dir_array.

– Kusalananda
yesterday






No, just use /srv/*/folderA unquoted (or '/srv/'*'/folderA'). It will expand correctly at the time of assigning to dir_array.

– Kusalananda
yesterday














@XrXca your suggestion gave me the following message: [: /srv/level1A/folderA: binary operator expected

– JuanD
yesterday





@XrXca your suggestion gave me the following message: [: /srv/level1A/folderA: binary operator expected

– JuanD
yesterday













@Kusalananda I think your solution is working. Testing a few more scenarios now. Thank you much. I dont know if I can accept a sub comment as an answer.

– JuanD
yesterday





@Kusalananda I think your solution is working. Testing a few more scenarios now. Thank you much. I dont know if I can accept a sub comment as an answer.

– JuanD
yesterday













@Kusalananda: Either should work, I tend to use the () construct when I'm using extglobs just because it's obvious it's not "normal", when a block of code is copied from one script to another, and I use the @(A|B|C).construct quite a bit.so it's "similar' in my mind.

– XrXca
yesterday





@Kusalananda: Either should work, I tend to use the () construct when I'm using extglobs just because it's obvious it's not "normal", when a block of code is copied from one script to another, and I use the @(A|B|C).construct quite a bit.so it's "similar' in my mind.

– XrXca
yesterday













@XrXca I'm just looking at the code and noticing that the only thing the script is using bash for (and not /bin/sh) is that single array. Changing it to set -- patterns would allow it to run under /bin/sh (the loop would loop over "$@" instead). My personal preference is to use /bin/sh, which is why I commented the way I did. Apart from that, you may well be right.

– Kusalananda
yesterday






@XrXca I'm just looking at the code and noticing that the only thing the script is using bash for (and not /bin/sh) is that single array. Changing it to set -- patterns would allow it to run under /bin/sh (the loop would loop over "$@" instead). My personal preference is to use /bin/sh, which is why I commented the way I did. Apart from that, you may well be right.

– Kusalananda
yesterday


















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f512148%2fbash-array-with-folder-paths-and-wildcards%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







-bash, shell, shell-script, variable, wildcards

Popular posts from this blog

Frič See also Navigation menuinternal link

Identify plant with long narrow paired leaves and reddish stems Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?What is this plant with long sharp leaves? Is it a weed?What is this 3ft high, stalky plant, with mid sized narrow leaves?What is this young shrub with opposite ovate, crenate leaves and reddish stems?What is this plant with large broad serrated leaves?Identify this upright branching weed with long leaves and reddish stemsPlease help me identify this bulbous plant with long, broad leaves and white flowersWhat is this small annual with narrow gray/green leaves and rust colored daisy-type flowers?What is this chilli plant?Does anyone know what type of chilli plant this is?Help identify this plant

fontconfig warning: “/etc/fonts/fonts.conf”, line 100: unknown “element blank” The 2019 Stack Overflow Developer Survey Results Are In“tar: unrecognized option --warning” during 'apt-get install'How to fix Fontconfig errorHow do I figure out which font file is chosen for a system generic font alias?Why are some apt-get-installed fonts being ignored by fc-list, xfontsel, etc?Reload settings in /etc/fonts/conf.dTaking 30 seconds longer to boot after upgrade from jessie to stretchHow to match multiple font names with a single <match> element?Adding a custom font to fontconfigRemoving fonts from fontconfig <match> resultsBroken fonts after upgrading Firefox ESR to latest Firefox