1830239 Members
2066 Online
109999 Solutions
New Discussion

Re: Advanced scripting

 
SOLVED
Go to solution
Edward_12
Occasional Contributor

Advanced scripting

Dear Collegues;

I have a listing of files as:
-rw-rw-r-- 1 bit abs 19 Mar 3 09:55 BRF.200302.TXT1
-rw-rw-r-- 1 bit abs 88 Mar 3 09:56 BRF.200302.TXT2
-rw-rw-r-- 1 bit abs 60 Mar 3 09:56 BRF.200302.TXT3
...

I want to write a script called "rname.sh" that changes '.' to '_' for all files at once and then call another script "prcess.sh' on each renamed file .

This script may be called as $rname.sh BRF.200302* or $rname.sh BRF.*

So far I do all steps one at a time manually.

Thanks for you help.
Regards;
Edward
Jesus' King
4 REPLIES 4
Balaji N
Honored Contributor

Re: Advanced scripting

here is a snippet. put in the script and run.

for i in `ls -1`
do
mv $i `echo $i | sed 's/\./_/g'`
done.

hth
-balaji
Its Always Important To Know, What People Think Of You. Then, Of Course, You Surprise Them By Giving More.
Bjoern Myrland
Advisor

Re: Advanced scripting

similar as last post but i'll post it anyways :)
(rname.sh)

#! /bin/bash

export PATH=$PATH:.

for i in $(ls BRF*)
do
FNAME=$(echo $i|sed -e 's/\./_/g')

mv $i $FNAME
prcess.sh $FNAME
done
Stuart Browne
Honored Contributor
Solution

Re: Advanced scripting

Given that you want to pass the file-names on the command line, then you'll need to do something slightly different to what has been said.

I'll grab the previous script, and modify it to use command-line given arguments:

------------------------------
#!/bin/bash

while [ ! -z "$1" ]
do
FNAME=$(echo $1|sed -e 's/\./_/g')

mv $1 $FNAME
prcess.sh $FNAME
shift
done
------------------------------

As you are passing it the file-name glob, the shell will automatically expand it, and just list a whole long list of file-names.

The 'while', coupled with the 'shift' will keep looping around until it's worked it's way through all of the arguments.

Instead of checking if the string "$1" is not empty (! -z), we could could instead check to see if the file exists (-f), or if it's readable (-r), or some other check.

The 'PATH' is un-necessary (infact could be set explicitly to "PATH=/bin", but..) so I removed it from the example.

Hope this helps you.
One long-haired git at your service...
Edward_12
Occasional Contributor

Re: Advanced scripting

Thanks Alot, while I didn't deplore your sujjestions as is, they really helped me figure out how to write the required script.
Jesus' King