Browse code

Now it valichecks the rsync config files, to try to make sure all they contain is an rsync command line and comments

Apollia authored on06/28/2017 15:51:30
Showing1 changed files
... ...
@@ -609,6 +609,169 @@ fi
609 609
 
610 610
 
611 611
 
612
+# 04:12:29 06/21/2017.  Function from:
613
+#
614
+# https://stackoverflow.com/questions/11856054/bash-easy-way-to-pass-a-raw-string-to-grep
615
+
616
+ere_quote() {
617
+    sed 's/[]\.|$(){}?+*^]/\\&/g' <<< "$*"
618
+}
619
+
620
+
621
+Get_File_Line_Number()
622
+{
623
+	local filepath="$1"
624
+	local unescaped_string="$2"
625
+	
626
+	escaped_search_string_for_grep=$(ere_quote "$unescaped_string")
627
+	
628
+	result=$(grep --line-number "$escaped_search_string_for_grep" "$config_filepath" | head -n 1 | gawk '{print $1}' FS=":")
629
+	
630
+	
631
+}
632
+Abort_Because_of_Invalid_Config_File()
633
+{
634
+	local config_filepath="$1"
635
+	local reason="$2"
636
+	
637
+	echo "$This_Script_Name: Aborting, because this config file appears to be invalid:
638
+
639
+	$config_filepath
640
+
641
+$reason"
642
+	
643
+	exit 21
644
+}
645
+
646
+
647
+
648
+Do_Valicheck____Is_This_A_Valid_Rsync_Config_File()
649
+{
650
+	local config_filepath="$1"
651
+
652
+	read -r -d '' search_string_for_grep <<'EOF'
653
+[ -z "$from_path_for_rsync" ] && exit 1
654
+EOF
655
+	
656
+	Get_File_Line_Number "$config_filepath" "$search_string_for_grep"
657
+	line_number_of_abort_1="$result"
658
+	
659
+	if [[ "$line_number_of_abort_1" -gt 0 ]]
660
+	then
661
+		#echo "Abort 1 is present!"
662
+		:
663
+	else
664
+		Abort_Because_of_Invalid_Config_File "$config_filepath" "It was missing the first required abort."
665
+	fi
666
+	
667
+	
668
+	read -r -d '' search_string_for_grep <<'EOF'
669
+[ -z "$to_path_for_rsync" ] && exit 2
670
+EOF
671
+	
672
+	Get_File_Line_Number "$config_filepath" "$search_string_for_grep"
673
+	line_number_of_abort_2="$result"
674
+	
675
+	if [[ "$line_number_of_abort_2" -gt 0 ]]
676
+	then
677
+		#echo "Abort 2 is present!"
678
+		:
679
+	else
680
+		Abort_Because_of_Invalid_Config_File "$config_filepath" "It was missing the second required abort."
681
+	fi
682
+
683
+	if [[ "$line_number_of_abort_1" -lt "$line_number_of_abort_2" ]]
684
+	then
685
+		earliest_abort_line_number="$line_number_of_abort_1"
686
+		latest_abort_line_number="$line_number_of_abort_2"
687
+	else
688
+		earliest_abort_line_number="$line_number_of_abort_2"
689
+		latest_abort_line_number="$line_number_of_abort_1"
690
+	fi
691
+	
692
+	line_number_before_earliest_abort_line=$(echo "$earliest_abort_line_number - 1" | bc)
693
+
694
+	uncommented_lines_before_earliest_abort=$(cat "$config_filepath" | head -n "$line_number_before_earliest_abort_line" | awk '!/^#|^ / { print; }' )
695
+	
696
+	if [ ! -z "$uncommented_lines_before_earliest_abort" ]
697
+	then
698
+		Abort_Because_of_Invalid_Config_File "$config_filepath" "It had these uncommented lines before the earliest abort:
699
+
700
+$uncommented_lines_before_earliest_abort"
701
+		:
702
+	fi
703
+	
704
+	
705
+	line_number_after_latest_abort_line=$(echo "$latest_abort_line_number + 1" | bc)
706
+	
707
+	
708
+	line_number_of_rsync=$(grep --line-number "^rsync" "$config_filepath" | gawk '{print $1}' FS=":")
709
+	
710
+	line_number_before_rsync=$(echo "$line_number_of_rsync - 1" | bc)
711
+
712
+	
713
+	uncommented_lines_before_rsync_and_after_later_abort=$(cat "$config_filepath" | head -n "$line_number_before_rsync" | tail -n +"$line_number_after_latest_abort_line" )
714
+	
715
+
716
+	if [ ! -z "$uncommented_lines_before_rsync_and_after_later_abort" ]
717
+	then
718
+		Abort_Because_of_Invalid_Config_File "$config_filepath" "It had these uncommented lines between the abort section and the rsync:
719
+
720
+	$uncommented_lines_before_rsync_and_after_later_abort"
721
+		:
722
+	fi
723
+	
724
+	
725
+	line_beginning_with_rsync=$(grep -e "^rsync" "$config_filepath")
726
+	
727
+	last_line=$(tail -n 1 "$config_filepath")
728
+	
729
+	
730
+	read -r -d '' what_the_last_line_should_be <<'EOF'
731
+"$to_path_for_rsync" \
732
+EOF
733
+
734
+	if [ "$last_line" != "$what_the_last_line_should_be" ]
735
+	then
736
+		Abort_Because_of_Invalid_Config_File "$config_filepath" "The last line wasn't the following (with a newline after it):
737
+		
738
+$what_the_last_line_should_be"
739
+	fi
740
+	
741
+	
742
+	uncommented_lines_with_semicolons=$(cat "$config_filepath" | awk '!/^#|^ / { print; }' | grep ";")
743
+	
744
+
745
+	if [ ! -z "$uncommented_lines_with_semicolons" ]
746
+	then
747
+		Abort_Because_of_Invalid_Config_File "$config_filepath" "There were uncommented lines containing semicolons, which are forbidden."
748
+	fi
749
+	
750
+	
751
+	rsync_lines_not_ending_in_space_backslash=$(cat "$config_filepath" |  tail -n +"$line_number_of_rsync" | grep -vE '[ ][\\]$' )
752
+	
753
+	# 11:26:32 06/28/2017.  I have no idea why it's necessary to use -v instead
754
+	# of just this:
755
+	#
756
+	# grep -E '[^ ][^\\]$'
757
+	#
758
+	# No idea why that fails.
759
+	
760
+	
761
+	if [ ! -z "$rsync_lines_not_ending_in_space_backslash" ]
762
+	then
763
+		Abort_Because_of_Invalid_Config_File "$config_filepath" "There were lines in its rsync command which didn't end in ' \' (space backslash, without quotes):
764
+
765
+	$rsync_lines_not_ending_in_space_backslash"
766
+	fi
767
+	
768
+	return 0
769
+}
770
+
771
+Do_Valicheck____Is_This_A_Valid_Rsync_Config_File "$Fillepath____Ramize_Rsync"
772
+Do_Valicheck____Is_This_A_Valid_Rsync_Config_File "$Fillepath____Physave_Rsync"
773
+
774
+
612 775
 
613 776
 ############
614 777
 # Function