[NTLUG:Discuss] Shell script help

. Daniel xdesign at hotmail.com
Fri May 12 10:08:56 CDT 2006


Attachments don't work.  I'll cut'n'paste.  But I did try the opposite 
approach and it worked.  Still, I wonder why the -f $PATHLIST didn't 
respond that it was a file?

Okay, I didn't save the older version and this hotmail didn't save the sent 
email.  Oh well.  Here's what I have now anyway.  Perhaps you can just 
IMAGINE what the opposite logic was like... that is the "if" looked like if 
[ -f $PATHLIST ] then ----  Now the test is "if [ -d $PATHLIST ] then else 
---"  

For some reason, the test for being a file comes back negative when the 
filenames are not english.  However, testing to see if it's a directory and 
processing the name if it's "else" works just fine.  Any clues?

Here's the script in the current form:
-------------------------
#!/bin/sh

PATHLIST=$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS
echo "DEBUG: $PATHLIST" > ~/debug.out
set >> ~/debug.out
echo `pwd` >> ~/debug.out

if [ "$PATHLIST" == "" ]
 then
  PATHLIST=$1
fi
echo "DEBUG:" >> ~/debug.out

if [ -d $PATHLIST ]
 then
  echo "DEBUG: The script thinks it's a directory already." >> ~/debug.out
 else
  echo "DEBUG: The script doesn't think this is a directory." >> 
~/debug.out
#  PATHLIST=`dirname $PATHLIST`
  PATHLIST=${PATHLIST%/[^/]*}
fi
echo "DEBUG: $PATHLIST" >> ~/debug.out
cd $PATHLIST
echo `pwd` >> ~/debug.out

gnome-terminal --geometry=132x25
-------------------------


>
>Okay, so I have found that my script failure is somewhere 
>unexpected.
>
>I have attached the actual working script and the debug.out file.  
>For some reason, the code I added to test if the pathlist is to a 
>file or not, thinks it's not a file.  Any ideas?  I'll also try the 
>opposite approach of checking to see if it's a directory and if not, 
>perform the conversion to see how that goes...
>
>
>>. Daniel wrote:
>>     >Your technique is very interesting but incredily cryptic.
>>     >How does that work??
>>
>>It's something that the Shell does in the evaluation/expansion of
>>its variables.  Do a 'man bash' and search for the section entitled
>>'VARIABLE EXPANSION'.
>>
>>     echo ${a%/[^/]*}
>>
>>This says echo the following argument.  The argument is a shell 
>>variable
>>$a or ${a} -- they mean the same thing.  The stuff after the 
>>percent sign
>>is a regular expression which means a slash followed by any number 
>>of
>>characters, zero or more, EXCEPT there can be no slashes '/'.  The 
>>percent
>>sign means remove the smallest possible match of this at the tail 
>>end of
>>the value of the variable 'a'.
>>
>>     >It works, but HOW?
>>
>>He he he... it's a shell game [pun intended]. :-D
>>The nice thing is that the 'dirname' program doesn't need to 
>>started
>>which can save you a bundle of time if this expression is inside a
>>loop of some kind.
>>
>>     >Anyway, both methods are working... well except for one 
>>glitch:
>>     >
>>     >I'm a WEIRD user in that I often work with filenames in other
>>     >languages...  in my case, Japanese.  So if a filename is in
>>     >Japanese, it breaks this somehow.  It breaks both methods.
>>     >
>>     >No, scratch that... they both work from the command-line...
>>     >it must be something else... now I have to figure out what's
>breaking!
>>
>>I can't think of a case where it wouldn't but /bin/sh should be 
>>/bin/bash,
>>or linked that way.
>>
>>Let me know if I help with any more shell questions.
>>
>>	-Rusty-
>>--
>>    _____        Rusty Haddock  =  KD4WLZ  =  
>>rusty at fe2o3.lonestar.org
>>|\/   o \   o  **Out yonder in the Van Alstyne (TX) Metropolitan 
>>Area**
>>|   (  -<  O o              Microsoft is to software what
>>|/\__V__/                  McDonalds is to gourmet cooking
>


>_______________________________________________
>http://ntlug.pmichaud.com/mailman/listinfo/discuss





More information about the Discuss mailing list