0

I'm trying to compare a number with a digit from first column of required files in a folder. My current script only works if there is single line in that file. It fails whenever any file has more than one line.

cat 192.168.1.2_time-final2_timepls
86.6333 /home/fes/nginx/Templates/test.default

cat 192.168.1.3_time-final2_timepls
8 /home/fes/nginx/createfile
122 /home/fes/nginx/Templates/rtPortal.default

cat 192.168.1.4_time-final2_timepls
981 /home/fes/nginx/Templates/test.default

My Code:

dir1="/home/user1/test/time-final/*_time-final2_timepls"

y=100

for files3 in $dir1; do

        z=$(cat $files3 | awk '{print $1}')

        if awk 'BEGIN{exit ARGV[1]>ARGV[2]}' "$z" "$y"
                then
                  echo "z is smaller than y"
        else
                  echo "z greater than y" "Filepath: "  $files3
        fi


done

Actual Output:

[root@user1]# ./my-script.sh
z smaller than y
z greater than y Filepath:  /home/user1/test/time-final/192.168.1.3_time-final2_timepls
z greater than y Filepath:  /home/user1/test/time-final/192.168.1.4_time-final2_timepls

I'm trying to get this output which will also print the line from the file if it satisfies condition z>y:

z smaller than y 
z smaller than y 
z greater than y Filepath:  /home/user1/test/time-final/192.168.1.3_time-final2_timepls    Filename: /home/fes/nginx/Templates/rtPortal.default 
z greater than y Filepath:  /home/user1/test/time-final/192.168.1.4_time-final2_timepls    Filename: /home/fes/nginx/Templates/test.default
6
  • 1
    Could you please describe what it is all your questions are about. It feels like we're helping you build some huge sprawling piece of advanced management application, similar to Ansible, I'm not sure what the use cases are and what the end goal might possibly be. We asked you whether rsync --dry-run would do some of what you're trying to do, or whether Ansible or some other existing management software would help, but I don't think you ever answered and it's evident that you didn't care.
    – Kusalananda
    Jul 24 at 8:02
  • ok. Here's my algorithm for what I'm building: Sample algorithm: 1) Take md5sums of required files from all 5 servers. 2) Compare md5ums of files from all 5 servers with files on Active server 3) Report if any file is not synced. 4) Take care of any missing files on servers. 5) Log the Last modification time of that file(which was not synced) and for how much time that file was not in sync.
    – n1Ls
    Jul 24 at 8:07
  • rsync --dry-run was an option but what I need more is "reporting" more than actually syncing files. I already have filesync deployed in my environment but if any of the file dosent get synced. I need to report/LOG that file's last modification file and for how long that file was not insync
    – n1Ls
    Jul 24 at 8:09
  • 1
    There's something I'm missing. If the files on the servers should be in sync with your "Active server", then why are the files not in sync? Can't whoever changes them change be told to not do that? Also, why aren't the files shared via NFS or some other network file system, if the same files have to be in sync on several servers on a local network? Code like the code I gave you solves tiny problems, but solutions to tiny problems can't always be naively concatenated to solve larger issues.
    – Kusalananda
    Jul 24 at 8:22
  • Your previous answer really helped me a lot. I'm given this project with respective to Reporting perspective and I cannot change existing infra so I'm kinda forced to implement this. Thanks for your concern!
    – n1Ls
    Jul 24 at 8:35
0

Anyways, I got the answer from a previous solution:

y=100

awk '{print $1, $2}' *_time-final2_timepls |
        while read -r num1 path1; do
        z=$num1
        if awk 'BEGIN{exit ARGV[1]>ARGV[2]}' "$z" "$y"
                then
                  echo "z is smaller than y"
        else
                  echo "z greater than y" "Filepath: "  $path1
        fi    
done

Output:

z is smaller than y
z is smaller than y
z greater than y Filepath:  /home/fes/nginx/Templates/rtPortal.default
z greater than y Filepath:  /home/fes/nginx/Templates/test.default

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.