Hello,
I am using a script where I check if /home and /backup are present in /etc/mtab but can't manage the 'and' operator in grep. Currently I use the 'if grep -q "/home" ; then ......... then I do 'fi' and again 'if grep -q "/backup" to finally go to my commands. I tried to make grep do the AND combo but got errors as it works only on one line. I want my command to start only if both /home and /backup are present in mtab. With my dual check method it works, but I want a clean lean script.
Regards, Rony.
Here you go.....
mount | grep -q home HOMESTATUS=$? mount | grep -q backup BACKUPSTATUS=$?
if [ $HOMESTATUS -eq 0 -a $BACKUPSTATUS -eq 0 ]; then echo "You got both man, do your thing..." exit 0; else echo "Error -- either home or backup missing -- Or is it BOTH !%$!!@#%!" exit 1; fi
Regards R. K. Rajeev
P.S. : Dont rely too much on mtab, a read only filesystem situation might mean you have old/stale data in it, always pipe live mount command or use /proc/mounts.
On 26 February 2013 23:45, gnulinuxist@gmail.com gnulinuxist@gmail.com wrote:
Hello,
I am using a script where I check if /home and /backup are present in /etc/mtab but can't manage the 'and' operator in grep. Currently I use the 'if grep -q "/home" ; then ......... then I do 'fi' and again 'if grep -q "/backup" to finally go to my commands. I tried to make grep do the AND combo but got errors as it works only on one line. I want my command to start only if both /home and /backup are present in mtab. With my dual check method it works, but I want a clean lean script.
Regards, Rony. -- http://mm.ilug-bom.org.in/mailman/listinfo/linuxers
On 26 February 2013 23:45, gnulinuxist@gmail.com gnulinuxist@gmail.com wrote:
Hello,
I am using a script where I check if /home and /backup are present in /etc/mtab but can't manage the 'and' operator in grep. Currently I use the 'if grep -q "/home" ; then ......... then I do 'fi' and again 'if grep -q "/backup" to finally go to my commands. I tried to make grep do the AND combo but got errors as it works only on one line. I want my command to start only if both /home and /backup are present in mtab. With my dual check method it works, but I want a clean lean script.
Regards, Rony. -- http://mm.ilug-bom.org.in/mailman/listinfo/linuxers
Here you go.....
mount | grep -q home HOMESTATUS=$? mount | grep -q backup BACKUPSTATUS=$?
if [ $HOMESTATUS -eq 0 -a $BACKUPSTATUS -eq 0 ]; then echo "You got both man, do your thing..." exit 0; else echo "Error -- either home or backup missing -- Or is it BOTH !%$!!@#%!" exit 1; fi
Regards R. K. Rajeev
P.S. : Dont rely too much on mtab, a read only filesystem situation might mean you have old/stale data in it, always pipe live mount command or use /proc/mounts.
On 26 February 2013 23:45, gnulinuxist@gmail.com gnulinuxist@gmail.com wrote:
I am using a script where I check if /home and /backup are present in /etc/mtab but can't manage the 'and' operator in grep. Currently I use the 'if grep -q "/home" ; then ......... then I do 'fi' and again 'if grep -q "/backup" to finally go to my commands. I tried to make grep do the AND combo but got errors as it works only on one line. I want my command to start only if both /home and /backup are present in mtab. With my dual check method it works, but I want a clean lean script.
How about:
if [ $( grep -cE '/(home|backup)' /etc/mtab ) = 2 ]; then echo Found both mounts else echo Found only one of the two mounts fi
(Refine as per your needs)
Binand
On 27 February 2013 10:41, Binand Sethumadhavan binand@gmail.com wrote:
On 26 February 2013 23:45, gnulinuxist@gmail.com gnulinuxist@gmail.com wrote:
I am using a script where I check if /home and /backup are present in /etc/mtab but can't manage the 'and' operator in grep. Currently I use the 'if grep -q "/home" ; then ......... then I do 'fi' and again 'if grep -q "/backup" to finally go to my commands. I tried to make grep do the AND combo but got errors as it works only on one line. I want my command to start only if both /home and /backup are present in mtab. With my dual check method it works, but I want a clean lean script.
How about:
if [ $( grep -cE '/(home|backup)' /etc/mtab ) = 2 ]; then echo Found both mounts else echo Found only one of the two mounts fi
(Refine as per your needs)
Fails for Case where mount points /home and /home_new exist. Still get count 2 with no backup. In addition, if i have /backup mounted too, the script would actually fail, since the count now becomes 3 ! :-P
Regards R. K. Rajeev
Binand
On 27 February 2013 10:52, Rajeev R. K. rajeevrk@xlncenterprises.com wrote:
How about:
if [ $( grep -cE '/(home|backup)' /etc/mtab ) = 2 ]; then echo Found both mounts else echo Found only one of the two mounts fi
(Refine as per your needs)
Fails for Case where mount points /home and /home_new exist. Still get count 2 with no backup. In addition, if i have /backup mounted too,
Come on man, that's why I specifically mentioned "refine to your needs". If I were to refine this for Rony's needs as he articulated, I'd write that regex as:
'^[^[:space:]]*[[:space:]]/(home|backup)[[:space:]]'
Which covers all the special cases you mention and some more.
Binand
On 27 February 2013 10:58, Binand Sethumadhavan binand@gmail.com wrote:
On 27 February 2013 10:52, Rajeev R. K. rajeevrk@xlncenterprises.com wrote:
How about:
if [ $( grep -cE '/(home|backup)' /etc/mtab ) = 2 ]; then echo Found both mounts else echo Found only one of the two mounts fi
(Refine as per your needs)
Fails for Case where mount points /home and /home_new exist. Still get count 2 with no backup. In addition, if i have /backup mounted too,
Come on man, that's why I specifically mentioned "refine to your needs". If I were to refine this for Rony's needs as he articulated, I'd write that regex as:
'^[^[:space:]]*[[:space:]]/(home|backup)[[:space:]]'
Which covers all the special cases you mention and some more.
Lolz, No worries man, i was just approaching it from a design perspective, rather than targeting code brevity. I still say having independent tests makes most of these edge cases moot, and plus has another important advantage from a usability perspective. It allows you to specifically inform the user of why the script aborted, specifically identifying which of the 2 required mount points was not available(or both, for that matter). If the script is to fail, let it fail with class :-)
Regards R. K. Rajeev
Binand
On 26 February 2013 23:45, gnulinuxist@gmail.com gnulinuxist@gmail.com wrote:
I am using a script where I check if /home and /backup are present in /etc/mtab but can't manage the 'and' operator in grep.
Just came across this one.
if mountpoint -q /home; then echo "/home is mounted" fi if mountpoint -q /backup; then echo "/backup is mounted" fi
No hassles, no grep, no -Ecw... :-)
Binand