Shell Script Error No such file or directory - linux

i tried to execute an easy shell skript with curl comand.
#!/usr/bin/env bash
.~/.foo
curl -n ${URL}/submit/foo/netrc
i executed this shell script with sudo rights and got the error message /root/.foo No such file or directory This hidden file .foo has an variable URL. If i executed this script without sudo rights. I got the error forbidden. Where is my mistake ?

Related

Bash script not running , user issue

i've created a bash script batch-create-users.sh and I want execute it from the terminal, Im on the folder when the script is and I run the command
./batch-create-users.sh and I got error
the file './batch-create-users.sh' is not executable by this user
I entered as sudo -s and give the password but it doesn't help, any idea?
Give execute privilege to the file before executing.
chmod +x batch-create-users.sh

permission denied-public key while running a shell script which contains ansible command

I have an shell script
#!/bin/bash
echo "hello world"
ansible zookservers -i /home/ec2-user/kafka_scripts/ansible_rep/inventory -a "/home/ec2-user/kafka_2.11-0.9.0.0/bin/kafka-server-start.sh kafka_2.11-0.9.0.0/config/server.properties" --sudo
my error was
| FAILED => SSH Error: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
while connecting to 172.30.0.243:22
when I run the ansible command then it is executing
but when I kept it inside a shell script I am getting the above error
my shell is with chmod 777 permission - so the problem it not with shell execution permission
I found something interesting
when I run the script from any other place I'm getting error. but when run it in ansible directory then it is executed.
later when I run it from any other directory it is not throwing any error.
so the problem is with initial public key authentication
I found something intersting -- when I run the script from any other place I'm getting error. but when run it in ansible directory then it is executed
The problem most likely is the location of your ansible.cfg. Ansible will use the config from one of these locations (from the docs):
ANSIBLE_CONFIG (an environment variable)
ansible.cfg (in the current directory)
.ansible.cfg (in the home directory)
/etc/ansible/ansible.cfg
First match wins.
So it uses the config from the "ansible directory", if called from there. If called from any other location there is no ansible.cfg in that directory. Since this is where you have stored you username and private key location the authentication fails.
The best solution seems to be to utilize the environment variable ANSIBLE_CONFIG. Just store the path to your ansible.cfg in there. I think it is /home/ec2-user/kafka_scripts/ansible_rep/ansible.cfg, right?
You can set that variable in your script.
ANSIBLE_CONFIG=/home/ec2-user/kafka_scripts/ansible_rep/ansible.cfg ansible zookservers -i /home/ec2-user/kafka_scripts/ansible_rep/inventory -a "/home/ec2-user/kafka_2.11-0.9.0.0/bin/kafka-server-start.sh kafka_2.11-0.9.0.0/config/server.properties" --sudo

Bash: sourcing file as user from script

I am creating a script meant to be run as superuser that reads a file and runs a number of scripts on behalf of all users. The important bit is this:
sudo -u $user -H source /home/$user/list_of_commands
However, whether I encose the command with quotesor not, this fails with:
sudo: source /home/user/list_of_commands: command not found
I have even tried with the . bash builtin:
sudo: . /home/user/list_of_commands: command not found
Of course running source outside a sudo environment works. I thought there might be a PATH problem, and I tried to bypass it by providing the full path to source. However, I cannot find the executable: which source returns which: no source in (/usr/local/sbin:usr/local/bin:usr/bin). So I'm stuck.
How do I make a script source a file as a user?
source is a builtin not a command, use it with bash -c:
sudo -u $user -H bash -c "source /home/$user/list_of_commands"

ssh command to run remote script exist shell on remote server when switching user

When I run a script such as this:
ssh -t root#10.10.10.10 '/tmp/somescript.sh'
where the script is defined as:
#!/bin/sh
mkdir -p /data/workday/cred
chown -R myuser:myuser /data
su myuser - # <------- NOTICE THIS ! ! ! !
rpm -Uvp --force --nodeps --prefix /data/place /data/RPMs/myrpm.rpm
Notice the above su command.
If I comment-out the su command, the script runs remotely and then my shell prompt returns to where I came from ( same server where I ran the ssh command above )
But leaving the script as listed above, causes the script to complete successfully but the shell prompt stays on the remote server.
How can I prevent that ? Making sure that the issuer of the rpm command is a different user than root just a listed ?
But leaving the script as listed above, causes the script to complete successfully but the shell prompt stays on the remote server.
Not exactly. The script is running up to the su command, which spawns a new subshell, and stopping there until you exit the shell. Until you exit that shell, the rpm command never runs, and when it does, it runs as root.
If you want to run the rpm command as a non-root user, you'd need to do something a little different, like:
sudo -u myuser rpm -Uvp ...
add 'exit' at the end of your script

Permission issues, not able to run script as root

I am running a shell script(Script.sh) which, itself, is calling other shell scripts(
Script2.sh, Script3.sh ...etc). I logged in as a root user and have given execution permission to all the scripts. But on when I execute "ls -l" the scripts still dont have execution permissions displayed on file attributes column. "Script.sh" runs by following syntax:
root#freescale $ sh Script.sh
But this script is not able to execute other scripts(Script2.sh, Script3.sh) being called by it. Error is reported as "Permission denied"
I already gave execution permission by chmod command but then also neither the permissions are changing nor the scripts(Script2.sh, Script3.sh ..) are executing.
I hope this error is due to the reason that Script2.sh are called in Script3.sh as:
./Script2.sh
./Script3.sh
And if I write it as :
sh Script2.sh
It executes but doesn't able to execute other script which are called inside Script2.sh and reports same error as "Permission Denied"
Make sure that your partition is not mounted with the noexec flag (which - as the name suggests - prevents making any files executable)
Kindly make sure the permission and ownership for the script.sh file, also try
# chmod 755 script.sh
# chown root.root script.sh
Thanks.

Resources