Why sudo cat gives a Permission denied but sudo vim works fine?

前端 未结 6 1955
生来不讨喜
生来不讨喜 2021-01-29 22:41

I am trying to automate the addition of a repository source in my arch\'s pacman.conf file but using the echo command in my shell script. However, it fails like th

6条回答
  •  醉酒成梦
    2021-01-29 22:59

    http://www.innovationsts.com/blog/?p=2758

    As the instructions are not that clear above I am using the instructions from that blog post. With examples so it is easier to see what you need to do.

    $ sudo cat /root/example.txt | gzip > /root/example.gz
    -bash: /root/example.gz: Permission denied

    Notice that it’s the second command (the gzip command) in the pipeline that causes the error. That’s where our technique of using bash with the -c option comes in.

    $ sudo bash -c 'cat /root/example.txt | gzip > /root/example.gz'
    $ sudo ls /root/example.gz
    /root/example.gz

    We can see form the ls command’s output that the compressed file creation succeeded.

    The second method is similar to the first in that we’re passing a command string to bash, but we’re doing it in a pipeline via sudo.

    $ sudo rm /root/example.gz
    $ echo "cat /root/example.txt | gzip > /root/example.gz" | sudo bash
    $ sudo ls /root/example.gz
    /root/example.gz

提交回复
热议问题