Skip to contentSkip to conversation

network/nfs_lib.sh: Use double quotes for grep pattern

PATCH New Xiao Yang 3 participants 7 messages Jun 8, 2020 – Jun 18, 2020

lore
Share & tools
lore

Xiao Yang

author
#0 lore raw

PATCH 1/1 network/nfs_lib.sh: Use double quotes for grep pattern

sh -c 'grep -q '^[# ]*udp *= *y' ...' in nfs_server_udp_enabled() cannot processe nested single quotes correctly if rsh/ssh is used and gets the error:
--------------------------------------------
grep: Unmatched [ or [^ (or grep: Unmatched [, [^, [:, [., or [=) nfs01 1 TCONF: UDP support disabled on NFS server
--------------------------------------------

This issue make many NFS tests including UDP become TCONF even if NFS supports UDP so use double quotes inside to fix it.

Fixes 0f1c36c51 ("nfs: Detect disabled UDP")
---
 testcases/network/nfs/nfs_stress/nfs_lib.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
testcases/network/nfs/nfs_stress/nfs_lib.sh +1 -1
index f7eb57d66..e236cd485 100644
@@ -58,7 +58,7 @@ nfs_server_udp_enabled()
 	tst_rhost_run -c "[ -f /etc/nfs.conf ]" || return 0
 	config=$(tst_rhost_run -c 'for f in $(grep ^include.*= '/etc/nfs.conf' | cut -d = -f2); do [ -f $f ] && printf "$f "; done')
 
-	tst_rhost_run -c "grep -q '^[# ]*udp *= *y' /etc/nfs.conf $config"
+	tst_rhost_run -c "grep -q \"^[# ]*udp *= *y\" /etc/nfs.conf $config"
 }
 
 nfs_setup_server()
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/1 network/nfs_lib.sh: Use double quotes for grep pattern

Hi Xiao,

testcases/network/nfs/nfs_stress/nfs_lib.sh nfs_server_udp_enabled() · line 61

Quoted 7 lines

+++ b/testcases/network/nfs/nfs_stress/nfs_lib.sh

testcases/network/nfs/nfs_stress/nfs_lib.sh +1 -1
@@ -58,7 +58,7 @@ nfs_server_udp_enabled()
 	tst_rhost_run -c "[ -f /etc/nfs.conf ]" || return 0
 	config=$(tst_rhost_run -c 'for f in $(grep ^include.*= '/etc/nfs.conf' | cut -d = -f2); do [ -f $f ] && printf "$f "; done')
 
-	tst_rhost_run -c "grep -q '^[# ]*udp *= *y' /etc/nfs.conf $config"
+	tst_rhost_run -c "grep -q \"^[# ]*udp *= *y\" /etc/nfs.conf $config"

Good catch. But I wonder if we shouldn't fix tst_rhost_run instead, to avoid this error in the future. How about replacing ' with \" in $cmd?

Kind regards,
Petr

author
#2 lore raw

PATCH 1/1 network/nfs_lib.sh: Use double quotes for grep pattern

Quoted 9 lines · Petr Vorel

Hi Xiao,

Quoted 4 lines

+++ b/testcases/network/nfs/nfs_stress/nfs_lib.sh

(quoted diff) +0 -0
@@ -58,7 +58,7 @@ nfs_server_udp_enabled()
  	tst_rhost_run -c "[ -f /etc/nfs.conf ]" || return 0
  	config=$(tst_rhost_run -c 'for f in $(grep ^include.*= '/etc/nfs.conf' | cut -d = -f2); do [ -f $f ]&&  printf "$f "; done')
Quoted 4 lines

testcases/network/nfs/nfs_stress/nfs_lib.sh nfs_server_udp_enabled() · line 61

Quoted 2 lines
testcases/network/nfs/nfs_stress/nfs_lib.sh +1 -1
-	tst_rhost_run -c "grep -q '^[# ]*udp *= *y' /etc/nfs.conf $config"
+	tst_rhost_run -c "grep -q \"^[# ]*udp *= *y\" /etc/nfs.conf $config"

Good catch. But I wonder if we shouldn't fix tst_rhost_run instead, to avoid this error in the future. How about replacing ' with \" in $cmd?

Hi Petr,

It is fine for me to fix the issue in tst_rhost_run() but I didn't find a better fix, could you provide an example about your idea?

Best Regards,
Xiao Yang

Quoted 7 lines

Kind regards,
Petr

.

#3 lore raw

PATCH 1/1 network/nfs_lib.sh: Use double quotes for grep pattern

In reply to Petr Vorel
Quoted 3 lines · Petr Vorel

Hi Xiao,

Applied the patch, thanks!

Quoted 11 lines

Review nfs_server_udp_enabled() · line 61

Quoted 8 lines

+++ b/testcases/network/nfs/nfs_stress/nfs_lib.sh

(quoted diff) +1 -1
@@ -58,7 +58,7 @@ nfs_server_udp_enabled()
 	tst_rhost_run -c "[ -f /etc/nfs.conf ]" || return 0
 	config=$(tst_rhost_run -c 'for f in $(grep ^include.*= '/etc/nfs.conf' | cut -d = -f2); do [ -f $f ] && printf "$f "; done')
 
-	tst_rhost_run -c "grep -q '^[# ]*udp *= *y' /etc/nfs.conf $config"
+	tst_rhost_run -c "grep -q \"^[# ]*udp *= *y\" /etc/nfs.conf $config"

Good catch. But I wonder if we shouldn't fix tst_rhost_run instead, to avoid this error in the future. How about replacing ' with \" in $cmd?

Perhaps this:

testcases/lib/tst_net.sh +1 -2
index 59b373e..9c8f163 100644
@@ -172,8 +172,7 @@ tst_rhost_run()
        local output=
        local ret=0
        if [ -n "${TST_USE_SSH:-}" ]; then
-               output=`ssh -n -q $user@$RHOST "sh -c \
-                       '$pre_cmd $cmd $post_cmd'" $out 2>&1 || echo 'RTERR'`
+               output=$(ssh -n -q $user@$RHOST "$pre_cmd $cmd $post_cmd" $out 2>&1 || echo 'RTERR')
        elif [ -n "$TST_USE_NETNS" ]; then
                output=`$LTP_NETNS sh -c \
                        "$pre_cmd $cmd $post_cmd" $out 2>&1 || echo 'RTERR'`
#4 lore raw

PATCH 1/1 network/nfs_lib.sh: Use double quotes for grep pattern

Hi Alexey, Xiao,

Quoted 7 lines

Perhaps this:

testcases/lib/tst_net.sh +0 -0
index 59b373e..9c8f163 100644

testcases/lib/tst_net.sh tst_rhost_run() · line 178

Quoted 9 lines
testcases/lib/tst_net.sh +1 -2
@@ -172,8 +172,7 @@ tst_rhost_run()
        local output=
        local ret=0
        if [ -n "${TST_USE_SSH:-}" ]; then
-               output=`ssh -n -q $user@$RHOST "sh -c \
-                       '$pre_cmd $cmd $post_cmd'" $out 2>&1 || echo 'RTERR'`
+               output=$(ssh -n -q $user@$RHOST "$pre_cmd $cmd $post_cmd" $out 2>&1 || echo 'RTERR')
        elif [ -n "$TST_USE_NETNS" ]; then
                output=`$LTP_NETNS sh -c \
                        "$pre_cmd $cmd $post_cmd" $out 2>&1 || echo 'RTERR'`

Nice! Much simpler than what what I was just going to post (sed replacement). Would it work for rsh as well? i.e. can it work without sh -c?
I have no working rsh setup.

And removing it from all 3 variants would be great (keeping them to be the same, also it might allow to also use shell functions, which doesn't work with sh -c "..."). Why was sh -c "..." used anyway?

BTW I have more tst_net.sh, but I post them after we solve this one (as replacing quotes with sed, which is in my prepared patchset is ugly).

Kind regards,
Petr

#5 lore raw

PATCH 1/1 network/nfs_lib.sh: Use double quotes for grep pattern

Quoted 23 lines · Petr Vorel

Hi Alexey, Xiao,

Quoted 7 lines

Perhaps this:

testcases/lib/tst_net.sh +0 -0
index 59b373e..9c8f163 100644

testcases/lib/tst_net.sh tst_rhost_run() · line 178

Quoted 9 lines
testcases/lib/tst_net.sh +1 -2
@@ -172,8 +172,7 @@ tst_rhost_run()
        local output=
        local ret=0
        if [ -n "${TST_USE_SSH:-}" ]; then
-               output=`ssh -n -q $user@$RHOST "sh -c \
-                       '$pre_cmd $cmd $post_cmd'" $out 2>&1 || echo 'RTERR'`
+               output=$(ssh -n -q $user@$RHOST "$pre_cmd $cmd $post_cmd" $out 2>&1 || echo 'RTERR')
        elif [ -n "$TST_USE_NETNS" ]; then
                output=`$LTP_NETNS sh -c \
                        "$pre_cmd $cmd $post_cmd" $out 2>&1 || echo 'RTERR'`

Nice! Much simpler than what what I was just going to post (sed replacement). Would it work for rsh as well? i.e. can it work without sh -c?
I have no working rsh setup.

Quoted 3 lines

And removing it from all 3 variants would be great (keeping them to be the same, also it might allow to also use shell functions, which doesn't work with sh -c "..."). Why was sh -c "..." used anyway?

Hi Petr,

Don't remember, so if all work without it I would remove it. For rsh, I doubt that's is used now days, let's remove it too? The replacement (ssh) in the tst_rhost_run() has been for a long time already.

Quoted 6 lines

BTW I have more tst_net.sh, but I post them after we solve this one (as replacing quotes with sed, which is in my prepared patchset is ugly).

Kind regards,
Petr

#6 lore raw

PATCH 1/1 network/nfs_lib.sh: Use double quotes for grep pattern

Hi Alexey,

Quoted 9 lines
Quoted 3 lines

And removing it from all 3 variants would be great (keeping them to be the same, also it might allow to also use shell functions, which doesn't work with sh -c "..."). Why was sh -c "..." used anyway?

Hi Petr,

Don't remember, so if all work without it I would remove it. For rsh, I doubt that's is used now days, let's remove it too? The replacement (ssh) in the tst_rhost_run() has been for a long time already.

+1 agree, go ahead :).

Kind regards,
Petr