LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-16-2006, 02:34 AM   #1
daleo
LQ Newbie
 
Registered: Nov 2005
Posts: 7

Rep: Reputation: 0
Need bash script!!!


Hi!

Need a bash script that will be run with input parameters <directory>,<current_suffix>,<needed_suffix>
and will change all filenames suffixes in this diretory and all subdirectories.
 
Old 11-16-2006, 02:39 AM   #2
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
So go a head an write it.
Check out the bash introduction and advance bash scripting tutorials on tldp.org.
The one you want should not be that hard to do even if you don't have experience with bash scripting. I think the script you want is even in the bash intdocution tutorial.

Greets zhjim
 
Old 11-16-2006, 04:53 AM   #3
uncle-c
Member
 
Registered: Oct 2006
Location: The Ether
Distribution: Fedora 14, Ubuntu , Slax 5.1.8, OpenSolaris, Centos 4.8
Posts: 296

Rep: Reputation: 30
Hi ! I'm sure many others will tell you that your question sounds very "homeworkish" The way to make the most out of the forum is to attempt to tackle the problem and post where you think you are going wrong. The folk here will always try to nudge you in the right direction but at the same time making you fathom out the problem yourself. That way you will actually find the solution yourself and understand how you got there thus giving you a better understanding of the OS. As the previous poster said, the solution is quite straightforward all you have to do is to read a simple bash tutorial.
Apologies for being so harsh But as you can see non-homeworkish problems are very swiftly answered so people on the forum are not bad they just think in the way which would benefit us all.
If your problem isn't homework related then please accept my profound apologies.
Cheers,

Uncle

ps: Many many many moons ago I got told off on the Usenet for posting a similarly versed question ! When I sat down with some text the problem became quite straightforward to solve !!
 
Old 11-16-2006, 06:31 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
The "Parameter Expansion" section of the bash manual page will explain why this might be part of your solution:
Code:
nn="${on%$oe}$ne"  # o and n mean old and new, you can guess the rest.
 
Old 11-16-2006, 06:35 AM   #5
daleo
LQ Newbie
 
Registered: Nov 2005
Posts: 7

Original Poster
Rep: Reputation: 0
Okay, guys.. I understand your position in that question. I sniffed through advanced bash guide,a s was advised above, and found that way of solving my problem:

#!/usr/local/bin/bash

ARGS=2
BAD_ARGS=65

if [ $# -ne "$ARGS" ]
then
echo "using order: `basename $0` old_suffix new_suffix"
exit $BAD_ARGS
fi

for filename in *.$1
do
mv $filename ${filename%$1}$2
done

exit 0


But this works ony in the current directory, while I need some recursive work over subdirectories also.

This time I faced the problem. Can I use such construction? :

#!/bin/bash
ARGS=2
BAD_ARGS=65

if [ $# -ne "$ARGS" ]
then
echo "using order: `basename $0` old_suffix new_suffix"
exit $BAD_ARGS
fi

function change_suffix()
{
for filename in *.$1
do
mv $filename ${filename%$1}$2
done
}

DIRS=$(find . -type d);

for DIR in ${DIRS}
do
if [ -d "${DIR}" ];
then
(cd $DIR;change_suffix; cd -
fi
done

exit 0
 
Old 11-16-2006, 07:10 AM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Use [CODE] tags to format your script nicely in the forum.

You can use the find command to recurse down sub-directories. find is a wonderful tool, but it's utterly confusing to everyone for at least 6 months. find will print out a list of files / directories (depending on the options used), which you can then recurse over in the shell with "while read filename; do ... done" style loop. Anything which creates a list of stuff with one item per line can be piped into such a loop, e.g.
Code:
locate ht-fonts/unicode |while read f; do echo "we found $f"; done
 
Old 11-16-2006, 10:33 AM   #7
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
Daleo your getting there. Not quite able to help your there but for testing just use some echo command inside your function.

Oh man matthew your are pulling trick that are even new to me *thumbs up* (or might it that i don't get your crytion), but I think its not very wise to say something about the find command and then give an example with the locate command.

Just for the fun. Is there a way do to it without find. solely with ls -R.

Greets zhjim
 
Old 11-16-2006, 11:18 AM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Quote:
Originally Posted by zhjim
Oh man matthew your are pulling trick that are even new to me *thumbs up* (or might it that i don't get your crytion), but I think its not very wise to say something about the find command and then give an example with the locate command.
You're right... But confusion is the seed of documentation reading, is the seed of true knowledge

Quote:
Originally Posted by zhjim
Just for the fun. Is there a way do to it without find. solely with ls -R.
You know that didn't even occur to me. It's a good point. I find the find command so useful now I know more or less how it works, that I often over-look simpler solutions. Having said that, getting ls -R to show only files with some extension isn't so nice - you have to use grep and then you're just adding another command into the thing. Even so ls -R and grep is probably easier to understand than find's cryptic options.
 
Old 11-21-2006, 05:51 PM   #9
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by daleo
function change_suffix()
{
for filename in *.$1
do
mv $filename ${filename%$1}$2
done
}
The use of $1 and $2 wont give you the desired values. Normaly they contain the values of commandline arguments. But when it comes to calling function the behave like argument. Means inside a function you would get the arguments of the function call.

So you would need to pass the original commandline arguments as an argument to the function. Also your way of cd'ing to the directorys does not seem right to me. Try to build a function that takes a directory as an argument and have it call it self if it finds another dir else just change suffix.

I have this function here somewhere. ... Somewhere. You would also need to build a complete path or cd somehow. Options are yours.

Or just use find. Exclamationmark. Point. Point.

Quote:
Originally Posted by matthewg42
You know that didn't even occur to me. It's a good point. I find the find command so useful now I know more or less how it works, that I often over-look simpler solutions. Having said that, getting ls -R to show only files with some extension isn't so nice - you have to use grep and then you're just adding another command into the thing. Even so ls -R and grep is probably easier to understand than find's cryptic options.
The ls -R was a first shot from mind. And a daemon to over come when it comes to script that do a certain task for each file. I'm just here for the challenge. Just my way (like linux?)

Beside i never realy got into find till I knew that it was part of lpi 102 exam and capabale of showing/finding acl's. Also while trying to get a grip on find I did not find it to hard to use it. But truly spoken it depend on the job you want to be done

Best Regards zhjim
 
  


Reply


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
Bash script hangs upon starting another script in the background masea2 Linux - Software 4 11-13-2006 06:18 AM
Bash script - executing a script through subdirectories bubkus_jones Programming 5 04-24-2006 06:05 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 01:20 AM
bash script - incrementing a filename in a script tslinux Programming 10 08-06-2003 12:58 AM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 06:34 AM


All times are GMT -5. The time now is 08:33 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration