This question already has answers here:
Pass commands as input to another command (su, ssh, sh, etc)
(3 answers)
Why can't I use 'sudo su' within a shell script? How to make a shell script run with sudo automatically
(6 answers)
how to execute multiple commands after sudo command
(2 answers)
Closed 5 years ago.
I have a little bash script to run, but it appears to stop without errors on the second line:
export REQUIRE_TRIGGER=0
sudo -s -H
killall ptpd ntpd
nice -n -19 ptpd -gGW -b eth0 -s2 -i NTP -t -c D
The script is in a file. What am I missing?
try to do
sudo killall ptpd ntpd
sudo nice -n -19 ptpd -gGW -b eth0 -s2 -i NTP -t -c D
Related
This question already has answers here:
Pass args for script when going thru pipe
(3 answers)
Closed 9 months ago.
I have written a shell script to configure a development environment and and retrieving using cURL. The script takes up to 3 flags, -d, -f and -s.
How do I pass the flags to the shell script?
Here is the command to run the bash script:
$ curl -sL https://example.com/setup.sh | bash
Here is my first (failed) attempt to pass flags to the script:
$ curl -sL https://example.com/setup.sh | bash -dfs
bash: -d: invalid option
Can anyone explain how to do this?
Use the -s argument:
curl -sL https://example.com/setup.sh | bash -s -- -dfs
This question already has answers here:
Why bash alias doesn't work in scripts? [duplicate]
(3 answers)
Closed 4 years ago.
On ubuntu 18.10 I've problem with a simple script.
If I execute this command directly from shell it works:
drush -y rsync #d8.live:web/sites/default/files #self:sites/default --delete -vv
If I create a .sh script with:
#!/bin/bash
drush -y rsync #d8.live:web/sites/default/files #self:sites/default --delete -vv
The script doesn't work and the drush command returns me an error:
The "--delete" option does not exist.
The command and the script are running from the same directory and the same user.
Where is the problem?
PS: "drush" is a wrapper that executes a docker-compose command
[EDIT]
$ type -a drush
drush ha "drush --strict=0" come alias
drush è /usr/local/bin/drush
$ cat /usr/local/bin/drush
#!/bin/bash
cd $PWD
docker-compose -p example exec --user 82 php drush $#
Aliases don't get expanded in scripts. If you want the script to include --strict=0 in the command line, you have to say so explicitly in the script.
As mentioned here:
$ type -a drush
drush ha "drush --strict=0" come alias
drush è /usr/local/bin/drush
the drush command is within your PATH environmental variable.
Now please make sure that /usr/local/bin folder is part of your `PATH variable, e.g. by:
$ tr : "\n" <<<$PATH | grep usr.local.bin
/usr/local/bin
This question already has answers here:
sudo echo "something" >> /etc/privilegedFile doesn't work [duplicate]
(15 answers)
Closed 5 years ago.
I'm trying to create and fill a file in a directory that requires sudo permission.
EDIT:
It seems that based on tested the suggested similar post.
sudo su -c "echo 'put in file' > file_name"
echo "Some text" | sudo tee /etc/file
will both create a file within the directory that requires sudo permission
The question has been edited to include a log of the problem being introduced, which includes the command:
# Taken from edited question
$ echo 'hello' > sudo tee /data/hello
In bash (which allows redirection operators at any point in a simple command), this is precisely equivalent to running:
# From question, with redirection moved to end to make actual behavior more readable
echo 'hello' tee /data/hello > sudo
That is, it's creating a file named sudo in your current directory, not using sudo to run tee (in the above, tee is just an argument to echo).
What you want, by contrast, is:
# CORRECT: Using a pipe, not a redirection
echo 'hello' | sudo tee /data/hello
with a pipe (|) rather than a >.
Assuming you have the required sudo privileges you could use an editor like vi or nano
sudo vi /etc/file
or
sudo nano /etc/file
If you don't have sudo for those programs you can su to root first and then try:
sudo su -
vi /etc/file
or
nano /etc/file
If you want to do it all on one command line, you can make sure that the file exists by running touch(1) on it. E.g.
sudo touch /etc/file && echo "Some text" | sudo tee /etc/file
This question is similar to this one: https://serverfault.com/questions/342697/prevent-sudo-apt-get-etc-from-swallowing-pasted-input-to-stdin but the answer is not satisfying (appending && to each line of bash script is not elegant) and does not explain why some users can paste/execute multiple subsequent apt-get install -y commands and others can't because stdout is swollen by the next command.
I have a script my_script.sh:
sudo apt-get install -y graphicsmagick
sudo apt-get install -y libgraphicsmagick++1-dev
...
It can have only two lines or more of sudo apt-get install stuff. The libraries (graphicsmagick, etc.) doesn't matter, it can be any library.
When I copy this script and paste it's contents to bash or just execute it like this:
cat my_script.sh | sudo -i bash
then for some reason only the first line (graphicsmagick) gets executed and the rest is just printed to the console. It happens only with sudo apt-get install -y, other scripts, which doesn't contain this command behave normally.
If I change bash to sh (which is dash) I get expected behaviour:
cat my_script.sh | sudo -i sh
Can you explain why this happens?
When answering, can you please avoid this questions/comments:
Why are you doing it this way?
Piping to your bash is not safe
Some other aspects are not safe or hackish
I just want to know why bash doesn't work as I would expect and sh does.
PS. I'm using Ubuntu 14.04, sh is dash as you can see here:
vagrant#vagrant-ubuntu-trusty-64:/tmp$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Feb 19 2014 /bin/sh -> dash
Bash and dash simply behave different when using -i flag.
Bash always goes to interactive mode even when stdin is not a terminal.
Dash on the other hand will not go into interactive mode, even with -i flag.
Probably need the -s option
If the -s option is present, or if no arguments remain after option
processing, then commands are read from the standard input. This option allows
the positional parameters to be set when invoking an interactive shell.
Bash man page
curl -s http://foo.com/bar.sh | sudo -i bash -s
Example
I want to run 2 different scripts from a single master shell script.
The first one uses the following command "rosh -n -l abcd" (It will log me in to the server with the user abcd and on the same shell I need to run the other script#2 and script#3 ...etc.)
Script#2- From there I need to change user using su - xyz and provide a password (it is fine if I can hardcode this in the file) (Script name is logintoServer)
Script#3- Run some script in the same shell to verify start of stop of server...
I have done the following but failed
I have one script which has rosh -n <servername> -l abcd /bin/sh -c "su - xyz" (I have to run this command in the same shell)
The below are the errors:
I am getting error while executing "standard in must be a tty"
I have tried to create 2 different scripts and run, but the problem is once the first script is run it does not run the 2nd script till I exit the script. (I need to run the 2nd script from the sub-shell created by the 1st script....)
I don't have rosh and I don't have a man page for rosh but a similar problem exists with ssh:
ssh localhost /bin/bash -c 'echo x' # (prints nothing)
ssh localhost "/bin/bash -c 'echo x'" # x
ssh localhost "/bin/bash -c 'tty'" # not a tty
ssh -t localhost "/bin/bash -c 'tty'" # /dev/pts/12\nConnection to localhost closed.
ssh localhost "/bin/bash -c 'su - $USER'" # su: must be run from a terminal
ssh -t localhost "/bin/bash -c 'su - $USER'"
the last asked for a password and then gave me a shell, so that would be 2 of 3 steps.
so one idea is to see if rosh has the -t option, too and the other is to enclose /bin/bash... with quotes, too (will require some escaping for the 3rd level).
What does rosh say with equivalent commands?
UPDATE
latest state:
rosh -n $host -l abcd -t "/bin/sh -c 'su - $user'"
Next I would save one step by saying /bin/su - xyz instead /bin/sh -c 'su - xyz', then you can use single quotes later, e.g.
rosh -n $host -l abcd -t "/bin/su - $user -c 'echo $PATH'"
this should print $PATH as seen by the echo command. Apparently it doesn't contain java. try man su, which java, man which.
su ... -c cmd runs cmd with the shell specified in /etc/passwd, so say </etc/passwd grep $user on the remote machine to find out which shell is used. if it's bash you can change $PATH in .bashrc or so, for other shells I don't know exactly.
Or specify an absolute path when launching java.
regarding password: with ssh I managed to use private key / public key and ssh-agent. For rosh I don't know if that works, too.