[Unix] a dos style rename wont work


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Unix] a dos style rename wont work
# 1  
Old 04-18-2008
[Unix] a dos style rename wont work

Hey guys i'm creating a dos style rename script, so if a user types say q14.* as the 1st param and b14.* as the 2nd and will rename all q14 files to b14 but keep the extensions, so i've developed nearly the full script "i think", if i use echo(echo "if $1 had been renamed it would now be $newfile") to simulate the rename it works fine shows

Quote:
q14.1 will be renamed to b14.1
q14.2 will be renamed to b14.2
.....etc
but it becomes a whole different story if i alter that line to "mv $1 $newFile"


Code:
#!/bin/sh

case $# in
0)echo Usage: dosRename file >&2
	   ;;
*)for file
		do
				case $file in

				[A-Za-z0-9]*\.\*) type=main
					   ;;
				\*\.[A-Za-z0-9]* )type=extension
					   ;;
				esac
		#echo $type
		done

		endFile=$file

		while [ $# -ne 1  ]
		do
				case $type in
				'extension')	extension=`echo $endFile |sed -e 's/\*\./\./'`
								newfile=`echo $1 |sed -e "s/\.[A-Za-z0-9]*/$extension/"`
								echo  "if $1 had been renamed it would now be $newfile"
							 #   mv $1 $newfile
					   ;;
				'main')		 fileName=`echo $endFile | sed -e 's/\.\*//'`
								newFile=`echo $1 | sed -e "s/[A-Za-z0-9]*\.\(.*[A-Za-z0-9]*\)/$fileName\.\1/"`
								echo "if $1 had been renamed it would now be $newFile"
							#	mv $1 $newFile
					   ;;
				esac
				shift 1
		done
	   ;;

esac

it renames the 1st one fine then the rest go along the lines of

dosRename q14.* b14.*
it dose first one fine but 2nd one ends up q14.1.2 and third q14.1.2.3

any ideas what is up with my script?


edit ~ if any help if i include mv or cp the output is as followed
Quote:
if nob.1 had been renamed it would now be b14.1
if nob.1.2 had been renamed it would now be b14.1.1.2
and this is the output if i dont include them

Quote:
if nobby.1 had been renamed it would now be b14.1
if nobby.1.2 had been renamed it would now be b14.1.2

Last edited by fblade1987; 04-18-2008 at 10:33 AM..
# 2  
Old 04-18-2008
The "rename" command already exists on Unix, and does exactly this.

man rename
# 3  
Old 04-19-2008
Quote:
Originally Posted by ShawnMilo
The "rename" command already exists on Unix, and does exactly this.

man rename

No, it doesn't exist on many, if not most, versions of UNIX.

It is included with Linux distros, but there are two different versions with different syntax.
# 4  
Old 04-19-2008
You are using $1 but the loop variable is $file. So your loop loops over the files and puts the name of each in $file, but you are not referring to that variable, you are referring to another variable which continues to point to the first file in the list. Try changing the $1:s to $file:s.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Aliases wont work after set command

Hi, I have setup my .profile with some helpful aliases and some set commands in it. I have it as: alias gr='autorep -G' alias c='clear' alias x='exit' alias wcl='wc -l' alias l1='ls -1 "$@"' alias ll='ls -l "$@"' alias la='ls -altr "$@"' alias l='ls -ltr "$@"' alias ml='m_ls -ltr... (5 Replies)
Discussion started by: grep_me
5 Replies

2. Solaris

ALOM wont work when KVM connected to Sun Fire V440 server

Hi, I was asked to connect a KVM screen to a Sun Fire V440 last night so I connected it up but no joy and nothing on the KVM screen. I was told that a reboot may fix the problem so connected to the ALOM and rebooted. On the plus side, the KVM screen now works but I lost the ALOM connection. ... (0 Replies)
Discussion started by: jimmy54321
0 Replies

3. Solaris

Creating script - grep wont work?

This is my first script and I wont get it working.. sorry for being a total noob but here it is: #./bin/sh -x echo "1:st argument = $1"; echo "2:nd argument = $2"; grep "$1" "$2" In the terminal I write, for example, su.sh sausage sausage.txt Also tried su.sh "sausage" "sausage.txt" but... (2 Replies)
Discussion started by: Prantare
2 Replies

4. Shell Programming and Scripting

2 commands in script wont work together

For some reason when I run these separately they work but when they are in a script file the second wont: printf "%s\n" "$RANDOM"."$RANDOM" "$RANDOM"."$RANDOM" "$RANDOM"."$RANDOM" "$RANDOM"."$RANDOM" | tee $HOME/bpl/random RANDOM1=$( <$HOME/bpl/random ) Anyone know why? (8 Replies)
Discussion started by: pkohn11
8 Replies

5. OS X (Apple)

AjaxTerm wont work on Mac OS X

just downloaded Ajaxterm-0.10 on my Mac 10.5.8 and after a ./ajaxterm.py i get: AjaxTerm at http://localhost:8022/ which looks ok but as soon i go to the website: ./ajaxterm.py:418: DeprecationWarning: 'I' format requires 0 <= number <= 4294967295 fcntl.ioctl(fd,... (0 Replies)
Discussion started by: berot3
0 Replies

6. Windows & DOS: Issues & Discussions

DOS script to grab the first file in a dir and rename it

:confused: Hello, Is there any way to use the dir command / some DOS Script to select only first file of similar pattern of files in a direcotory and rename it for example, one directory has 5 files abc_1005.txt abc_5256.txt abc_2001.txt abc_2003.txt abc_3006.txt by use script I would... (2 Replies)
Discussion started by: raghav525
2 Replies

7. Shell Programming and Scripting

My Wireless Script Wont Work.....anyone??

ive been trying to write a simple (at least i thought it was) script to launch my wireless usb under ubuntu with kde. im using nano. when i run it from a terminal it works fine, but it just wont run from the script. there is a caveat, but first heres the script. ... (7 Replies)
Discussion started by: braze
7 Replies

8. UNIX for Dummies Questions & Answers

head command wont work on MF file

I am trying to do a head on a mainframe file and on doing ti just gives me a blank screen with nothing on it. however, when i do a tail for the same file...i get a few lines on the screen. i know tht mainframe files have all the records on one line...does this have to do something with this.... (10 Replies)
Discussion started by: alfredo123
10 Replies

9. HP-UX

Netscape Wont work...

Cant Get Netscape to work... I get this messsage: Perhaps there is a problem with your name server? if your site must use a non-root name server, you will need to set the $SOCKS_NS environment variable to point at the appropreate name server. It may (or may not) be necessary to set this... (2 Replies)
Discussion started by: Eric_Bakken
2 Replies
Login or Register to Ask a Question