Re: help in shell scripting

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, 2003-11-02 at 02:22, Nabin Limbu wrote:
> Hi,
> 
> In my script, I need to read a file name and change its first 
> character of file name with another predefined character. How can I 
> do that with shell scripting.
> 
> for example:
> 
> if A = "glow"
> 
> then, B should be "blow"  ("g" replaced by "b")
> ( glow  -> blow )
> 

While this is possible, depending on the data, using bash parameter
expansion, I think it would get kind of hairy to bullet proof it.

[bhughes@bretsony bhughes]$ echo $myfile
blow
[bhughes@bretsony bhughes]$ newfile=${myfile/b/g}
[bhughes@bretsony bhughes]$ echo $newfile
glow

this does weird things when trying to work with unknown input data:

for instance:

[bhughes@bretsony bhughes]$ myfile=ablob
[bhughes@bretsony bhughes]$ newfile=${myfile/b/g}
[bhughes@bretsony bhughes]$ echo $newfile
aglob

man bash 
search for Parameter Expansion

you could then do something like 

[bhughes@bretsony bhughes]$ for myfile in b*; do echo mv $myfile ${myfile/b/g};done
mv backup gackup
mv bigemail.txt gigemail.txt
mv bin gin
mv browser.xul growser.xul
[bhughes@bretsony bhughes]$ 


remove the echo in the do loop and the mv stament would be run

This does not check for file type or perms but should of course.

I would use perl or awk or sed or anything that understands regexps for
this so you can test the first char only easily.

HTH

Bret


-- 
redhat-list mailing list
unsubscribe mailto:redhat-list-request@xxxxxxxxxx?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/redhat-list

[Index of Archives]     [CentOS]     [Kernel Development]     [PAM]     [Fedora Users]     [Red Hat Development]     [Big List of Linux Books]     [Linux Admin]     [Gimp]     [Asterisk PBX]     [Yosemite News]     [Red Hat Crash Utility]


  Powered by Linux