Difference between revisions of "Loops"

From Steak Wiki
Jump to navigationJump to search
 
(11 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
Computers will have you spinning in your chair. Soul Coughing says that everything goes in circles, but that's incorrect, everything turns in spirals. You never return to point A. "You can't go home again."
 +
 +
===Template Loop===
 
This is a basic (bash) loop to operate on files.
 
This is a basic (bash) loop to operate on files.
 
+
<pre>
Examples are given for 7z and video conversion.
+
for i in *.7z
 +
do name=`echo $i | cut -d'.' -f1`;
 +
echo $name;
 +
7z e -o"$name" ./"$i"
 +
done
 +
</pre>
 +
Here's the above with comments included.
 
<pre>
 
<pre>
 
#!/bin/bash -x
 
#!/bin/bash -x
Line 21: Line 30:
 
Basically, trim off extension of i, put into name. Use both for extractions/conversion.
 
Basically, trim off extension of i, put into name. Use both for extractions/conversion.
  
===Batch Edit Photos===
+
===One Liner Loop===
 +
while true; do echo "ok"; sleep 1; done
 +
 
 +
===Batch remove EXIF data and resize photos===
 +
 
 +
for i in *.{jpg,JPG,png,PNG}
 +
do
 +
  mogrify -strip $i
 +
  convert $i -resize 240x180\! edit_$i
 +
done
 +
verify the data is gone with
 +
identify -verbose filename | grep exif
 +
<small>#note the \! is required to force it to ignore aspect ratio... also note that magick == convert</small>
 +
====Keep Aspect ratio and rename====
 
<pre>
 
<pre>
#!/bin/bash
+
for i in *.jpg
for i in *.JPG
+
do  
do
+
mogrify -strip $i              #remove evil exif data
#delete exif data
+
name=`echo $i | cut -d'.' -f1`; #remove extension
mogrify -strip $i
+
echo $name;
#test convert size in gimp first to get ratio right
+
name=${name}edit_.jpg
convert -resize 1080x810 $i ~/Desktop/photofolder/$i
+
convert $i -resize 1440x $name #resizes only width. to resize only height, put the x first.
 
done
 
done
 
</pre>
 
</pre>
 
+
====Above but add date====
Add date:
 
 
<pre>
 
<pre>
 
#!/bin/bash
 
#!/bin/bash
Line 43: Line 64:
 
#test convert size in gimp first to get ratio right
 
#test convert size in gimp first to get ratio right
 
convert -resize 1080x810 $i ~/Desktop/photofolder/$DATE$i
 
convert -resize 1080x810 $i ~/Desktop/photofolder/$DATE$i
 +
done
 +
</pre>
 +
====rotate====
 +
<pre>
 +
for i in *.jpg
 +
do convert $i -rotate 90 $i; #use -90 for ccw
 
done
 
done
 
</pre>
 
</pre>
Line 77: Line 104:
 
   wget $line
 
   wget $line
 
done < "$input"</pre>
 
done < "$input"</pre>
Obviously here, wget is the command being used. Such an example being to download from a list of URLs delimited by newline.
+
Obviously here, wget is the command being used. This example downloads from a text file with a list of URLs.

Latest revision as of 21:50, 24 April 2024

Computers will have you spinning in your chair. Soul Coughing says that everything goes in circles, but that's incorrect, everything turns in spirals. You never return to point A. "You can't go home again."

Template Loop

This is a basic (bash) loop to operate on files.

for i in *.7z
 do name=`echo $i | cut -d'.' -f1`;
 echo $name;
 7z e -o"$name" ./"$i" 
done

Here's the above with comments included.

#!/bin/bash -x
#for i in *.webm;
        for i in *.7z
 do name=`echo $i | cut -d'.' -f1`;
 echo $name;

#for h264
# ffmpeg -i $i -s 1280x720 -c:a copy $name.mp4;

#for webm

#-map_metadata -1 is to remove metadata, at least try to remove some. (need to verify)
#ffmpeg -i $i -map_metadata -1 -c:v libvpx -crf 10 -b:v 1M -c:a libvorbis $name.webm
 7z e -o"$name" ./"$i" 
done

Basically, trim off extension of i, put into name. Use both for extractions/conversion.

One Liner Loop

while true; do echo "ok"; sleep 1; done

Batch remove EXIF data and resize photos

for i in *.{jpg,JPG,png,PNG}
do
  mogrify -strip $i
  convert $i -resize 240x180\! edit_$i 
done

verify the data is gone with

identify -verbose filename | grep exif

#note the \! is required to force it to ignore aspect ratio... also note that magick == convert

Keep Aspect ratio and rename

for i in *.jpg
 do 
 mogrify -strip $i               #remove evil exif data
 name=`echo $i | cut -d'.' -f1`; #remove extension
 echo $name;
 name=${name}edit_.jpg 
 convert  $i -resize 1440x $name #resizes only width. to resize only height, put the x first.
done

Above but add date

#!/bin/bash
DATE=$(date "+%Y_%m_%d")
for i in *.JPG
do
#delete exif data
mogrify -strip $i
#test convert size in gimp first to get ratio right
convert -resize 1080x810 $i ~/Desktop/photofolder/$DATE$i
done

rotate

for i in *.jpg
 do convert $i -rotate 90 $i; #use -90 for ccw
done

Interactive Range Loop

#!/bin/bash
for i in {1..254}; 
do 
some program that stalls goes here;
done

this will stop at the program, if it doesn't immediately complete so do this: ctrl - \

that will skip to the next range loop (i increments). note its not ctrl - c which will exit script. you can hold ctrl and depress \

What is CTRL - \? https://en.wikipedia.org/wiki/Signal_(IPC) One of the other options besides ctrl - c.


read file into command line by line

#!/bin/bash
input="text_file.txt"
while IFS= read -r line
do
  wget $line
done < "$input"

Obviously here, wget is the command being used. This example downloads from a text file with a list of URLs.