/media/bill/PROJECTS/System_maintenance/Linux/for loops notes.txt www.BillHowell.ca 23May2018 initial see also "/media/bill/PROJECTS/System_maintenance/Linux/file read loops.txt" +--+ from "$d_bin"'webPage scrape kyivindependent.sh' kyivindependent_news() { echo "" >"$p_news" if [ -f "$p_source" ]; then old_date="old_date not set yet" while read -u 9 line; do new_date=$( echo "$line" | grep '
' | sed 's|
\(.*\), \([a-zA-Z]\{3\}\)\(.*\) \([0-9]\+\)
|\4\22022|' ) if [ "$new_date" != "" ]; then echo "$new_date
" >>"$p_news" echo "
" >>"$p_news" echo "" >>"$p_news" old_date="$new_date" fi inn_article=$( echo "$line" | grep '
' | sed 's|
\(.*\), \([a-zA-Z]\{3\}\)\(.*\) \([0-9]\+\)
|\4\22022|' ) if [ "$new_date" != "" ]; then echo "$new_date
" >>"$p_news" echo "
" >>"$p_news" echo "" >>"$p_news" old_date="$new_date" fi out_article=$( echo "$line" | grep '
' ) # Note: grep escape "-/ don't escape stuff within chr_apos new_time=$( echo "$line" | grep '' | sed 's|.*\(.*\)|\1|' ) if [ "$new_time" != "" ]; then old_time="$new_time" fi link=$( echo "$line" | grep '
[ ]\+

|
\n\n|;s|
||;s|
||;s|

||;s| title=\".*\/">|\">|;s|

||;s|
||;s|

' | sed 's|\(.*\)

\(.*\)<\/p>|\2|' ) #echo "$content" if [ "$out_article" != "" ]; then inn_article="" echo "
" >>"$p_news" echo "" >>"$p_news" elif [ "$link" != "" ]; then echo "$old_time $old_date $link
" >>"$p_news" elif [ "$content" != "" ]; then echo "$content
" >>"$p_news" fi done fi done 9< "$p_source" else echo "kyivindependent.com page source.txt doesnt exist : $p_source" fi } ************************************************************ 08********08 https://www.shellhacks.com/bash-read-file-line-by-line-while-read-line-loop/ #] The general while read line construction that can be used in Bash scripts: while read LINE do COMMAND done < FILE #!/bin/bash #] FILE=$1 #] while read LINE; do #] echo "This is a line: $LINE" #] done < "$FILE" ************************* 23May2018 for loops https://www.cyberciti.biz/faq/bash-for-loop/ Bash For Loop Examples Posted on October 31, 2008in Categories BASH Shell, CentOS, Debian / Ubuntu, FreeBSD, Linux, Solaris-Unix, Suse, Ubuntu Linux, UNIX last updated December 25, 2017 Examples #] This type of for loop is characterized by counting. The range is specified by a beginning (#1) and ending number (#5). The for loop executes a sequence of commands for each member in a list of items. A representative example in BASH is as follows to display welcome message 5 times with for loop: #] #] #!/bin/bash #] for i in 1 2 3 4 5 #] do #] echo "Welcome $i times" #] done #] Sometimes you may need to set a step value (allowing one to count by two’s or to count backwards for instance). Latest bash version 3.0+ has inbuilt support for setting up ranges: #] #!/bin/bash for i in {1..5} do echo "Welcome $i times" done #] #] Bash v4.0+ has inbuilt support for setting up a step value using {START..END..INCREMENT} syntax: #] #!/bin/bash #] echo "Bash version ${BASH_VERSION}..." #] for i in {0..10..2} #] do #] echo "Welcome $i times" #] done #] #] Three-expression bash for loops syntax - This type of for loop share a common heritage with the C programming language. It is characterized by a three-parameter loop control expression; consisting of an initializer (EXP1), a loop-test or condition (EXP2), and a counting expression (EXP3). for (( EXP1; EXP2; EXP3 )) do command1 command2 command3 done A representative three-expression example in bash as follows: #!/bin/bash #] for (( c=1; c<=5; c++ )) #] do #] echo "Welcome $c times" #] done #] Conditional exit with break You can do early exit with break statement inside the for loop. You can exit from within a FOR, WHILE or UNTIL loop using break. General break statement inside the for loop: #] for I in 1 2 3 4 5 #] do #] statements1 #Executed for all values of ''I'', up to a disaster-condition if any. #] statements2 #] if (disaster-condition) #] then #] break #Abandon the loop. #] fi #] statements3 #While good and, no disaster-condition. #] done #] #] Following shell script will go though all files stored in /etc directory. The for loop will be abandon when /etc/resolv.conf file found. #!/bin/bash #] for file in /etc/* #] do #] if [ "${file}" == "/etc/resolv.conf" ] #] then #] countNameservers=$(grep -c nameserver /etc/resolv.conf) #] echo "Total ${countNameservers} nameservers defined in ${file}" #] break #] fi #] done Early continuation with continue statement To resume the next iteration of the enclosing FOR, WHILE or UNTIL loop use continue statement. for I in 1 2 3 4 5 do statements1 #Executed for all values of ''I'', up to a disaster-condition if any. statements2 if (condition) then continue #Go to next iteration of I in the loop and skip statements3 fi statements3 done #] This script make backup of all file names specified on command line. If .bak file exists, it will skip the cp command. #!/bin/bash #] FILES="$@" #] for f in $FILES #] do #] # if .bak backup file exists, read next file #] if [ -f ${f}.bak ] #] then #] echo "Skiping $f file..." #] continue # read next file and skip the cp command #] fi #] # we are here means no backup file exists, just use cp command to copy file #] /bin/cp $f $f.bak #] done # enddoc