Why am I getting a syntax error when trying to use -v flag for variables in psql command? - zsh

I'm trying to use the following psql command in a zsh shell to send a query to a database:
psql -Atx $mydburl -v myvar=1 -c "SELECT :myvar"
And I'm getting the following error:
ERROR: syntax error at or near ":"
LINE 1: SELECT :myvar
^
However, when I use a pipe, this works just fine:
echo "SELECT :myvar" | psql -Atx $mydburl -v myvar=1
And it produces the expected result of:
?column?|1
What is going on here?

That's as documented:
-c command
[...]
command must be either a command string that is completely parsable by the server (i.e., it contains no psql-specific features), or a single backslash command.

Related

sql select statement over adb

I would like to have a batch file that runs a select statement over adb and returns the found rows.
This is the command that I use in my batch file:
adb shell "su -c sqlite3 /data/user_de/0/com.android.providers.telephony/databases/mmssms.db 'select * from sms;'"
The problem I have is, that my command produces an error:
Error: incomplete input
What I don’t understand is that the fallowing command works just fine:
adb shell "su -c sqlite3 /data/user_de/0/com.android.providers.telephony/databases/mmssms.db '.tables'"
This returns the tables from the database, as expected.
My guess is, that I need to escape some characters, the “;” maybe, but escaping it with \ or with ^ both did not work.
Any help would be greatly appreciated.
I guess you are using powershell, then escape the ;:
adb shell "su -c sqlite3 /data/user_de/0/com.android.providers.telephony/databases/mmssms.db 'select * from sms`;'"

Unexpected "\n" while trying to exec command in paramiko

Okay, so I tried to remove files on remote machine with paramiko. But I'm stuck on this error:
"bash: -c: line 1: syntax error near unexpected token `('\n",
"bash: -c: line 1: `rm -rf /home/server/audio(23).mp3'\n".
Code:
self.ssh = paramiko.SSHClient()
self.ssh.load_system_host_keys()
# here is code with establishing ssh connection
# ...
# connection established
path = '/home/server/audio(23).mp3'
com = f"rm {path}"
stdin, stdout, stderr = self.ssh.exec_command(com)
print(stderr.readlines()) # here I see the error
I tried to check is com variable has such substring "\n" but got false as answer so it's not my fault I guess:
print("\n" in com) # returns false each time
I've reformatted your error message a little bit, and the problem should be clearer now:
"bash: -c: line 1: syntax error near unexpected token `('\n",
"bash: -c: line 1: `rm -rf /home/server/audio(23).mp3'\n".
As you can see, the \n doesn't have anything to do with the error message itself, it's simply just part of the output of the command as returned by paramiko. It's telling you that the unexpected token is (, not \n. When you interpret the error message as an array of python strings, this gets even clearer:
bash: -c: line 1: syntax error near unexpected token `('
bash: -c: line 1: `rm -rf /home/server/audio(23).mp3'
The correct fix is to use the shlex.quote function from the Python standard library to escape your file's path before you execute it:
from shlex import quote
path = '/home/server/audio(23).mp3'
com = f"rm {quote(path)}"
stdin, stdout, stderr = self.ssh.exec_command(com)
From the docs:
Return a shell-escaped version of the string s. The returned value is a string that can safely be used as one token in a shell command line, for cases where you cannot use a list.
Using shlex.quote should prevent the shell on the other end (bash, in this case) from getting tripped up by any special characters that may be present in the path (like spaces, parentheses, quotation marks—you name it). Note that shlex only works on Unix shells. If you want to use paramiko to connect to a Windows server over SSH, you'll need a different library. This depends on the server that you're connecting to: it doesn't matter where you run the Python code, it only matters where self.ssh ends up pointing to.

Typeset: -A: invalid option error when trying to create an associate array in ZSH

I'm trying to run a zsh script on version zsh 5.7.1 (x86_64-apple-darwin19.0) using ./filename
typeset -A animals
animals=( ["moo"]="cow" ["woof"]="dog")
echo ${animals[moo]}
But I keep getting the error that typeset: -A: invalid option It also throws the same error with declare -A animals. It works fine with a lowercase -a but thats not an associate array.

Execute multiple unix commands from SAS

I am trying to delete a file from a SAS program. The Unix commands I want to run are:
unalias rm;
rm -f &file..txt;
How do I do this? I tried using the 'x' statement, but can't get it to work. I need the commands to run in the same shell for unalias to work AND I need the macro variable to resolve.
You can use semi-colon (;) to separate multiple commands in one command string.
x "unalias rm; rm -f &file..txt";
In general I just use a leading backslash to force Unix to NOT use the alias for command.
x "\rm -f &file..txt";

Errror string catch: redundancy with grep command

Currently I am using the following command to catch the Error String in the MY_FILE_NAME*.log
Currentdate=`date -u +"%Y/%m/%d"`
YEST=`TZ=XYZ+24 date '+%Y/%m/%d'`
grep -E "$Currentdate|$YEST" MY_FILE_NAME*.log | grep "Type: Error"
This command is generating huge data with the string "Type: Error" with redundancy in the same error type (in my case the same error is displayed like 100 times)
I want the error strings of same type to be displayed only once
If using GNU/Linux try the '-m' switch
grep -m 1 -E "$Currentdate|$YEST" MY_FILE_NAME*.log | grep "Type: Error"
In the GNU version of grep, the '-m ' switch stops reading the input file after matches are found. This feature does not exist in the older Unix grep on which AIX and similar are built.
If on AIX where there is no -m or -B see this StackOvreflow post

Resources