I have a small bash script that does simple file modifications and I want to rewrite the code to be more readable. My goal is to pass Commands as strings into a function that loops the command over a Directory.
I've tried to use different methods to escape the "$" or different """ combinations but none really work.
#!/bin/bash
process="/Users/Gernot/Tools/.Process"
output="/Users/Gernot/Tools/2 Output"
input="/Users/Gernot/Tools/1 Input/"
function run {
for file in "$input$1"/*
do
echo "running procedure $1" #echoes which procedure is running
$2 #does the command for every file in the directory
done
}
run "PDF Komprimieren" "magick convert \$file -density 110 -compress jpeg -quality 100 \$file"
This is the error I get:
running procedure PDF Komprimieren
convert: unable to open image '$file': No such file or directory @ error/blob.c/OpenBlob/3497.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/556.
convert: no images defined `$file' @ error/convert.c/ConvertImageCommand/3273.
eval, eg.$(eval echo "$input/$1). It looks like you want to loop through your args as wellpdf_komprimieren()andmagick_convert()which will be, IMHO, more readable. Even when you get your command evaluated, the spaces in the dirname and perhaps filename will cause more problems.evalbefore$2.