Skip to content
Advertisement

how do I get bash globbing to work in script

I am trying to convert some of my ksh93 scripts to bash in cygwin. I have found 2 things right now that give me trouble. The first is a function I put in .bashrc was not recognized in the script. I put the function in the script to get around that. The second is that it won’t glob like it does in ksh93. Setting extglob didn’t seem to help. Here is what I have done:

#! /bin/bash

rep() {
        perl -E 'say "'"$1"'" x '$2
        # seq -s"$1" $2|tr -d '[:digit:]'
}

# added these 2 lines for testing
shopt -s extglob
shopt extglob

ziptext="Monthly files for $(date --date="$(date +%Y-%m-15) -1 month" +'%B %Y')"
equals4=$(rep = $((${#ziptext} + 6)))
equals="$(rep = ${#ziptext})"
spaces="$(rep ' ' ${#ziptext})"

# added these 2 lines for testing
ls -l 20[0-9][0-9]' Monthly Data - review.xlsx'
pwd

echo "

  $equals4
  =  $equals  =
  =  $spaces  =
  =  $ziptext  =
  =  $spaces  =
  =  $equals  =
  $equals4nnn" | zip -9 -u -z 
        20[0-9][0-9]' Monthly Data - review.xlsx' 
        20[0-9][0-9]' Monthly Tables - review.xlsx'

The result is:

extglob         on
ls: cannot access '20[0-9][0-9] Monthly Data - review.xlsx': No such file or directory
/cygdrive/c/reports
        zip warning: 20[0-9][0-9] Monthly Data - review.xlsx not found or empty
        zip warning: name not matched: 20[0-9][0-9] Monthly Tables - review.xlsx

From the shell, doing

ls 20[0-9][0-9]' Monthly Data - review.xlsx' 
    20[0-9][0-9]' Monthly Tables - review.xlsx'

Results in

'2019 Monthly Data - Review.xlsx'*  '2019 Monthly Tables - Review.xlsx'*

What setting am I missing out on to get this to work like it did in ksh93?

Advertisement

Answer

Your filename has capital R Review while your glob uses lowercase review.

Your local shell most likely has nocaseglob enabled to do case insensitive globbing. If this used to work on ksh93, it probably had a similar option enabled by default as well.

In bash you have to enable it explicitly in a script with shopt -s nocaseglob

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement