From 98c12e215329e91ed17cc09afcf703cdb2326ffe Mon Sep 17 00:00:00 2001 From: urbanware-org Date: Sat, 14 Oct 2017 23:14:52 +0200 Subject: [PATCH] Revision and fix for issue #13 --- CHANGELOG | 8 +++++-- core/analyze.sh | 29 +++++++++++++++++++---- core/global.sh | 3 ++- core/monitor.sh | 29 +++++++++++++++++++---- core/output.sh | 23 ++++++++++-------- docs/usage_salomon.txt | 2 +- salomon.sh | 53 ++++++++++++++++++++++++++++-------------- 7 files changed, 106 insertions(+), 41 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 9a42f54..d2c7a0a 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,13 +1,17 @@ CHANGELOG (SaLoMon) - Next version (outstanding) + Version 1.8.3 (2017-10-14) + * Revised the code to process the input files inside the main SaLoMon + as well as the analyzing and monitoring core script. * Revised the output header inside the output core script (negligible - changes). + changes related to multiple input files). * Revised the separator when processing multiple input files (replaced the previous one with a line of hyphens). + # Fixed the bug that input file paths cannot contain any spaces. + Version 1.8.2 (2017-09-13) * Revised the code in various SaLoMon components to allow multiple diff --git a/core/analyze.sh b/core/analyze.sh index ca04f4d..97c6b11 100755 --- a/core/analyze.sh +++ b/core/analyze.sh @@ -13,16 +13,36 @@ analyze_input_file() { check_patterns - for i in $input_file; do - tail "$i" &>/dev/null + spaces=0 + for file in $input_file; do + temp=$(sed -e "s/^ *//g;s/ *$//g" <<< "$file") + + grep "#" <<< "$temp" &>/dev/null + if [ $? -eq 0 ]; then + filepath="$(sed -e "s/#/\ /g" <<< "$temp")" + spaces=1 + else + filepath="$temp" + spaces=0 + fi + + tail "$filepath" &>/dev/null if [ $? -ne 0 ]; then - usage "No read permission on the given input file '$i'" + usage "No read permission on the given input file '$filepath'" fi + + if [ $spaces -eq 1 ]; then + temp=$(sed -e "s/\ /\*/g" <<< $filepath) + input_file_list="$input_file_list $temp" + else + input_file_list="$input_file_list $filepath" + fi + done timestamp=$(date "+%Y%m%d%H%M%S%N") temp_file="/tmp/salomon_${timestamp}.tmp" - paste -d "\n" $input_file | grep -v "^$" > $temp_file + paste -d "\n" $input_file_list | grep -v "^$" > $temp_file input_file=$temp_file count=0 @@ -35,7 +55,6 @@ analyze_input_file() { fi print_output_line "$line" - if [ $slow -eq 1 ]; then sleep 0.$delay fi diff --git a/core/global.sh b/core/global.sh index 932d179..ca52cec 100755 --- a/core/global.sh +++ b/core/global.sh @@ -11,7 +11,7 @@ # ============================================================================ set_global_variables() { - version="1.8.2" + version="1.8.3" arg_case="" input_file="" @@ -31,6 +31,7 @@ set_global_variables() { highlight_all=0 highlight_cut_off=0 highlight_upper=0 + interactive=0 prompt=0 slow=0 start_line=1 diff --git a/core/monitor.sh b/core/monitor.sh index ee1ea97..c9ed98a 100755 --- a/core/monitor.sh +++ b/core/monitor.sh @@ -13,16 +13,35 @@ monitor_input_file() { check_patterns - for i in $input_file; do - tail "$i" &>/dev/null + spaces=0 + for file in $input_file; do + temp=$(sed -e "s/^ *//g;s/ *$//g" <<< "$file") + + grep "#" <<< "$temp" &>/dev/null + if [ $? -eq 0 ]; then + filepath="$(sed -e "s/#/\ /g" <<< "$temp")" + spaces=1 + else + filepath="$temp" + spaces=0 + fi + + tail "$filepath" &>/dev/null if [ $? -ne 0 ]; then - usage "No read permission on the given input file '$i'" + usage "No read permission on the given input file '$filepath'" fi + + if [ $spaces -eq 1 ]; then + temp=$(sed -e "s/\ /\*/g" <<< $filepath) + input_file_list="$input_file_list $temp" + else + input_file_list="$input_file_list $filepath" + fi + done - tail -n $start_line -F $input_file 2>/dev/null | while read line; do + tail -n $start_line -F $input_file_list 2>/dev/null| while read line; do print_output_line "$line" - if [ $slow -eq 1 ]; then sleep 0.$delay fi diff --git a/core/output.sh b/core/output.sh index 1931685..db080a4 100755 --- a/core/output.sh +++ b/core/output.sh @@ -63,16 +63,19 @@ print_output_header() { filepath=$(readlink -f $input_file) print_line "${color_white}Input file:" "${color_yellow}$filepath" else - input_file_list="" + desc="Input files:" for file in $input_file; do - filepath=$(readlink -f $file) - temp="$files $filepath" - files="$temp" + temp=$(sed -e "s/^ *//g;s/ *$//g;s/#/\ /g" <<< "$file") + filepath=$(readlink -f "$temp") + if [ -z "$filepath" ]; then + continue + fi + print_line "${color_white}$desc" "${color_yellow}$filepath" + if [ ! -z "$desc" ]; then + desc="" + fi done - input_file_list=$(sed -e "s/^\ //g" <<< $files) - - print_line \ - "${color_white}Input files:" "${color_yellow}$input_file_list" + print_line fi if [ "$color_file" = "" ]; then @@ -202,8 +205,8 @@ print_output_line() { if [ $seperator_line -eq 1 ]; then grep "^==>.*<==$" <<< $1 &>/dev/null if [ $? -eq 0 ]; then - temp=$((sed -e "s/==>//g" | sed -e "s/<==//g") <<< $1) - fp=$(readlink -f $temp) + temp=$(sed -e "s/==>//g;s/<==//g;s/^ *//g;s/ *$//g" <<< $1) + fp=$(readlink -f "$temp") ln=$(printf -- "-%.0s" $(seq 0 80)) seperator="\e[1;30m--\e[0;37m[\e[1;33m$fp\e[0;37m]\e[1;30m$ln" echo -e "$seperator\e[0m" | cut -c 1-113 diff --git a/docs/usage_salomon.txt b/docs/usage_salomon.txt index 3b0707b..6d01417 100644 --- a/docs/usage_salomon.txt +++ b/docs/usage_salomon.txt @@ -107,7 +107,7 @@ USAGE (salomon.sh) inside the config sub-directories ('colors' and 'filters') of the SaLoMon script directory. - 3. Processing an input file + 3. Processing input file The following examples use a color config file. See section 2.3 for details. diff --git a/salomon.sh b/salomon.sh index b5299b4..41d71fe 100755 --- a/salomon.sh +++ b/salomon.sh @@ -47,7 +47,8 @@ else ;; -i|--input-file) shift - input_file="$input_file $1" + temp=$(sed -e "s/^ *//g;s/ *$//g;s/\ /#/g" <<< $1) + input_file="$input_file $temp" check_argument "-i/--input-file" "$input_file" "file" shift ;; @@ -153,6 +154,24 @@ else esac done + # Input file + temp=$(sed -e "s/^ *//g;s/ *$//g" <<< "$input_file") + input_file="$temp" + + if [ -z "$input_file" ]; then + usage "No input file given" + fi + + for file in $input_file; do + filepath=$(sed -e "s/^ *//g;s/ *$//g;s/#/\ /g" <<< $file) + if [ ! -e "$filepath" ]; then + usage "The given input file '$filepath' does not exist" + elif [ ! -f "$filepath" ]; then + usage "The given input file path '$filepath' is not a file" + fi + done + + # Action to perform if [ ! -z "$action" ]; then if [ "$action" = "analyze" ]; then follow=0 @@ -165,24 +184,12 @@ else fi fi + # Prompt before exit if [ $follow -eq 0 ] && [ $prompt -eq 1 ]; then usage "The analyzing mode does not support a prompt before exit" fi - if [ -z "$input_file" ]; then - usage "No input file given" - fi - - temp=$(echo "${input_file}" | sed -e 's/^ *//g;s/ *$//g') - input_file="$temp" - for i in $input_file; do - if [ ! -e "$i" ]; then - usage "The given input file '$i' does not exist" - elif [ ! -f "$i" ]; then - usage "The given input file path '$i' is not a file" - fi - done - + # Color file if [ ! -z "$color_file" ]; then if [ ! -e "$color_file" ]; then color_file="${color_dir}${color_file}" @@ -197,6 +204,7 @@ else fi fi + # Highlighting highlight_params=$(( highlight + highlight_all + highlight_upper )) if [ $highlight_params -gt 1 ]; then usage \ @@ -208,7 +216,7 @@ else "The '--cut-off' argument can only be used with '--highlight-all'" fi - + # Filter pattern if [ -z "$filter_pattern" ]; then if [ "$arg_case" = "-i" ]; then usage \ @@ -242,6 +250,8 @@ else filter_pattern=$(sed -e "s/#/\ /g" <<< "$temp") filter=1 fi + + # Delay grep -E "^[0-9]*$" <<< "$delay" &>/dev/null if [ $? -eq 0 ]; then temp=$delay @@ -254,6 +264,8 @@ else else usage "The delay must be a number between 100 and 900" fi + + # Exclude pattern if [ ! -z "$exclude_pattern" ]; then grep "#" <<< "$exclude_pattern" &>/dev/null if [ $? -eq 0 ]; then @@ -266,6 +278,8 @@ else -e "s/;/\n/g") <<< "$exclude_pattern") exclude=1 fi + + # Remove pattern if [ ! -z "$remove_pattern" ]; then grep "#" <<< "$remove_pattern" &>/dev/null if [ $? -eq 0 ]; then @@ -278,6 +292,8 @@ else -e "s/;/\n/g") <<< "$remove_pattern") remove=1 fi + + # Wait on match if [ -z "$wait_match" ]; then usage "The wait value must not be empty" else @@ -290,16 +306,19 @@ else usage "The wait value must be a number greater than zero" fi fi + + # Check requirements check_command grep 0 grep check_command sed 0 sed check_command tail 0 coreutils fi -# Process the given input file +# Prepare output first if [ $header -eq 1 ]; then print_output_header fi +# Finally, process the given input file if [ $follow -eq 1 ]; then monitor_input_file else