Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
passing params to su in a script fails [solved]
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
DaggyStyle
Watchman
Watchman


Joined: 22 Mar 2006
Posts: 5929

PostPosted: Wed Oct 23, 2024 8:01 am    Post subject: passing params to su in a script fails [solved] Reply with quote

Greetings,

I'm working on a script that gets run as root and run it as other users with params, this is the script:
Code:
#!/bin/bash -x

do_virsh() {
   local uri=$1
   su -l -c "LC_ALL=C virsh -c ${uri} $@ 2>/dev/null | head -n -1" ${user}
}

test1() {
   local uri=$1
   shift

   do_virsh "${uri}" list --uuid $@ | grep -v 00000000-0000-0000-0000-000000000000
}

for uri in {qemu:///system,qemu:///session}; do
   users=root

   [ "${uri}" = "qemu:///session" ] && users=dagg

   for user in ${users}; do
      export user
      test1 ${uri} "--persistent"
   done
done


when I run it I get this:
Code:

+ for uri in {qemu:///system,qemu:///session}
+ users=root
+ '[' qemu:///system = qemu:///session ']'
+ for user in ${users}
+ export user
+ test1 qemu:///system --persistent
+ local uri=qemu:///system
+ shift
+ do_virsh qemu:///system list --uuid --persistent
+ local uri=qemu:///system
+ su -l -c 'LC_ALL=C virsh -c qemu:///system qemu:///system' list --uuid '--persistent 2>/dev/null | head -n -1' root
+ grep -v 00000000-0000-0000-0000-000000000000
su: unrecognized option '--uuid'
Try 'su --help' for more information.
+ for uri in {qemu:///system,qemu:///session}
+ users=root
+ '[' qemu:///session = qemu:///session ']'
+ users=dagg
+ for user in ${users}
+ export user
+ test1 qemu:///session --persistent
+ local uri=qemu:///session
+ shift
+ do_virsh qemu:///session list --uuid --persistent
+ local uri=qemu:///session
+ su -l -c 'LC_ALL=C virsh -c qemu:///session qemu:///session' list --uuid '--persistent 2>/dev/null | head -n -1' dagg
+ grep -v 00000000-0000-0000-0000-000000000000
su: unrecognized option '--uuid'
Try 'su --help' for more information.


any ideas how to pass the params correctly without using sed or switching to other utils such as sudo or doas?
_________________
Only two things are infinite, the universe and human stupidity and I'm not sure about the former - Albert Einstein


Last edited by DaggyStyle on Wed Oct 23, 2024 10:09 am; edited 1 time in total
Back to top
View user's profile Send private message
DaggyStyle
Watchman
Watchman


Joined: 22 Mar 2006
Posts: 5929

PostPosted: Wed Oct 23, 2024 10:09 am    Post subject: Reply with quote

after long search and trying, this works:
Code:
do_virsh() {
   local uri=$1
   shift

   su -f ${user} -s /bin/bash << EOS
   LC_ALL=C virsh -c ${uri} $@ 2>/dev/null | head -n -1
EOS
}

_________________
Only two things are infinite, the universe and human stupidity and I'm not sure about the former - Albert Einstein
Back to top
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 22697

PostPosted: Wed Oct 23, 2024 1:43 pm    Post subject: Reply with quote

"$@" expands to the arguments as individual words, which in the first script allowed them to leak out and become visible to su. Fortunately, the arguments meant for virsh confused su into failing, rather than doing the wrong thing. Piping is probably fine here. Another option would have been to use $* instead of $@.

You don't need to export user, since you only use it as an in-process variable later.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum