Back-ticks within double quotes - terminal

java -jar ngdbc.jar -c "SELECT Name as Title, Desc as Description FROM `table_name` "
I want to add an argument with back-ticks within the query string. However when I execute it in the terminal the back-ticks get executed first and it says zsh: command not found: table_name. Any ideas on how to work around the problem ?

Backticks are subject to expansion in a double-quoted string. Either escape the backticks individually
java -jar ngdbc.jar \
-c "SELECT Name as Title, Desc as Description FROM \`table_name\`"
or use single quotes for the entire argument
java -jar ngdbc.jar \
-c 'SELECT Name as Title, Desc as Description FROM `table_name`'
In shell, backticks are an old form of command substitution. For example,
$ echo "Command substitution: `echo 3`"
Command substitution: 3
This is equivalent to the newer, nestable, more readable form $(...):
$ echo "Command substitution: $(echo 3)"
Command substitution: 3a

try to use this one
java -jar ngdbc.jar -c "SELECT Name as Title, Desc as Description FROM ${table_name}"
In bash or zsh terminal back-ticks are using to run commands.

Related

Escaping shell character in groovy jenkins pipeline

I have some issue to escape quote character in my jenkins pipeline. for example :
I want to append a text in to file through the pipeline using this command below:
openshift.rsh("${podname}", """sh -c 'echo "define( 'WP_HOME', 'http://rabah-test.com' );" >> wp-config.php'""")
and expecting this in to the wp-config.php file:
define( 'WP_HOME', 'http://rabah-test.com' );
but unfortunately i don't have the quotes in my result :
define( WP_HOME, http://rabah-test.com );
I believe it is not an issue with jenkins. Try to run your command in the terminal first.
You have a simple quotes for your sh command and simple quotes inside the define.
I would try something like
sh -c 'echo "define( \'WP_HOME\', \'http://rabah-test.com\' );" >> wp-config.php'
Note, instead of adding the define to the wp-config.php file, I would be tempted to version a default WP_HOME and replace it by a sed
I found how to deal with it. Instead of using simple quotes, I decided to use double quotes and escape with two backslashes.
Like this:
sh -c 'echo "define( \\" WP_HOME \\" , \\" http://rabah-test.com \\" );" >> wp-config.php'
And it now works perfectly.

Escaping dollar sign in sh -c

I am working with an application that runs commands in bash. This is the "template" it is using:
sh -c '<command> "<argument>"'
Please note that I cannot edit the quotes, only thing I can edit is the command and argument. I cannot escape the dollar sign either.
This "template" works fine unless there is a dollar sign in the argument:
sh -c 'echo "x=test$test"'
gives the following output:
x=test
How can I get the exact output, which is:
x=test$test
I could do this if I could switch the quotes:
sh -c "echo '"'x=test$test'"'"
x=test$test
Any way to accomplish this?
If your sh -c command is being run by bash (not by a baseline POSIX shell), and your argument doesn't contain ' literals, then you can do some trickery, as follows:
#!/bin/bash
sh -c 'echo ""'$'\'x=test$test\'""'
That is to say, that the string "'$'\' should be prepended to <argument>, and \'" should be appended.
Note that this is ABSOLUTELY NOT SECURE against shell injection attacks. To make it so, we'd need to modify the inner contents of argument; at bare minimum, replacing \ with \\ and ' with \'.
Even better (particularly from a security perspective) would be to fix the program you're working with to pass arguments out-of-band from code. To do that would mean an argv looking like the following (given below in Python syntax suitable for subprocess.Popen(cmd, shell=False)):
['sh', '-c', 'echo "$1"', '_', 'x=test$test']

Assign a Variable in bash login shell

i am trying to do this from a Windows command prompt.
C:\cygwin64\bin\bash --login -c "$var="<hallo>" &&
echo "$var""
and i get error :
The system cannot find the file specified.
but this works:
C:\cygwin64\bin\bash --login -c
"var="hello" && echo "$hello""
The login shell seems to cause the problem when it gets a '<'. how can i still assign the string with angle brackets to the shell variable?
When you write
C:\cygwin64\bin\bash --login -c "$var="<hallo>" && echo "$var""
You are expecting the shell to strip off the outer quotes from that argument to -c and end up with a string that looks like
$var="<hallo>" && echo "$var"
but that's not what the shell does.
The shell just matches quotes as it goes along. So the shell sees.
["$var="][<hallo>][" && echo "][$var][""].
You need to escape the inner quotes from the current shell or use different quotes to avoid this parsing problem.
C:\cygwin64\bin\bash --login -c 'var="<hallo>" && echo "$var"'
Note also that I removed the $ from the start of the variable name in the assignment and that I used single quotes on the outside so that the current shell didn't expand $var.
With double quotes on the outside you'd need to use something like this instead.
C:\cygwin64\bin\bash --login -c "var='<hallo>' && echo \"\$var\""
For a similar discussion of shell parsing and how things nest (or don't) with backticks you can see my answer here.

"bash -c" doesn't export vars from sourced scripts

I have an inclusion file test.inc:
export XXX=xxx
I use it when call bash to interpret a string:
bash -c ". test.inc; echo $XXX"
But the variable is not set at the point of echo command. If I do 'export' I can see it though:
bash -c ". test.inc; export"
Shows
declare -x XXX="XXX"
How do I make my first command see the exported variables from sourced files when I use bash -c syntax?
You are using double quotes. Therefore your current shell expands $XXX long before the bash -c instance sees it. Switch to single quotes, or escape the dollar sign.

Postgres variable substitution when using \copy

I'm using the psql command \copy and I would like to pass a variable to it from the shell (for table name) like I've done when scripting queries. I've read in the documentation that:
The syntax of the command is similar to that of the SQL COPY command. Note that, because of this, special parsing rules apply to the \copy command. In particular, the variable substitution rules and backslash escapes do not apply.
This seems quite definitive, however I'm wondering if anyone knows of a workaroud?
You could use shell variable substitution with heredoc syntax. Example:
#!/bin/sh
tablename=foo
psql -d test <<EOF
\copy $tablename FROM '/path/to/file'
EOF
You can pass the variable from the shell to psql with -v psql_var="$shell_var" commandline parameter (or access it directly with a shell escape `echo "$shell_var"` after exporting it). Then, you can build the \copy meta command in another meta command (locally with \set or server side with \gset). Example:
#!/bin/sh
tablename=foo
psql -d test -v tbl="$tablename" <<\EOF
\set cmd '\\copy ' :tbl ' FROM ''/path/to/file'''
\echo :cmd
:cmd
EOF

Resources