shelllings

a practical way to learn shell
Log | Files | Refs | README | LICENSE

21_forloop.sh (1057B)


      1 #!/bin/sh
      2 
      3 # For loops follow a logic where it does an operation for every instance
      4 #
      5 # $ for var in list; do
      6 # $ 	operation
      7 # $ done
      8 #
      9 # From the semi-colon used, you can see that the snippet above can be
     10 # 4 lines long, but it is generally condensed using the semi-colon.
     11 #
     12 # Let's say you want to wipe the metadata from a set of photos in a 
     13 # folder you are in, it follows
     14 #
     15 # $ for pic in *.jpg; do
     16 # $ 	exiftool -all= "$pic"
     17 # $ done
     18 #
     19 # Fix the program below. If you mess up the test/ folder, you can git
     20 # clone the project and move it in. Hopefully the annoyance will cause
     21 # you to be more careful when running these commands.
     22 #
     23 # You want to add a line at the very end of every file, but you also
     24 # want to delete it. Don't worry about deleting the '# bloat message'
     25 # from the tests/ folder, as the program should delete all lines like
     26 # that
     27 
     28 for file in tests/*; do
     29 	echo "# bloat message" >> "$test_file"
     30 done
     31 
     32 for file in tests/*; do
     33 	tail -n -1 "$test_file" > "$test_file".tmp
     34 	mv -f "$test_file".tmp "$test_file" # this line is valid
     35 done