Globbing or not globbing

Hi guys,

Here is a simple script. It writes the current time to specific files in a directory.
The arguments are the names of the files to write the date to (without path nor extension).

root:~# cat /usr/local/bin/dummy.sh
#!/bin/sh -e
for file in $@; do
    date >> /var/lib/$file.dat
done

The command

root:~# dummy.sh a1 a2 a3

will write the current time to

/var/lib/a1.dat
/var/lib/a2.dat
/var/lib/a3.dat

I'd like to allow wildcards in the arguments.
But the command

root:~# dummy.sh a*

will try to write the current time to files in /var/lib/ named after files in /root/a* plus the extension .dat.

To achieve my goal, I'd need globbing to be disabled in

for file in $@

but enabled in

date >> /var/lib/$file.dat

How's that doable?

Best regards
Santiago

If you assume that /var/lib/a*.dat will expand to something like /var/lib/ab.dat /var/lib/ac.dat , your command line will become date >> /var/lib/ab.dat /var/lib/ac.dat and will fail. Only the first word after >> will be considered as the file-name to be appended to and the rest of the file-names will be considered as arguments to date .

You will need to call the script like so

dummy.sh a\*.dat 

because the glob expansion happens in the shell, not the script :wink:

You can try something like this dummy.sh 'a*'

--ahamed

That's right elixir_sinari.
Please forgive my mistake and considere the following code :

root:~# cat /usr/local/bin/dummy.sh
#!/bin/sh -e
for arg in $@; do                       # disable globbing
    for file in /var/lib/$arg.dat; do   # enable globbing
        date >> $file
    done
done

Sorry Skrynesaver and ahamed101. Escaping the wildcard doesn't change anything.

#!/bin/sh -e
cd /var/lib
for arg in $@; do
    for file in /var/lib/$arg.dat; do  
        date >> $file
    done
done

then call it this way:
sh -f -c "./dummy.sh a*"
--
Bye

1 Like

Hey Lem,
Thanks for your help.
That still leaves me with one problem.
If run several times, I'll end up with files named a1.dat.dat.dat.dat.

I don't know what is problem here..

see this... works perfect....

$ sh test.sh file*
file
file1
file2
file3
filename
filena_test.xls

$ cat test.sh
#!/bin/sh -e
for file in $@; do
    date >> /home/gerbil/Gerb_jump/$file
        echo $file
done

$ for i in file*; do tail -1 $i; done
Mon Oct  1 09:44:16 EDT 2012
Mon Oct  1 09:44:16 EDT 2012
Mon Oct  1 09:44:16 EDT 2012
Mon Oct  1 09:44:16 EDT 2012
Mon Oct  1 09:44:16 EDT 2012
Mon Oct  1 09:44:16 EDT 2012

Of course. :slight_smile:

Maybe you can avoid it this way:

#!/bin/sh -e
cd /var/lib
for arg in $@; do
    for file in /var/lib/$arg; do  
        date >> $file
    done
done

Then call it this way:
sh -f -c "./dummy.sh a*.dat"
--
Bye

Thanks Lem.
Of course, I could call it this way.
But this trivial script is just the tip of the iceberg.
I required that the name would be given without extension because in my real script, depending on the situation, the extension might change (and only the script knows what extension to use).
So the user should really only give a name and no extension.
Ha! We're stuck.

And what about find? If you don't have file names with spaces or strage chars, please try:

#!/bin/sh
for arg in "$@"; do
        date | tee -a $(find /var/lib -maxdepth 1 -name "$arg") >/dev/null 
done

Usage: ./dummy.sh 'a*' 'b*' 'c*'
--
Bye

1 Like

Thanks Lem,
That's a great idea!
Indeed, I know there won't be any strange names.