Redirecting a bash error - bash

I am writing a bash script, which has a problem:
path=$(pwd)
data=$(ls -al $path) > /dev/null 2>/dev/null
The problem occurs if $path is a "locked" directory (no permission for user x), call it "BadDir". In that case, the program outputs:
ls: cannot access /home/user/.../BadDir/..: Permission denied
All I want is to hide this output.
I know there is redirection to /dev/null but I don't know how to use it in this particular case.

you can redirect all error message to another with using EXEC
for test, first create folder
mkdir /tmp/t/
sudo chown root /tmp/t/
sudo chgrp root /tmp/t/
sudo chmod 400 /tmp/t/
e.g:
ls -al /tmp/t/
output:
ls: cannot open directory /tmp/t/: Permission denied
and using EXEC first of file:
exec 2>/dev/null
ls -al /tmp/t/
with exec you can control and redirect all error message or another output

Related

How to execute and log as specific user?

I need to run a command/shell-script as on other user. The stdout shall be written to a logfile.
I tried it like this:
export LOGDIR=foo/bar
sudo -u www command /home/www > /home/www/$LOGDIR/command.log
But I get always this error:
-bash: /home/www/foo/bar/command.log: Permission denied
You can try this easily with this little stupid example:
sudo -u edeviser ls /home/edeviser > /home/edeviser/$LOGDIR/ls.log
I see the problem is, that the redirection with > is not done as the user specified by the ´-u´ option.
How to execute the command and log as the same specific user?
You can try something like
sudo -u www bash -c "command /home/www > /home/www/$LOGDIR/command.log"
export logdir="foo/bar"
sudo -u www command /home/www |sudo -u www dd of="/home/www/$logdir/command.log"

echo string into file fails when cp succeeds

OS: Ubunutu 14.04
In the /home/ubuntu directory, I created the following script:
echo >000-default.conf.test
sudo cp 000-default.conf.test /etc/apache2/sites-enabled/000-default.conf.test
sudo echo 'this is a test'>> /etc/apache2/sites-enabled/000-default.conf.test
sudo cat /etc/apache2/sites-enabled/000-default.conf.test
When I run the script, I get the following error message:
./test_f.sh: line 3: /etc/apache2/sites-enabled/000-default.conf.test: Permission denied
Any ideas why I am getting the error message when the copy operation is succeeding?
Sure.
Redirecting output into files is done by the shell, not by sudo. So if the shell is running under unprivileged user, then >> is invoked earlier than privileges are acquired by sudo.
You can use the following approach:
echo >000-default.conf.test
sudo cp 000-default.conf.test /etc/apache2/sites-enabled/000-default.conf.test
echo 'this is a test' | sudo tee -a /etc/apache2/sites-enabled/000-default.conf.test >/dev/null
sudo cat /etc/apache2/sites-enabled/000-default.conf.test
By the way, instead of
echo >000-default.conf.test
you can use
touch 000-default.conf.test
or even
>000-default.conf.test

How can use invert grep after pipe?

I want to list directory disk usage in a fileserver.
I also want to ignore the error messages. Here is my command:
du -sh * | grep -v "Permission denied" | sort -n
The result still contains the permission denied lines:
du: cannot access './myFile1/': Permission denied
du: cannot access './myFile2/': Permission denied
du: cannot access './myFile3/': Permission denied
What am I doing wrong?
This is because the "Permission denied" is sent through standard error, not through standard output.
If you don't want this information, just silence it by redirecting stderr to /dev/null:
du -sh * 2>/dev/null | sort -n
This happens with all these error messages:
$ touch a
$ ls a asfasd
ls: cannot access asfasd: No such file or directory
a
$ ls a asfasd | grep cannot
ls: cannot access asfasd: No such file or directory
$ ls a asfasd 2>/dev/null
a

bash permission set to 777 but file execution still denied

I wrote a bash script and am trying to execute it from sh but I am getting a "permission denied error". I set to permissions to 777 but it is still preventing me from executing the file.
# which sh
/bin/sh
# ls -l myscript.sh
-rwxrwxrwx 1 root wheel 974 Nov 4 09:16 myscript.sh
# ./myscript.sh
zsh: permission denied: ./myscript.sh
Any help would be greatly appreciated.
Not sure what could be the reason but try invoking shell explicitly passing your
script as argument/command to the Shell like sh myscript.sh

Why can't I echo contents into a new file as sudo? [duplicate]

This question already has answers here:
Bash Deployment Script Permission Problem
(2 answers)
Closed 8 years ago.
I came across this weird problem just now and I can't seem to get to the bottom of it. I'm trying to add a config file to /etc/sudoers.d/ but get permission denied. In the example below, the file "tweedle" doesn't exist and:
drwxr-xr-x 2 root root 4.0K Jan 2 18:27 sudoers.d/
So here's the command:
$ sudo echo "tweedle ALL=(ALL) ALL" > /etc/sudoers.d/tweedle
-bash: /etc/sudoers.d/tweedle: Permission denied
It doesn't even work when I break it into two commands:
$ sudo touch /etc/sudoers.d/tweedle
$ sudo echo "poodle" > /etc/sudoers.d/tweedle
When I tested it locally, same problem:
$ cd ~
$ mkdir -m 755 tweedle
$ sudo chown root:root tweedle
$ sudo echo "battle" > ~/tweedle/beetle
-bash: /home/spanky/tweedle/beetle: Permission denied
$ sudo touch tweedle/beetle
$ sudo echo "battle" > tweedle/beetle
-bash: tweedle/beetle: Permission denied
Without sudo, all is well:
$ cd ~
$ mkdir poodle
$ echo "noodle" > poodle/bottle
$ cat poodle/bottle
noodle
Thoughts?
The echo command is being run as root, but the redirection is done by your shell, so it's executed as the current user, not as root.
The simplest solution is to invoke a root shell to run both the command and the redirection.
Rather than:
sudo echo line > file
try this:
sudo sh -c 'echo line > file'
or
sudo bash -c 'echo line > file'
The answer is to use "tee" with a pipe, a command I wasn't familiar with, so you can use sudo for the second half:
$ echo "tweedle ALL=(ALL) ALL" | sudo tee /etc/sudoers.d/tweedle

Resources