Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Shell script
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Generalization=== Simple batch jobs are not unusual for isolated tasks, but using shell loops, tests, and variables provides much more flexibility to users. A POSIX sh script to convert JPEG images to PNG images, where the image names are provided on the command-line—possibly via wildcards—instead of each being listed within the script, can be created with this file, typically saved in a file like <code>/home/''username''/bin/jpg2png</code> <syntaxhighlight lang="sh"> #!/bin/sh for jpg; do # use $jpg in place of each filename given, in turn png=${jpg%.jpg}.png # construct the PNG version of the filename by replacing .jpg with .png printf 'converting "%s" ...\n' "$jpg" # output status info to the user running the script if convert "$jpg" jpg.to.png; then # use convert (provided by ImageMagick) to create the PNG in a temp file mv jpg.to.png "$png" # if it worked, rename the temporary PNG image to the correct name else # ...otherwise complain and exit from the script printf >&2 'jpg2png: error: failed output saved in "jpg.to.png".\n' exit 1 fi # the end of the "if" test construct done # the end of the "for" loop printf 'all conversions successful\n' # tell the user the good news </syntaxhighlight> The <code>jpg2png</code> command can then be run on an entire directory full of JPEG images with just <code>/home/''username''/bin/jpg2png *.jpg</code>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)