I'm trying to substitute a variable with double backslash using shell script task, my environment variable is configured to use double backslash.
When is substituted in bash script, the TFS puts only one backslash.
Variable:
Substitution in script, I'm using double quotes because of white spaces in connection string:
Log of execution:
I tried everything, put more backslashs, one quote around, but nothing works.
I appreciate a help.
Thanks
You need type \\\\\\\\ in your variable:
CONNECTION_STRING: Data Source=192.168.2.227\\\\\\\\COMMERCE
Yes, I solved it!!!
I just put the backslashes inside double quotes
Related
I'm trying to use a ZShell function to replace forward- and backslashes with a hyphen. So that "Hello/Every\Person" becomes "Hello-Every-Person". I can do it for the forward slash using
arg=${arg:gs/\//-}
but when I try the same syntax for the backslash it fails. Running
arg=${arg:gs/\//-}
arg=${arg:gs/\\/-}
produces "Hello-EveryPerson". It strips out the backslash, but doesn't do the replacement. I was expecting the normal rules of escaping characters to apply, so the double backslash would resolve to a character.
Can anyone tell me what I'm missing?
Works in zsh version 5.8 exactly as you expected.
I'm trying to write a bash script that will invoke ansible playbook with extra-vars. Some of this vars is an array of strings with spaces. So i am very confused in how to pass them correctly. I already tried many combinations of quotes and slashes. So I came here for help.
ansible-playbook -i inventory.yml playbook.yml --extra-vars \
'username="${login}" fullname="${username}" password="${password}" groups="['Users','Remote Desktop Users']"';
This is what you need to know about bash variables and quoting:
For the following examples, the variable ${text} is set to Hello:
Variables are expanded inside double quotes. e.g. "${text}" => Hello
Variables are not expanded inside single quotes. e.g. '${text}' => ${text}
Single quotes have no special meaning inside double quotes and vice-versa. e.g. "'${text}'" => 'Hello' and '"${text}"' => "${text}"
If you need to place a double-quote inside a double-quoted string, or a single quote inside a single-quoted string, then it must be escaped. e.g. "\"${text}\"" => "Hello" and '\'${text}\'' => '${text}'
With all that said, in your case, you want the variables to be expanded, so you should enclose the entire --extra-vars value in double quotes. According to the Ansible website, the value of each of these extra variables does not need to be quoted, unless it contains spaces. To be safe, you can quote the variables as you might not be able to control their values.
Try this. I have added extra line breaks to make the code easier to understand:
ansible-playbook -i inventory.yml playbook.yml --extra-vars \
"username='${login}' \
fullname='${username}' \
password='${password}' \
groups=['Users','Remote Desktop Users'] \
"
It seems that variable name 'groups' is reserved by ansible.
I changed name, and script starts working.
The answer of Andrew Vickers is also correct.
I am using the touch command to try and create a file with the name "\?$*'KwaMe'*$?\" (quotation marks included as part of the file name). However when I type touch "\?$*'KwaMe'*$?\" in the Terminal, it doesn't give me the result I am expecting. How can I create this file?
You need to escape special characters with the backslash symbol (\).
This command will create a file named "\?$*'KwaMe'*$?\":
touch \"\\\?\$\*\'KwaMe\'\*\$\?\\\"
Explanation
Double your \, like this: \\, so that your shell does not interpret the backslashes from your filename as escape characters.
Escape " and ', like this: \", \', so that your shell interprets the double quotes as part of the filename.
Escape $, like this: \$, otherwise your shell will think you're using a variable.
Escape ? and *, like this: \?, \*, to prevent filename expansion.
I am trying to run an SSH command that will invoke a script on a remote machine that writes some Lua code to a file.
I have this script command that executes under bash:
ssh bob writelua.sh '{version=1,{["foo"]=17}}'
And writelua.sh looks like this:
echo "return $1" > bar.lua
The end result, however, is that bar.lua has the content:
return version=1
I had thought that single quotes prevented all interpretation. How can I edit the scripts and escaping to pass the raw Lua code through unharmed?
The single quotes prevent interpretation on the local host. The remote host sees the command line
writelua.sh {version=1,{["foo"]=17}}
which is subject to brace expansion. You need a second set of quotes so that the first set of single quotes is passed through to the remote host.
ssh bob writelua.sh "'{version=1,{[\"foo\"]=17}}'"
As you can see, the quotes start to get unwieldy. A better solution is to simply copy a script containing
writelua.sh '{version=1,{["foo"]=17}}'
to the remote host and execute that remotely.
An example using the $'...' quotes:
ssh bob writelua.sh $'{version=1,{[\'foo\']=17}}'
Use heredoc and avoid all the excessive quoting:
ssh -T bob << \EOF
writelua.sh '{version=1,{["foo"]=17}}'
EOF
This will send raw script to remote host and it will get interpreted on the remote host itself.
When it gets too complex, particularly with lots of escaping, I prefer generating the command on a temporary script and execute it locally or remotely via SSH as required.
But there's an alternative: using echo to store the command in a variable and taking advantage of three things:
Single quotes don't do variable expansion and allow double quotes, so you can include something like "$myvar" without escaping $ or "
Double quotes allow variable expansion and single quotes, which means you can include something like animals='all'; echo love $animals to have $animals replaced by its value, and without escaping the '
Strings of both types, i.e. enclosed by single quotes or double quotes, can be concatenated simply by putting them together.
As an example, if I want something like this executed on a remote machine:
source /my-env.sh; perl -MMYLIB::DB -e 'my $t=db_list("name", 1553786458); print "#$t"'
But instead of 1553786458 I want to pass the value from a local variable:
now=`date +%s`
We could have this:
get_list=`echo 'source /my-env.sh; perl -MMYLIB::DB -e' "'my " '$t=db_list("name", ' "$now" '); print "#$t"' "'"`
You can see that single and double quotes are alternated, so we din't have to do any escaping! They don't need to be separated by spaces, but it improves readability and won't affect the result in this case.
And now we can execute:
ssh user#host $get_list
There's still no guarantee that this approach will always work, so once you've built your command, the safest bet would be to copy it over in a file.
If you can use Perl...
use Net::OpenSSH;
my $ssh = Net::OpenSSH->new("bob");
$ssh->system('writelua.sh', '{version=1,{["foo"]=17}}')
or die $ssh->error;
Net::OpenSSH takes care of quoting everything for you.
So I found this Stack Overflow question which relates to what I would like to do; however, I am having trouble with the directory having spaces within it. I tried looking at several other Stack Overflow questions, but either I misunderstood them, or none have really addressed my problem. I've seen stuff on passing arguments as arrays and using %1 or something to address the special characters, but nothing has worked yet.
I tried entering the following into cmd.exe:
schtasks /Create /SC DAILY /TN PythonTask /TR "python "C:\Users\me\stuff with spaces \pythonprogram.py""
However, the quotes appear to not be taken in the correct order. I would like the command to be input as python "C:\Users\me\stuff with spaces \pythonprogram.py" to cmd.exe everyday.
How can I use quotes around quotes on the Windows command line?
ANSWER FROM BELOW:
Add a backslash \ before the argument which you are putting in quotes. I.e.:
do_some_command_in_windows_shell_with_this_given_string "run "something.exe""
is replaced with:
do_some_command_in_windows_shell_with_this_given_string "run \"something.exe""
Educated guess:
Escape the inner quotes with a backslash.