for i in *.jpg
do
j = '$i | cut -d . -f 1'
convert $i $j".png"
done
temp.sh: line 3: j: command not found
I know about mogrify, just wanted to know how to get variables done in bash.
You can also use bash's builtin parameter expansion to remove the first dot and everything after it: j="${i%%.*}"
If you want to remove the last dot and anything following it (i.e. the file's extension), use: j="${i%.*}"
Example:
$ i="file.with.dots.ext"
$ echo "${i%%.*}"
file
$ echo "${i%.*}"
file.with.dots
for f in ./*.jpg; do convert "$f" "${f%.*}.png"; done
.
You have to remove the spaces around =
:
for i in *.jpg
do
j=`echo $i | cut -d . -f 1`
convert $i $j".png"
done
j=`echo $i | cut -d . -f 1`
.
Apr 15, 2011 at 7:52
In addition, your script won't work as expected if any filename contains any special character (such as a space). You should get in the habit of always enclosing variable references in double quotes. Try this:
for i in *.jpg
do
j="$(echo "$i" | cut -d . -f 1)"
convert "$i" "${j}.png"
done
$i
happens to hold a command name.
Apr 15, 2011 at 10:32
Integrating bash parameter expansion and double quotes for files with special characters in their names, and the code simplifies too:
for f in *.jpg; do
convert "$f" "${f%.*}.png"
done