Bash (Linux) and Powershell (Windows) Scripts to Test File Access to Multiple Locations

Linux


Here’s the bash script with 2 example targets:


#!/bin/bash
while true
do
([ -e /mnt/test1/test1.txt ] && date >> /mnt/LOG/log1.txt || echo "FAILURE")
([ -e /mnt/test2/test2.txt ] && date >> /mnt/LOG/log2.txt || echo "FAILURE")
sleep 1
done


Save as say fileaccess.sh and run in bash as sh fileacess.sh

In the above example we’re testing access to 2 files, each in a different mount point. Time stamp is log for successes. Failure is echoed to the screen for failure. There’s a wait of 1 second.

It’s possible to do it as a one line to from the bash shell:


while true ; do ([ -e /mnt/test1/test1.txt ] && date >> /mnt/LOG/log1.txt || echo "FAILURE") ; ([ -e /mnt/test2/test2.txt ] && date >> /mnt/LOG/log2.txt || echo "FAILURE"); sleep 1 ; done


Powershell

Here’s the powershell script with 2 example targets:


"" > C:\LOG\log1.txt
"" > C:\LOG\log2.txt
while($TRUE){
If (test-path C:\mnt\test1\test1.txt){ [String](date) >> C:\LOG\log1.txt }
else { Write-Host "FAILURE" }
If (test-path C:\mnt\test2\test2.txt){ [String](date) >> C:\LOG\log2.txt }
else { Write-Host "FAILURE" }
sleep 1
}


Save as say filetester.ps1 and run in powershell .\filetester.ps1

Similarly, you could turn it into a one liner. Or you can just copy and paste it into powershell.

Intention

The idea is that you can set up a number of NFS mounts, and/or a number of CIFS shares, and test the availability of file access.

Comments