Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notes on Bash scripting session #8

Open
ccbaumler opened this issue Jun 28, 2023 · 1 comment
Open

Notes on Bash scripting session #8

ccbaumler opened this issue Jun 28, 2023 · 1 comment

Comments

@ccbaumler
Copy link

bash scripting by Oliver

bash flows

JPG to PNG conversion. How can this be automated?

Simple for loops on the command line.

$ 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)

end of for loop:
;
done

eom -> https://man.archlinux.org/man/eom.1.en

ImageMagick -> https://anaconda.org/conda-forge/imagemagick

bash arrays

#To view large directory of many files

$ ls Images/ | less

#cause crashes in command line

$ ls *.xml 

#but this does not

$ echo *.xml

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

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.

@ccbaumler
Copy link
Author

Additional links for the bash from Nick:

And if you know regular expressions but struggle with sed/awk, this tool is a nice replacement: https://github.com/chmln/sd

This is a helpful reference if, like me, you easily forget arcane languages you only use occasionally: https://learnxinyminutes.com/

https://ucdavisdatalab.github.io/workshop_introduction_to_the_command_line/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant