I am currently working with some of the functions in bash. But as of now I am very confused. Let me explain first what I want to do.
Target Output:
2016-11-18T05:48:01 NFS="NFS1" MOUNT_STATUS=True RUN_TIME=0.1
So to obtain this target output, I have to check all NFS mounted storage using df -hP. If it is mounted it will show TRUE otherwise FALSE, then actually the tricky part here is the script will have to create a test file in the mount path.
For example I run df -hP (output is below_ and check if /dev is mounted (if yes) my script will create a test file in /dev then delete test file, then record the start time and end time for file creation until the file deletion (maybe around 0.5ms)
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 8.5G 3.7G 4.9G 44% /
devtmpfs 902M 0 902M 0% /dev
tmpfs 913M 0 913M 0% /dev/shm
Here is my script as of now
now="$(date +%Y-%m-%dT%H:%M:%S)"
sp=" "
NFS="NFS1 NFS2 NFS3"
MOUNT_STATUS="$TRUE FALSE" #TRUE or FALSE
RUN_TIME="" #Start time to endtime of script per NFS
if [ $# -eq 0 ]; then
echo $now
set -e
else
for i in "$@"; do
echo -n "$now" ;
echo -n NFS=$(df -hP "$i" | awk '{print $NF}' | sed -n '1!p') 2>&1 | sed 's/^/ /' ;
echo $(if [[ "$i" == "/media /a/nbackup201 /a/ndt301" ]] ;
echo -n MOUNT_STATUS=;
then
echo 'TRUE' ;
else
echo $([[ "$i" != "/media /a/nbackup201 /a/ndt301" ]]) ;
echo 'FALSE' ; fi) | sed 's/^/ /'
done
fi
As of now, the script is still missing several parts but one of my problem is below
[root@strg ~]# sh script.sh /dev /run /boot a
2016-11-21T08:50:51 NFS=/dev MOUNT_STATUS=TRUE
2016-11-21T08:50:51 NFS=/run MOUNT_STATUS=TRUE
2016-11-21T08:50:51 NFS=/boot MOUNT_STATUS=TRUE
2016-11-21T08:50:51df: ‘a’: No such file or directory
NFS= MOUNT_STATUS=TRUE
[root@strg ~]#
So when I run the script and type a wrong mount point if will still show "NFS= MOUNT_STATUS=TRUE, I am confused what is wrong with the loop because it should show NFS=a STATUS=FALSE. Can someone help me?
Also if you are so kind, maybe you can tell me where can I insert the file creation/delete and to record the time as well? and what will be the best option/command to create the file? let me know if this is too confusing. I will try to explain better.