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
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 .
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.
$ 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
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.