-2

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.

1

Your problem is that your script loops over every argument and executes all commands regardless of whether an argument is a proper mountpoint. I didn't test it, but the below solution should give you the output you want.

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" ;
if [ -d $i ]; then
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/^/ /'             
else
echo -n NFS=$i MOUNT_STATUS=false
done
fi
  • Hi. Thank you for your comment. Actually i am still confused even though you shared that. But i do appreciate it. I'll be testing and continue working first since my work is really a mess. – kukiaz Nov 22 '16 at 7:49
  • @kukiaz I added a check to see if the mount point exists before it executes any of the checks. It still may not give you the desired output if you feed it a directory that exists and isn't a mountpoint, but it should get you much closer to your desired outcome. – smokes2345 Nov 22 '16 at 14:59

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.