Art Alexion on 22 Feb 2006 16:56:58 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] bash script help needed


bergman@merctech.com wrote:

>What doe the echo statements display before the error message? Are they:
>
>	OLD="*"
>	NEW="*"
>	mv "*" "*"
>  
>
No. They show exactly what I expect and want.

>That's what I'd expect if there are no files or directories in the current directory.
>  
>
But there are.

>=> 
>=> even though $NEW does not yet exist, so I am not really moving multiple
>=> files.
>
>The issue isn't that $NEW doesn't exist, but that the initiall expansion of the 
>"*" wildcard failed to produce a list of files to be renamed, thus the literal 
>character "*" is passed to the mv command. When the shell gets that command, it 
>tries to expand "*", producing the error.
>
>=> 
>=> What am I doing wrong?
>
>Well, what you're "doing wrong" is not checking the results of the 
>metacharacter expansion before calling mv(1). The action of mv will vary, depending 
>on what files and directories are in the directory where the script is 
>executing.
>  
>
Actually there is a lot of checking in the script before I get into the
loop.  I just included the loop because that is where I thought the
problem was.

Here is the checking stuff

# See if a command line arg was passed
if [ ! -z $1 ]; then
  # We have a command line arg - was it a directory??
  if [ -d $1 ]; then
    # Wonderful! We were given a directory to clean up!
    echo -n "Changing to $1 - "
    cd $1 2&>/dev/null
    if [ $? = 0 ]; then
      # YAY! We're in the directory
      echo "DONE"
    else
      # Couldn't change to the directory...WTF?!
      echo "Couldn't change to $1! ERROR!!"
      exit 1
    fi
  else
    # It wasn't a directory - bail out!
    echo "That's NOT a directory (idiot!) :P"
    exit 1
  fi
fi

>
>I'd suggest that you go back and take another look at the thread "Interactive 
>rm" from a couple of weeks ago. That thread deals with this very issue--how the 
>shell handles wildcard expansion.
>
>You may also want to look at:
>
>	http://www.oreilly.com/catalog/bash3/
>	http://www.oreilly.com/catalog/shellsrptg/
>	
>  
>
I just bought Ken O. Burch, "Linux Shell Scripting with Bash" as I think
it is time to learn more about these issues instead of just trying to
adapt existing scripts through trial and error.

-- 

_______________________________________
Art Alexion
Arthur S. Alexion LLC

PGP fingerprint: 52A4 B10C AA73 096F A661  92D2 3B65 8EAC ACC5 BA7A
The attachment -- signature.asc -- is my electronic signature; no need for alarm.
Info @ http://mysite.verizon.net/art.alexion/encryption/signature.asc.what.html

Key for signed PDFs available at
http://mysite.verizon.net/art.alexion/encryption/ArthurSAlexion.p7c
The validation string is TTJY-ZILJ-BJJG.
________________________________________

Attachment: signature.asc
Description: OpenPGP digital signature

___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug