szukałem dirname przez 'apropos' i nie znalazlem.. a szkoda;)
zroiblęm tak:
Kod
local old_name=`basename "$1"`
local old_path=${1/%"$old_name"/}
Zdecydowanie szybki:) Ale idzie całkiem dobrze udalo mi sie napisac cos takiego:
Kod
#!/bin/bash
# Error codes
# 1 - no pattern chosen
# 2 - no files given
# 3 - user has no rights for filename modification or file does not exist
# 4 - filename with name that is to be assigned to another file already exists
# 5 - mv failed
# 6 - wrong sed expression format
function Modify()
{
#echo "I'm in modify $@"
for file in "$@"
do
# if we talk about .. or . just skip it
if [ "$1" = ".." ] || [ "$1" = "." ]; then
continue
fi
#if user has no rights to modify file exit
if [ ! -w "$1" ]; then
echo "$1 File does not exist or You do not have needed permissions."
exit 3
fi
#if this is a directory and recursion was set penetrate it
if [ -d "$1" ] && [ "$recursion" = "true" ]; then
pushd "$1" > /dev/null 2>&1
#echo "entering $1"
Modify `ls -A`
#echo "leaving $1"
popd > /dev/null 2>&1
fi
#obtain path and filename
local old_name=`basename "$1"`
local old_path=${1/%"$old_name"/}
#echo "Old path: $old_path"
#modify filename
case "$mod_type" in
upper)
new_name=`echo "$old_name" | tr '[:lower:]' '[:upper:]'`
;;
lower)
new_name=`echo "$old_name" | tr '[:upper:]' '[:lower:]'`
;;
sed)
new_name=`echo "$old_name" | sed "$sed_pattern"`
if [ new_name = "" ]; then
echo "Wrong sed expression format."
exit 6
fi
;;
esac
#check if we will not overwrite smthing
if [ -e "$old_path$new_name" -a ! "$old_path$new_name" = "$old_path$old_name" ]; then
echo "File with the name $new_name already exists, cannot modify."
exit 4
fi
if [ "$new_name" = "$old_name" ]; then
continue
fi
if ! mv "$old_path$old_name" "$old_path$new_name"; then
pwd
echo "Mv failed to change filename."
exit 5
fi
changed=$((changed + 1))
echo "Filename changed from $old_name to $new_name;"
shift
done
}
ShowHelp ()
{
cat << EOF
Modify script modifies filnames according to given arguments.
Syntax: modify [-r] [-u/-l/sed_pattern] FILES..
Modify takes only one parameter, following values are allowed:
-u make all filenames uppercase
-l make all filenames lowercase
sed pattern - maodifies filnames accordingly to given pattern
Options:
-r modfications are made recursivly
Author: Roman Baluta <balutar@o2.pl>
EOF
}
ShowUsage()
{
echo -e "\nPlease use: modify --help to get information on using this script.\n"
}
#init variables
mod_chosen="false"
mod_type=""
sed_pattern=""
recursion="false"
changed=0
#echo "#of arguments: $#"
#if -h or --help printout help
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
ShowHelp
exit 0
fi
#this loop retrieves arguments
for arg in "$@"
do
case "$1" in
-u)
#if modify method already chosen then this is not an rgument but file name
if [ "$mod_chosen" = "true" ]; then
break
fi
mod_type="upper"
mod_chosen="true"
shift
;;
-l)
#if modify method already chosen then this is not an rgument but file nam
if [ "$mod_chosen" = "true" ]; then
break
fi
mod_type="lower"
mod_chosen="true"
shift
;;
-r)
#if modify method already chosen then this is not an rgument but file name
if [ "$mod_chosen" = "true" ] || [ "$recursion" = "true" ] ; then
break
fi
recursion="true"
shift
;;
#determine wheather it is not a try of passing sed pattaren
*)
#if method is already chosen then this can only be a filename
if [ "$mod_chosen" = "true" ]; then
break
fi
mod_type="sed"
mod_chosen="true"
sed_pattern="$1"
shift
;;
esac
done
#echo "#of files: $#"
#echo mod_type:$mod_type
#echo mod_chosen:$mod_chosen
#if no method was chosen exit and display usage info
if [ "$mod_chosen" = "false" ]; then
ShowUsage
exit 1
fi
if [ "$#" = "0" ]; then
ShowUsage
exit 2
fi
#rename files given as next arguments with the selected pattern
Modify "$@"
echo -e "\n$changed filename(s) changed.\n"
exit 0
Modyfikuje nazwy plików (-l to lowercase, -u to upper case, albo korzysta seda) no i -r powoduje, ze dziala rekursywnie. Po co pisac cos takiego? -> uczelnia:)
Jesli przypadkiem zobaczycie jakies błędy albo cos co mozna zrobic lepiej to dajcie znac, dzieki za pomoc!