I convert images in a directory named foo to bar by like this.
$ mkdir bar
$ mogrify -path bar -negate foo/*.png
Is there option in Imagemagick that create a folder with -path option, if it not exist.
I convert images in a directory named foo to bar by like this.
$ mkdir bar
$ mogrify -path bar -negate foo/*.png
Is there option in Imagemagick that create a folder with -path option, if it not exist.
To take arguments from the mogrify command, and do other actions based on their content, you can "override" the program with a function of your own, then pass the original arguments on to the function:
mymog(){
[[ $1 == "-path" ]] && [[ ! -d $2 ]] && mkdir "$2"
mogrify "$@"
}
To use it, just replace mymog wherever you would use mogrify:
mymog -path bar -negate foo/*.png
The function tests to see if the first argument is -path. If so, it goes on to test if the second argument is not an existing directory. If it is not, then it creates that directory. (The [[ ]] && is just another way to write if-then statements.) In either case it goes on to pass all the arguments to the mogrify command.
The only warning is that you have to put the -path argument first -- you can't stick it elsewhere in the line. You should be able to use this wherever you would normally use mogrify and it will just ignore the mkdir part if there is no -path definition.
To have this available in your daily usage, add those 4 lines to your .bash_profile or .bashrc file, depending on your operating system.
EDIT #2: New answer with no dependence on position of -path
function mogmod(){
args=("$@")
for ((i=0; i < $#; i++)) {
if [[ ${args[$i]} = "-path" ]]
then
mypath=${args[((i+1))]}
[[ ! -d "$mypath" ]] && mkdir "$mypath"
fi
}
mogrify "$@"
}
mogrify like that. But I don't know how to wrap mogrify only when -path argument exists. It would be very helpful if you show me how.
– ironsand
Sep 16 '13 at 23:23
testmake ; in front of mogrify -negate foo/*.png without the -path element, it will just make the directory if it doesn't exist, but the mogrify command will run independent of testmake. Basically, you can add that whenever you are using -path or omit it if not, but it shouldn't hurt anything to use it all the time. (Maybe I am not understanding your typical use.)
– beroe
Sep 17 '13 at 01:09
-path is given.
– ironsand
Sep 17 '13 at 04:32
shell or bash with the title "How to re-use arguments from a shell command to perform other functions" ;^)
– beroe
Sep 17 '13 at 14:38
-path must be the first argument. I know I'll forget the rule if I use the function once in a month.
– ironsand
Sep 18 '13 at 10:04
mkdir -p /path/to/barmakes parents only as needed. – justbrowsing Sep 15 '13 at 22:13mogrifynot option ofmkdir. – ironsand Sep 15 '13 at 22:20