How can I execute a series of commands in a bash subshell as another user using sudo?

后端 未结 6 887
别跟我提以往
别跟我提以往 2021-01-30 10:33

I\'m writing a bash script that needs to sudo multiple commands. I can do this:

( whoami ; whoami )

but I can\'t do this:

sudo          


        
6条回答
  •  醉话见心
    2021-01-30 11:29

    for example try this, I tested it:

    sudo bash -c "cd /;ls;ls|grep o"
    

    In this example you first change dir to /root, next list root directory and finally for root directory filter only directories having name with letter 'o'.

    But i thing better way is writting script that do all you need and give exitcode for all complex action. Then you can sudo script instead group of single commands like example above.

提交回复
热议问题