LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need bash script!!! (https://www.linuxquestions.org/questions/linux-newbie-8/need-bash-script-502110/)

daleo 11-16-2006 01:34 AM

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.

zhjim 11-16-2006 01:39 AM

So go a head an write it. :p
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

uncle-c 11-16-2006 03:53 AM

Hi ! I'm sure many others will tell you that your question sounds very "homeworkish" :tisk: ;) 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 !!

matthewg42 11-16-2006 05:31 AM

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.

daleo 11-16-2006 05:35 AM

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

matthewg42 11-16-2006 06:10 AM

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. :D 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

zhjim 11-16-2006 09:33 AM

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

matthewg42 11-16-2006 10:18 AM

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 :D

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.

zhjim 11-21-2006 04:51 PM

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


All times are GMT -5. The time now is 07:40 AM.