LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-19-2007, 08:24 AM   #1
me4linux
Member
 
Registered: Aug 2006
Distribution: Fedora 7
Posts: 165

Rep: Reputation: 30
Problem with recursive bash script


Hi everyone

I have written recursive script to rename the files in the given folder such that the folder name is appended to the filename in the beginning.
I am getting following syntax error when I have run the script.I couldnt find any syntax error in it.

[linux@localhost myscripts]$ ./rename ./test
./rename: line 7: syntax error near unexpected token `else'
./rename: line 7: `else'
the script is as follows


# Name of the script is rename
#!/bin/sh
for FILE in $1
do

if [`file $FILE | grep directory`] then
echo $1 is a directory
./rename $FILE
else
echo $FILE is renamed to ${1}_${FILE}
mv $FILE ${1}_${FILE}
fi

done


can any one help to correct the mistake in this script

THANKS IN ADVANCE

bYe

Last edited by me4linux; 03-19-2007 at 08:28 AM.
 
Old 03-19-2007, 08:34 AM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 326Reputation: 326Reputation: 326Reputation: 326
Code:
if [`file $FILE | grep directory`] then
should be:

Code:
if [`file $FILE | grep directory`] ; then
 
Old 03-19-2007, 08:41 AM   #3
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 52
Code:
if [ -d $FILE ] ; then
echo "$FILE is a directory"
fi
 
Old 03-19-2007, 08:54 AM   #4
omnio
Member
 
Registered: Feb 2007
Location: $HOME
Distribution: Hardened Gentoo
Posts: 66
Blog Entries: 1

Rep: Reputation: 16
Code:
#!/bin/sh

for FILE in $1/*
do
    if [ -d $FILE ] ; then
        echo $1 is a directory
        ./rename $FILE
    else
        echo $FILE is renamed to $(dirname $FILE)/$1_$(basename $FILE)
        mv $FILE $(dirname $FILE)/$1_$(basename $FILE)
    fi
done
and it is launched from outside the directory, like:
Code:
./rename test
EDIT: changed the "mv" destination and the "for" statement.

Last edited by omnio; 03-19-2007 at 09:38 AM.
 
Old 03-19-2007, 12:23 PM   #5
me4linux
Member
 
Registered: Aug 2006
Distribution: Fedora 7
Posts: 165

Original Poster
Rep: Reputation: 30
[QUOTE=omnio]
Code:
    else
        echo $FILE is renamed to $(dirname $FILE)/$1_$(basename $FILE)
        mv $FILE $(dirname $FILE)/$1_$(basename $FILE)
    fi
done
hi omnio

can u explain me the above two lines ...
I didnt come across dirname and basename till now....
What mistake is there in my approach...
Code:
else
echo $FILE is renamed to ${1}_${FILE}
mv $FILE ${1}_${FILE}
fi
Please let me know the mistake in my code .......!!!

Sorry to say that script is not working ...

I have tried with the new code but its showing cannot rename
the file just under test directory is renamed...
but the files in the subdirectories are not getting renamed...
I tried using both ...i.e ./test and test as the command line arguments.....


[linux@localhost bash_script.tar.gz_FILES]$ ./rename ./test
./test is a directory
./test/1/f is renamed to ./test/1/./test/1_f
mv: cannot move `./test/1/f' to `./test/1/./test/1_f': No such file or directory
./test is a directory
./test/2/f is renamed to ./test/2/./test/2_f
mv: cannot move `./test/2/f' to `./test/2/./test/2_f': No such file or directory
./test/file is renamed to ./test/./test_file
[linux@localhost bash_script.tar.gz_FILES]$ ./rename test
test is a directory
test/1/f is renamed to test/1/test/1_f
mv: cannot move `test/1/f' to `test/1/test/1_f': No such file or directory
test is a directory
test/2/f is renamed to test/2/test/2_f
mv: cannot move `test/2/f' to `test/2/test/2_f': No such file or directory
test/file is renamed to test/test_fil

bye

Last edited by me4linux; 03-19-2007 at 12:39 PM.
 
Old 03-19-2007, 03:36 PM   #6
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by me4linux
I have written recursive script to rename the files in the given folder such that the folder name is appended to the filename in the beginning.
You mean prepended to the filename.
Quote:
I am getting following syntax error when I have run the script.I couldnt find any syntax error in it.

[linux@localhost myscripts]$ ./rename ./test
./rename: line 7: syntax error near unexpected token `else'
./rename: line 7: `else'
the script is as follows


# Name of the script is rename

It is not a good idea to use the name of a command that is included in your system.
Quote:
#!/bin/sh
for FILE in $1
do

if [`file $FILE | grep directory`] then
Code:
if [ -d "$FILE" ]; then
Quote:
echo $1 is a directory
./rename $FILE
This will produce an endless loop. If you call rename with the name of a directory, it will call itself again and again ...

Somewhere, you need to cd into the directory.
Quote:
else
echo $FILE is renamed to ${1}_${FILE}
mv $FILE ${1}_${FILE}
If $FILE contains path information, you will be asking to move the file into a probably non-existent directory.

If $FILE contains spaces (or other pathological characters), this will fail even if there is no path information. Quote your variables:
Code:
mv "$FILE" "${1}_${FILE}"
Quote:
fi

done


can any one help to correct the mistake in this script

Try this:
Code:
ren()
(
  cd "$1" || return
  for file in *
  do
    if [ -d "$file" ]
    then
      printf "%s is a directory\n"  "$file" >&2
      ren "$file"
      continue
    fi
    printf "%s is renamed to %s\n" "$file" "${PWD##*/}_$file"
    mv "$file" "${PWD##*/}_$file"
  done
)

ren "${@:-.}"
 
Old 03-20-2007, 05:46 AM   #7
omnio
Member
 
Registered: Feb 2007
Location: $HOME
Distribution: Hardened Gentoo
Posts: 66
Blog Entries: 1

Rep: Reputation: 16
Obviously, cfaj was right in everything he said.

Quote:
Originally Posted by me4linux
I didnt come across dirname and basename till now....
You don't *have to* use them, but here is a quite clear explanation of them: http://www-128.ibm.com/developerwork...sh.html#N100E0


Quote:
Originally Posted by me4linux
What mistake is there in my approach...
No mistake, just some things need to be added.

Here is a slightly less complicated code (actually I like cfaj's style more but maybe this will look less cryptic to you):

Code:
#!/bin/sh
# don't name this script "rename".

do_rename() {
	[ -d "$1" ] || { echo "error: you must specify A DIRECTORY" ; exit 1 ; }
	echo ; echo "$1 is a directory, now descending into it."

	# enter this directory
	cd "$1"
	# preserve this directory name
	local MYDIR="${PWD}"

        # make sure only the last part of a path is prepended;
        # this also helps when an absolute path is passed as argument.
	PREPEND=$(basename "$1")

	for FILE in *
	do
		if [ -d "${FILE}" ]  ; then
			do_rename "${FILE}"
			# now exit from this directory
			cd "${MYDIR}"
		else
			echo "${FILE}" is renamed to "${PREPEND}_${FILE}"
			mv "$FILE" "${PREPEND}_${FILE}"
		fi
	done
}
do_rename "$1"
As far as I can tell, both scripts (cfaj's and mine) work with both relative and absolute paths, so you can launch them like this:

Code:
./rename mydir

-- or--

./rename /path/to/mydir
I don't know why you insist on this:
Code:
./rename ./mydir
since it is a really different fish; see this: http://mdobson.home.cern.ch/mdobson/...scripting.html

Sorry for the previous broken code, I was on a rush. Cheers.

EDIT: changed the code to work with absolute paths.

Last edited by omnio; 03-20-2007 at 07:46 AM.
 
  


Reply

Tags
rename, shell


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Recursive Upload Script Kenji Miyamoto Linux - General 1 09-01-2005 11:18 PM
Help with a recursive chmod script in bash lowpro2k3 Programming 11 07-25-2005 07:03 PM
Bash Programming, Recursive/Iterative Calls on Folder Contents gtwilliams Linux - Newbie 1 07-06-2005 06:44 PM
Recursive search in bash scripting ! zulfilee Linux - Software 3 12-12-2004 10:40 PM
modify bash script - recursive action xscousr Programming 6 09-17-2003 01:52 PM


All times are GMT -5. The time now is 10:53 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration