MrBoe
Routinier
Hallo!
Ich beginne grade mich etwas in shell Programmierung einzuarbeiten. Ich habe mir ein Skript größtenteils geklaut und ein bisschen daran herumprobiert. Unter anderem habe ich einen Counter eingebaut, der seinen initialen Wert aus einer Datei liest.
Mag man über das Skript denken was man will.
Ich habe ein Problem.
Es funktioniert soweit ganz gut. Allerdings kann es nicht mit Dateinamen umgehen, die Leerzeichen enthalten. Wie kann ich das ändern?
Ich beginne grade mich etwas in shell Programmierung einzuarbeiten. Ich habe mir ein Skript größtenteils geklaut und ein bisschen daran herumprobiert. Unter anderem habe ich einen Counter eingebaut, der seinen initialen Wert aus einer Datei liest.
Mag man über das Skript denken was man will.
Ich habe ein Problem.
Es funktioniert soweit ganz gut. Allerdings kann es nicht mit Dateinamen umgehen, die Leerzeichen enthalten. Wie kann ich das ändern?
Code:
#! /bin/bash
#
# NAME: number_files_01.sh
#
# SHELLS: bash
#
# PURPOSE: utility that numbers the files in a directory consecutively
#
# USAGE: number_files_01 <directory>
#+ As in "number_files_01 ~/Bilder/Urlaub_2006/", which numbers all picture files in the specified directory
if [ -z "$1" ] # check whether a command line argument was specified by the user
then
echo "Usage: `basename $0` <directory>" # error message
exit 65 # stop execution of the script
elif [ ! -d $1 ] # check whether the argument specified by the user is really a directory
then
echo "$1 is not a directory!" # error message
exit 1 # stop execution of the script
fi # end of first IF-clause
COUNTER=$(cat nummer) # establish a counter for the new filenames
cd $1 # change the current directory to the one specified by the user
for FILENAME in *.jpg # traverse all pictures in the directory specified
do # begin loop
if [ -f "$FILENAME" ] # check whether there is a file to rename
then
let "COUNTER += 1" # increase the counter
new_name=`printf %06d.jpg $COUNTER` # generate the new filename: consisting of 4 digits
if [ ! -d ./tmp ] # check whether a subdirectory already exists
then
mkdir tmp 2> /dev/null # try to create a temporary directory in $1
if [ $? -ne 0 ] # check whether the subdirectory was generated
then
echo 'The necessary subdirectory could not be created!' 1>&2 # error message
exit # stop execution of the script
fi # end of fourth IF-clause
fi # end of third IF-clause
cp -i $FILENAME ./tmp/$new_name # the combination of "cp" and "rm" seems more safe than the plain "mv"
if [ $? -eq 0 ] # check whether the file was copied
then
rm $FILENAME # delete the original file
fi # end of fifth IF-clause
echo $COUNTER > nummer
fi # end of second IF-clause
done # end of the loop
mv ./tmp/*.jpg $1 # move the renamed files to the directory specified by the user
if [ $? -eq 0 ] # check whether the files could be moved
then
rm -r ./tmp # delete the temporary directory
else
echo "The renamed files are still in the directory ./tmp ..."
fi # end of sixth IF-clause
exit 0 # end of the script