bash permission set to 777 but file execution still denied - bash

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

Related

permission denied in jenkins pipeline running bash script

Team, permission denied when trying to run script via jenkins pipeline any clue what could be the reason that shell script is not getting executed? I tried all combos I could.. still looking.. I gave all 777 perms in pipeline itself. I followed this but no luck.
/home/jenkins/agent/workspace/team/code-coverage-WIP#tmp/durable-29cd82d6/script.sh:
4: //: Permission denied
steps {
preBuild(bazel_init)
container('main') {
sh '''
pwd
ls -ltr
ls -ltr scripts/test-report.sh
chmod 777 scripts/test-report.sh
ls -ltr scripts/test-report.sh
BAZEL_OPTS="--config=ci --config=remote_cache_ci"
scripts/test-report.sh
'''.stripIndent()
output
+ ls -ltr scripts/test-report.sh
-rwxr-xr-x 1 jenkins jenkins 238 Dec 9 06:18 scripts/test-report.sh
+ chmod 777 scripts/test-report.sh
+ ls -ltr scripts/test-report.sh
-rwxrwxrwx 1 jenkins jenkins 238 Dec 9 06:18 scripts/test-report.sh
+ BAZEL_OPTS=--config=ci --config=remote_cache_ci
+ scripts/test-report.sh
/home/jenkins/agent/workspace/team/code-coverage-WIP#tmp/durable-29cd82d6/script.sh: 4: //: Permission denied
the script is
#!/bin/bash
set -e
which bazel
bazelbin=$(bazel info bazel-bin)
target="//src/cmd/gocoverage"
cmd="${bazelbin}/src/cmd/gocoverage/gocoverage_/gocoverage"
bazel build $target
$cmd -goprefix=go.corp.nvidia.com/maglev ${GOCOVERAGE_OPTS} $#
I had to remove #!/bin/bash from the script and then use like below in pipeline
sh '''
chmod 777 -R scripts
BAZEL_OPTS="--config=ci --config=remote_cache_ci"
bash scripts/test-report.sh
'''.stripIndent()

While_Run_For_loop_In_bash-Permission-denied

When I'm executing the bash script it said permission denied on a line. below the script and other details.
#!/bin/bash
find /var/opt/gitlab/backups/ -amin +60 |grep tar | cut -d '/' -f 6 >
/tmp/delete-files.txt
chmod +rw /var/opt/gitlab/backups/*.tar
chmod +rw /tmp/delete-files.txt
for i in `/tmp/delete-files.txt`
do
rm -rf /var/opt/gitlab/backups/$i
[root#git opt]# ./asaaa
./asaaa: line 10: /tmp/delete-files.txt: Permission denied'
[root#git opt]#
[root#git opt]# ll /tmp/delete-files.txt
-rw-r--r--. 1 root root 100 Mar 11 12:43 /tmp/delete-files.txt
[root#git opt]#
Help me to sort out this issue.
This line is incorrect:
for i in `/tmp/delete-files.txt`
Backticks mean command substitution. Your script will try to execute /tmp/delete-files.txt. This is not an executable file.
My guess is that what you wanted to do was:
for i in `cat /tmp/delete-files.txt`
Ie. execute cat command to print the contents of the /tmp/delete-files.txt and then loop through each of the printed lines in the for loop.

chown directory in bash

I am trying to chown a home directory test for an bash script. I need this functionality because of syncthing which is not syncing the ownerships.
#!/bin/bash
user=test
"chown $user:$user /home/$user"
When I use the above script, I get a message "test.sh: line 5: chown test:test ~/home/test/: No such file or directory
"
Output of
ls -l /home/ |grep test
drwx------ 5 pwresettest 1005 121 2. Nov 04:23 pwresettest
drwx------ 14 test 1001 4096 29. Okt 05:41 test
When I am using the command on the commandline, it works without problems.
Did I do something wrong?
The shell treats the quoted string as a single word to as the name of the command, rather than a command name followed by arguments. Simply take off the quotes you've added in your script:
#!/bin/bash
user=test
chown $user:$user /home/$user
When you use chown on the command line you aren't quoting the entire command. Don't do that in the script either. – Etan Reisner

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

Redirecting a bash error

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

Resources