|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
Simple Shell script question
Hi guys. Please forgive me as this is my first script of this nature. My previous scripting experience is limited to input file generation and auto execution of scientific software.
I have a directory full of stuff to rename. For such, I have written the following script: Code:
num=0 basename="5FFA45-" for curf in * do ftmp=`echo $basename$num` echo $ftmp #This is where I will be putting the 'mv' command let "num += 1" done Looking at this, my intensions are clear for the most part. I need something that will go through all the files, and will rename them with a common base-name, and add a number that cooresponds to the order in the directory listing. My problem is that I want to be able to preserve file extensions. How can I do this? |
|
#2
|
|||
|
|||
|
Code:
#!/bin/ksh
let count=0
basename="5FFA45-"
for file in *
do
file_ext=${file#*.*}
echo $basename"$count".$file_ext
# change the above line to: mv $file $basename"$count".$file_ext
let count=$count+1
done
exit
-- this will work on files with a single . and with no spaces in their names. |
|
#3
|
|||
|
|||
|
Hmmm. That looks interesting. I'm not sure that I understand some of it.
What do you think of this? Code:
#! /bin/bash
#
# A simple scipt to rename a bunch of files
# in a directory, such that the extensions
# are preserved.
#
# by Timothy A.V. Teatro
#
# Date: 08.19.04
num=0
basename="5FFA45-"
for curf in *
do
ext=`echo $curf | awk -F"." '{ print $2 }'`
ftmp=`echo $basename$num.$ext`
mv "$curf" "$ftmp"
let "num += 1"
done
Can you explain in a little more depth exactly what that line does? I am intruiged by it. Thank you very much for replying. |
|
#4
|
|||
|
|||
|
Code:
file_ext=${file#*.*}
removes everything up to and including the dot in the variable: file. It gets the extension just like your awk statement does, except it's a builtin korn/bash substitution function. and.. Code:
let "num += 1" should be: let num+=1 lose the spaces spaces; you don't really need the quotes. |
|
#5
|
|||
|
|||
|
timtro
did you already try to read&&write sh-scripts ? i know, you && a lot other people like 'ksh' and 'ksh' is still NOT standard, the only really standard is 'bourne' your script is very dangerous, you don't check the existence of the new file... NOTA: - basename is a unix-cmd, don't use it, could be very confusing. - don't abuse of ", use it when it's needed. try something like: Code:
#!/usr/bin/sh
num=0
newname=5FFA45-
for curf
do
ftmp=`echo $curf | sed 's/^.*\(\..*\)/\1/`
[ x$tmp = x ] && continue # extention not found
[ -f $newname$num$ftmp ] && continue # file exists
mv $curf $newname$num$ftmp
num=`expr $num + 1`
# or better num=`echo $num + 1 | bc`
# it's faster by large op
done
|
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Simple Shell script question |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|