Reputation: 123
I need to write a shell script.
I write a script:
for file in *.gif
do
filename=$(basename "$file")
filename=${filename%.*}
convert $file $filename.tlf
done
Is this right in your opinion? How can I add name changing?
PS: Please do not edit topic, because names of files are special information.
Upvotes: 2
Views: 92
Reputation: 18118
nr=0
for file in *.gif; do
nr=$(($nr+1))
filename="$(basename "$file")"
filename="${filename%.*}"
convert "$file" "${nr}_$filename.tlf"
done
Upvotes: 3
Reputation: 77137
If you're going to use the basename
command instead of parameter expansions, you should use it to its fullest power by giving it an extension suffix to drop. That will take care of both birds with one stone.
nr=0
for file in *.gif; do
filename="$(( ++nr ))_$(basename "$file" .gif).tlf"
convert "$file" "$filename"
done
You could also do the whole thing with parameter expansion:
nr=0
for file in *.gif; do
filename="$(( ++nr ))_${file##*/}"
filename="${filename%.*}"
convert "$file" "$filename"
done
The benefit of the parameter expansions is that you don't invoke a subshell every iteration. Unless you need to process a lot of files, it won't make a difference.
Upvotes: 1