'basename and convert in bash script error
I am a beginner in bash script programming. I'm trying to make a function to convert a large number of photo files without changing the name. I have read several topics about using basename or {s%.*} to remove the type, but without succeeding in applying it.
thanks in advance for your help
#!/bin/bash
FOLDER=~/Bureau/data/jpg
ERROR="$FOLDER/error.txt"
CURRENT=$PWD
if ! [[ -d "$FOLDER" ]]|| ! [[ -e $ERROR ]]
then
mkdir -p $FOLDER
touch $ERROR
else
rm -f "$FOLDER/*"
touch $ERROR
fi
for img in "$CURRENT/*.tif"; do
filename=$(basename -- "$img")
filename="${filename%.*}"
convert $img "$FOLDER/$filename.jpg" 2> $ERROR
done
Solution 1:[1]
I think you can simplify.
#!/bin/bash
dir="~/Bureau/data/jpg"
rm -fr "$dir/" # -f doesn't fail if it didn't exist
mkdir -p "$dir" # -p doesn't fail if it *did* exist
for img in "$PWD"/*.tif; do
stub="${img%.tif}" # simple strip the end
convert "$img" "$dir/$stub.jpg" 2> "$stub.err # err log per file
done # 2> "$dir/err.log" # if you don't want an error per, use this instead
You have a lot of cases of globs in quotes, which doesn't work.
Use "$PWD"/*.tif instead of "$PWD/*.tif".
Also, 2> inside the loop overwrites. Either use 2>>, or 2> outside the loop.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Paul Hodges |


