You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
$ ls Images/
images2.jpg images3.jpg images.jpg
$ rpm -qa ImageMagick
#checks the application
#to convert a single file
$ convert Images/images.jpg Images/images.png
$ ls Images/
images2.jpg images3.jpg images.jpg images.png
$ eom Images/*
$ for i in Images/*.jpg; do echo $i; done
images2.jpg
images3.jpg
images.jpg
$ for i in Images/*.jpg; do echo $(dirname $i)/$(basename $i .jpg).png; done
images2.png
images3.png
images.png
$ for i in Images/*.jpg; do convert $i $(dirname $i)/$(basename $i .jpg).png; done
$ ls Images/
images2.jpg images3.jpg images.jpg images.png images2.png images3.png
preamble of the for loop: for is the built in command of the shell. Bash has variables just like other languages. i is a user defined variable. Images/*.jpg
; allows for the next command
body of the for loop: do do the next command $ return the current value of the variable () will run the for loop across the sub-command i the variable defined in the preamble dirname returns the directory name basename returns the base name/file name (when including .jpg at the end of the command the .jpg is removed by the basename command)
to build an empty array
$ args=()
#to add to the array
$ args+=(DEBUG=1)
$ args+=(Foo)
#but echo only returns the first variable
$echo $args
DEBUG=1
#to treat it like an array in bash include `()` where `[#]` is the location on the arrya
$ echo $(args[0])
DEBUG=1
$ echo $(args[1])
Foo
#to return the entire array from a specific point
$ echo ${args[0]}
DEBUG=1 Foo
$ echo ${args[1]}
Foo
$ echo ${args[0]}
DEBUG=1 Foo
$ args[0]=DEBUG=0
$ echo ${args[0]}
DEBUG=0 Foo
#create a filename that includes a string
$ mv imaged.jpg "pretty image.jpg"
$ mv imaged.jpg pretty image.jpg
mv: target 'image.jpg': No such file or directory
#to bypass the error of including a whitespace in the filename
$ files=(file1)
$ files+=(file 2)
$ echo ${files[0]}
file1 file 2
$ for i in ${files[@]}; do echo $i ; done
file1
file
2
$ for i in "${files[@]}"; do echo $i ; done
file1
file 2
$ files=(Images/*.jpg)
$ for i in "${files[@]}"; do echo $i ; done
images2.jpg
images3.jpg
images.jpg
Pretty Image.jpg
Because bash uses strings, it will print each string but split them by whitespaces.
The text was updated successfully, but these errors were encountered:
bash scripting by Oliver
bash flows
JPG to PNG conversion. How can this be automated?
Simple for loops on the command line.
preamble of the for loop:
for
is the built in command of the shell. Bash has variables just like other languages.i
is a user defined variable.Images/*.jpg
;
allows for the next commandbody of the for loop:
do
do the next command$
return the current value of the variable()
will run the for loop across the sub-commandi
the variable defined in the preambledirname
returns the directory namebasename
returns the base name/file name (when including.jpg
at the end of the command the.jpg
is removed by thebasename
command)end of for loop:
;
done
eom -> https://man.archlinux.org/man/eom.1.en
ImageMagick -> https://anaconda.org/conda-forge/imagemagick
bash arrays
globbing
with wildcards (*
)Everything in bash is a string (a flat string of characters). For good and bad.
Bash arrays allows for a bit more complexity in bash.
make
is a command to make large software products.https://www.gnu.org/software/make/manual/make.html
Because bash uses strings, it will print each string but split them by whitespaces.
The text was updated successfully, but these errors were encountered: