Hi,

I'm trying to write a shell script that accepts wildcards as arguments. But instead of the wildcards expanding into the list of files from the current directory, I'd like to have them expand into files from another directory.

For example,
I have a script called test.sh in the folder /home/skulkarni
The folder listing for /home/skulkarni is as follows
#ls
test.sh
abc.txt
abd.txt

When test.sh is given a wildcard argument *, is it possible (either directly or through a workaround) for the script to interpret the wildcard on a different folder.

Thank you,
With regards,
Srihari Kulkarni

#!/bin/ksh
ls /home/FTPXFER/$1

finds files in the /home/FTPXFER path. The example below was run with the current working directory set to /home/jmcnama

usage:

test.sh '*'

output:

/home/FTPXFER/UX1006                 /home/FTPXFER/curocfix               /home/FTPXFER/premd_0404
/home/FTPXFER/UX1617                 /home/FTPXFER/curocsbp               /home/FTPXFER/premd_04_11
/home/FTPXFER/UXDEV3a                /home/FTPXFER/curocsfix              /home/FTPXFER/premd_04_12
/home/FTPXFER/UXURR                  /home/FTPXFER/custd_04_12            /home/FTPXFER/premd_04_19
/home/FTPXFER/UZBBMSG.DAT            /home/FTPXFER/custd_04_19            /home/FTPXFER/premd_04_22
/home/FTPXFER/UZBMSGT.DAT            /home/FTPXFER/custd_04_22            /home/FTPXFER/premd_04_25
/home/FTPXFER/UZBSRAT.DAT            /home/FTPXFER/custd_04_25            /home/FTPXFER/premd_0525
/home/FTPXFER/UZBSTUF.DAT            /home/FTPXFER/custd_05_02            /home/FTPXFER/premd_05_02
/home/FTPXFER/UZBVRAT.DAT            /home/FTPXFER/custd_bad_04_12        /home/FTPXFER/premd_05_03
/home/FTPXFER/UZBWDIS.DAT            /home/FTPXFER/custd_gasall           /home/FTPXFER/premd_0928
/home/FTPXFER/UZPNCOA.LIS            /home/FTPXFER/custd_gasall_old       /home/FTPXFER/premd_bad_04_11
/home/FTPXFER/UZWIND

Jim,
Thank you very much for your response.
I'm interested in handling the wildcards through the script itself rather than altering the command line. Referring to your code below, I'd be interested in the usage test.sh * and list the contents of a different folder.

I'm sorry to have missed out this point earlier.

Thanks,
Srihari

#!/bin/ksh
ls /home/FTPXFER/$1

finds files in the /home/FTPXFER path. The example below was run with the current working directory set to /home/jmcnama

usage:

test.sh '*'

output:

/home/FTPXFER/UX1006                 /home/FTPXFER/curocfix               /home/FTPXFER/premd_0404
/home/FTPXFER/UX1617                 /home/FTPXFER/curocsbp               /home/FTPXFER/premd_04_11
/home/FTPXFER/UXDEV3a                /home/FTPXFER/curocsfix              /home/FTPXFER/premd_04_12
/home/FTPXFER/UXURR                  /home/FTPXFER/custd_04_12            /home/FTPXFER/premd_04_19
/home/FTPXFER/UZBBMSG.DAT            /home/FTPXFER/custd_04_19            /home/FTPXFER/premd_04_22
/home/FTPXFER/UZBMSGT.DAT            /home/FTPXFER/custd_04_22            /home/FTPXFER/premd_04_25
/home/FTPXFER/UZBSRAT.DAT            /home/FTPXFER/custd_04_25            /home/FTPXFER/premd_0525
/home/FTPXFER/UZBSTUF.DAT            /home/FTPXFER/custd_05_02            /home/FTPXFER/premd_05_02
/home/FTPXFER/UZBVRAT.DAT            /home/FTPXFER/custd_bad_04_12        /home/FTPXFER/premd_05_03
/home/FTPXFER/UZBWDIS.DAT            /home/FTPXFER/custd_gasall           /home/FTPXFER/premd_0928
/home/FTPXFER/UZPNCOA.LIS            /home/FTPXFER/custd_gasall_old       /home/FTPXFER/premd_bad_04_11
/home/FTPXFER/UZWIND

You have to understand what globbing is. the * character is automatically globbed (turned into matching file names) when the shell sees it. This has to do with shell settings WHEN you enter the command

test.sh *

In order to do what you want you will have to turn off globbing.

Read the info file on bash to turn off/on globbing. Normally you would put this command in .bash_profile for bash. BTW- It will mess up every other script you have already written. That's why we use the '*' trick. Or write a wrapper.

The alternative is to write a wrapper script that does these things:
turn off globbing
ask for the parameter(s)
execute test.sh {paramters]
exit

test.sh will have to turn globbing back on when run like this


In ksh you put

set -o noglob

in /etc/profile or execute it from the command line to turn off globbing

set +0 noglob

truns globbing back on.

This article has been dead for over six months. Start a new discussion instead.