0

First of all I have little to no idea what I'm doing, but basically have a script file that works just fine, but I would like to have user input variables. I'm using cygwin and imagemagick. I cd my way to the folder of images I want to logo and use this script:

#!/bin/bash
for f in *.jpg; do convert "$f" -verbose -auto-orient -sharpen 5 -gravity 
southeast -draw 'image over 0,0 0,0 "l:/temp/logo.png"' -define 
jpeg:extent=1500kb l:/temp/og-rotate-logo/"$f"; done

I would love it if instead of writing a new script all the time to change some of the numbers, I could have the script run and the user be prompted to input the amounts and the script would use these inputs. For example I would like to adjust the sharpen level to 4 instead of 5 I have to write and save a new script with 4, but if the user could input the sharpen level they want that would be great! Any help is greatly appreciated. I'm obviously a newbie.

Just want to say thanks to Aaron for the help in pointing me in the right direction. I got this code working great. Thanks again for taking the time for a newbie. We all started somewhere.

#!/bin/bash
read -p "Enter Sharpen Value (5) " sharpenvalue; read -p "Enter Saturation 
Value (105) " saturationvalue; read -p "Enter File Size Value (1500) " 
filesizevalue; for f in *.jpg; do convert "$f" -verbose -auto-orient -sharpen 
"$sharpenvalue" -modulate 100,"$saturationvalue" -gravity southeast -draw 
'image over 0,0 0,0 "l:/temp/logo.png"' -define 
jpeg:extent="$filesizevalue"kb l:/temp/og-rotate-logo/"$f"; done
Brett
  • 47
  • 2
  • 9
  • 1
    Cross-site duplicate: https://unix.stackexchange.com/questions/32290/pass-command-line-arguments-to-bash-script – Benjamin W. Jul 23 '18 at 15:17
  • Possible duplicate of [How do I parse command line arguments in Bash?](https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash) – Benjamin W. Jul 23 '18 at 15:19
  • Please use your title to actually summarize your problem, rather than ranting. – chepner Jul 23 '18 at 15:49
  • sorry but it seems like if you are not asking about some amazing nuance in code capabilities then you get rejected on this site. i am obviously a newbie and have no idea what i'm doing. i've got this far with what i have been able to get off the web and i'm just trying to make my life a little easier with this. a simple answer and thank you is all that it should ever be. but good luck getting that here. no everyone is pissed because of the newbies asking dumb questions. ya it's probably dumb for a pro, but not to me. i've looked hard and can't find any help. thought maybe to look here – Brett Jul 23 '18 at 15:54
  • "have no idea what i'm doing" that can be a problem, in particular if it leads you to be unable to properly describe your problem. A noob problem that is properly described will likely get an answer from my experience. – Aaron Jul 23 '18 at 15:57
  • i'm such a newbie that i can barely explain my question. i have no idea what shell or bash or script or any of this stuff is. what i can tell you is that i have this working in windows using cygwin and imagemagick but adding input variables would be sweet. i have no idea. -end rant – Brett Jul 23 '18 at 15:58
  • Your question as it is currently is OK and answerable IMO (altough it might already be answered by the suggested duplicate). Maybe one detail : you say you would like to parametrize the "sharpen" value, but it's unclear if it's just an example of value you want to parametrize, and expect to be able to parametrize each of them, or if it's the only value you want to parametrize. – Aaron Jul 23 '18 at 16:00
  • ok i'll try to answer Aaron the best i can. I would like to be prompted to enter a value for sharpen and have that value used. eventually i would like to do this for all the possible places it could be used. such as to input the file size (-define jpeg:extent=1500kb) If there could be a user input variable for the 1500 that would be great as well. basically the program is ran and the user would answers a few questions about sharpen level, saturation, file size, etc and then it takes the input from the user and uses it instead of the fixed values i have to manually change all the time. – Brett Jul 23 '18 at 16:11
  • What you can use then is `read -p "message" variable` which will display "message" (e.g. `Sharpen value : ` and let the user provide a value for "variable" (e.g. `sharpen`). You can then check the value of the variable and set a default value if unset (e.g. `if [ -z "$variable" ]; then variable=default; fi`). Another maybe more idiomatic way would be to follow the suggestion of the "How do I parse command line arguments in Bash" linked duplicate, in which case the parameters would be passed in the commandline rather than interactively – Aaron Jul 23 '18 at 16:21
  • Oh and I forgot : in your current command line, replace the fixed value by `"$variable"` to reference the value the user entered. Of course in this comment and the previous one, replace "variable" and "message" by whatever is appropriate for the parameter you're working with – Aaron Jul 23 '18 at 16:25
  • this is really beginning to help. thank you. let me try some stuff – Brett Jul 23 '18 at 16:27
  • NO! Don't prompt for input. Take inputs either as positional parameters or named parameters. That is, the user types `cmd 5` or `cmd sharpenvalue=5` or `cmd --sharpen 5`. The first is the absolute simplest, and all you have to do is put `${1-5}` in your code instead of `5`. (`${1-5}` expands to the value of the first argument, defaulting to `5` if no argument is given.) – William Pursell Jul 23 '18 at 17:18

0 Answers0