[Cialug] [OT]: Shell script globbing
Daniel A. Ramaley
daniel.ramaley at DRAKE.EDU
Thu Dec 20 13:05:52 CST 2007
The script will be in sh. For testing i was using bash (my login shell)
but since then i've confirmed that real sh behaves the same. The test
machine is Solaris, where sh is really sh and not bash running in
compatibility mode.
I did a bit of poking around on the machine. On Linux boxes, "[" is
usually a symlink to "test", but i think bash (and bash when it is
symlinked to sh) usually uses a built-in function instead. Solaris
doesn't have "[", so the shell must be using a built-in to process the
conditional rather than calling the external program.
How easy is it to switch to ksh? I've never used it, but it is fairly
standard on *nix systems, isn't it?
On Thursday 20 December 2007 11:28, Major Stubble wrote:
>What shell are you using?
>
>ksh doesn't seem to have any issue with your first attempt:
>
>cthulu at rlyeh> touch file.bad
>cthulu at rlyeh> if [ -e *.bad ]; then echo bad; fi;
>bad
>cthulu at rlyeh> touch file2.bad
>cthulu at rlyeh> touch file3.bad
>cthulu at rlyeh> if [ -e *.bad ]; then echo bad; fi;
>bad
>cthulu at rlyeh> echo $SHELL
>/bin/ksh
>
>On Dec 20, 2007, at 11:21 AM, Daniel A. Ramaley wrote:
>> I have what should be a simple shell scripting problem. If any .bad
>> files exist, then the script should do some stuff (display an
>> error, do
>> some cleanup, and exit, though for testing i've simplified it). So
>> far i've come up with 2 solutions, one requiring a variable and
>> running "ls", the other requiring a seemingly superfluous "for"
>> loop. Is there a
>> more elegant solution i've not discovered?
>>
>> My first attempt, doesn't work because -e expects only 1 argument
>> and there may be multiple .bad files:
>> if [ -e *.bad ] ; then
>> echo Bad
>> fi
>>
>> My first working solution, letting ls handle the glob and doing a
>> string
>> comparison on the result:
>> GLOB=`ls *.bad 2> /dev/null`
>> if [ -n "$GLOB" ] ; then
>> echo Bad
>> fi
>>
>> My second solution; the "if" is required because if no .bad files
>> exist
>> the loop will still run once with file set to the literal "*.bad":
>> for file in *.bad ; do
>> if [ -e $file ] ; then
>> echo Bad
>> fi
>> done
>>
>> --------------------------------------------------------------------
>>-- --
>> Dan Ramaley Dial Center 118, Drake
>> University
>> Network Programmer/Analyst 2407 Carpenter Ave
>> +1 515 271-4540 Des Moines IA 50311 USA
>> _______________________________________________
>> Cialug mailing list
>> Cialug at cialug.org
>> http://cialug.org/mailman/listinfo/cialug
>
>_______________________________________________
>Cialug mailing list
>Cialug at cialug.org
>http://cialug.org/mailman/listinfo/cialug
--
------------------------------------------------------------------------
Dan Ramaley Dial Center 118, Drake University
Network Programmer/Analyst 2407 Carpenter Ave
+1 515 271-4540 Des Moines IA 50311 USA
More information about the Cialug
mailing list