Message "syntax error near unexpected token `('" - bash

I am trying to execute
sudo -su db2inst1 /opt/ibm/db2/V9.7/bin/db2 force application (1995)
but I get this error:
bash: syntax error near unexpected token `('
However,
sudo -su db2inst1 id
gives me correct output. So it must be something about the ()
If I try
sudo -su db2inst1 /opt/ibm/db2/V9.7/bin/db2 force application \(1995\)
I get
/bin/bash: -c: line 0: syntax error near unexpected token (' \ /bin/bash: -c: line 0: /opt/ibm/db2/V9.7/bin/db2 force application (1995)'
Running /opt/ibm/db2/V9.7/bin/db2 force application (1995) as db2inst1 user gives me the same error, but running
/opt/ibm/db2/V9.7/bin/db2 "force application (1995)"
works fine
The right syntax is
sudo -su db2inst1 '/opt/ibm/db2/V9.7/bin/db2 "force application (1995)"'

NOTE: While this answer seems to have been correct at the time [sudo was changed later that same year to add extra escaping around characters in the arguments with -i and -s], it is not correct for modern versions of sudo, which escape all special characters when constructing the command line to be passed to $SHELL -c. Always be careful and make sure you know what passing a command to your particular version of sudo will do, and consider carefully whether the -s option is really needed for your command and/or, if it would, if you'd be better served with sudo sh -c.
Since you've got both the shell that you're typing into and the shell that sudo -s runs, you need to quote or escape twice. Any of the following three would have worked with this now-ancient version of sudo:
sudo -su db2inst1 '/opt/ibm/db2/V9.7/bin/db2 "force application (1995)"'
sudo -su db2inst1 '/opt/ibm/db2/V9.7/bin/db2 force\ application\ \(1995\)'
sudo -su db2inst1 /opt/ibm/db2/V9.7/bin/db2 force\\ application\\ \\\(1995\\\)
Out of curiosity, why do you need -s? Can't you just do the following?
sudo -u db2inst1 /opt/ibm/db2/V9.7/bin/db2 'force application (1995)'
sudo -u db2inst1 /opt/ibm/db2/V9.7/bin/db2 force\ application\ \(1995\)

Try
sudo -su db2inst1 /opt/ibm/db2/V9.7/bin/db2 force application \(1995\)

Related

Bash won’t recognize existence of closing single quote in systemd unit file

I have the following systemd unit file set to automatically update all Arch Linux and AUR packages at the same time (using the yay AUR helper, of course) while also attempting to temporarily add (and then delete after it’s done, for obvious reasons) a sudoers.d entry to briefly give nobody sudo access to pacman in order to get AUR packages updated:
[Unit]
Description=Automatic Update
After=network-online.target
[Service]
Type=simple
SyslogIdentifier=autoupdate
ExecStartPre=/bin/bash -c 'echo \'nobody ALL= NOPASSWD: /usr/bin/pacman\' > /etc/sudoers.d/autoupdate'
ExecStart=/bin/bash -c \”XDG_CACHE_HOME=/var/tmp PWD=/var/tmp sudo -E -u nobody yay -Syuq --noconfirm --devel --timeupdate\”
ExecStartPost=/usr/bin/rm -f /etc/sudoers.d/autoupdate
KillMode=process
KillSignal=SIGINT
[Install]
WantedBy=multi-user.target
The problem is that bash fails to acknowledge the existence of the closing single quote on the ExecStartPre line:
nobody: -c: line 1: unexpected EOF while looking for matching `’`
nobody: -c: line 2: syntax error: unexpected end of file
This is of course despite the fact that manually typing sudo bash -c ‘echo nobody ALL\=NOPASSWD: /usr/bin/pacman > /etc/sudoers.d/autoupdate’ into my shell succeeds without incident.
What could be causing this discrepancy?
Turns out the overcomplication of the issue was rooted in the use of ExecStartPost= instead of ExecStopPost=. Once I changed the former to the latter, the original version of the unit file from long before this was posted (which was far simpler) worked perfectly.
regardless of why you want to use sudo even though you are root..
and without thinking about your code..
Use a script instead
ExecStartPre=/path/to/your/script prestart
ExecStart=/path/to/your/script start
ExecStartPost=/path/to/your/script poststart
your script
#!/bin/bash
case $1 in
prestart) echo "nobody ALL= NOPASSWD: /usr/bin/pacman" > /etc/sudoers.d/autoupdate;;
start) XDG_CACHE_HOME=/var/tmp
PWD=/var/tmp
sudo -E -u nobody yay -Syuq --noconfirm --devel --timeupdate;;
poststart) rm -f /etc/sudoers.d/autoupdate;;
esac

Errno 13 Permission denied when installing gcloud in macOS Mojave 10.14.2

When trying to install gcloud I am getting the following error [Errno 13] Permission denied.
I tried running the install with sudo, running the manual install and nothing.
The following errors where generated when running:
sudo curl https://sdk.cloud.google.com | bash
sudo curl "$url" | bash will invoke curl with sudo, but the bash, which is in another subshell, is not affected by the previous sudo.
It maybe not necessary to invoke curl with root, but bash seems need root. So just simply move sudo to where needs, for example curl "$url" | sudo bash.

Unix script can't alter postgres hba.conf configuration file on Ubuntu

I'm attempting to setup postgres 9.6 on ubuntu/vagrant through a provisioning script. Part of my script adds a line to pg_hba.conf with the following command:
sudo -u postgres echo "host all all all md5" >> /etc/postgresql/9.6/main/pg_hba.conf
However, this gives me the error -bash: /etc/postgresql/9.6/main/pg_hba.conf: Permission denied
Which is strange because I am allowed to edit the file with either sudo nano or sudo -u postgres nano.
Here are the permissions on the file:
-rw-r----- 1 postgres postgres 4641 Apr 6 16:11 pg_hba.conf
How can I add this line to my configuration file in a script?
The problem here is that redirection happens before command execution. So the redirection doesn't have the elevated privileges you expected it to.
There's more than one way around that problem. I generally use something like this.
echo "host..." | sudo tee -a /etc/postgresql/9.6/main/pg_hba.conf
Piping to sudo tee... avoids problems with quoting.
How bash executes commands
Redirections

Error installing Homebrew: /etc/sudoers: syntax error near line 46

I am trying to install Homebrew (using the code snippet right off their site):
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
I am getting:
>>> /etc/sudoers: syntax error near line 46 <<<
sudo: parse error in /etc/sudoers near line 46
sudo: no valid sudoers sources found, quitting
Not great with terminal commands and lingo. Can anyone help?
Your /etc/sudoers file seems to be borked.
Never open sudoer file with a normal editor. always use visudo
just type
sudo visudo
this will take you to /etc/sudoers and upon saving it will make sure that there is no error in formatting.
if you make an error in sudoer file, you will lose sudo access, so always use visudo

when there is pipe, how to deal with permission with sudo

if the command is with a pipe, how to deal with permission problem with sudo
the following command
sudo -u liy echo "update moz_cookies set value='f1=40000000&f3=40000&fv=11.2.202' where name='PREF' and host='.youtube.com';"|sqlite3 /user/liy/home/.mozilla/firefox/dd1pkryp.default/cookies.sqlite
leads to problem:
cannot open directory /user/liy/home/.mozilla/firefox/: Permission denied
how to deal with this? thanks
Put the sudo on the command doing the modifying:
echo 'some SQL' | sudo sqlite3 /some/database.db
You might even consider using a heredoc:
sudo sqlite3 /some/database.db <<ENDOFSQL
-- SQL here; you can even use multiple lines!
ENDOFSQL

Resources