I'm trying to auth on ngrok:
aaas-iMac:sss aaa$ ~/ngrok -authtoken bbtw942UevV!Ap94JzFz 80
-bash: !Ap94JzFz: event not found
What's with this? How do I auth? Thanks.
Your token has an un-escaped exclamation mark (!) in it, which is being interpreted by bash as a command history expansion.
You need to escape it by enclosing the authtoken in single quotes:
$ ~/ngrok -authtoken 'bbtw942UevV!Ap94JzFz' 80
Alternatively, escape it with a slash:
$ ~/ngrok -authtoken bbtw942UevV\!Ap94JzFz 80
Related
I am following https://docs.docker.com/get-started/06_bind_mounts/#start-a-dev-mode-container on a Windows PC and am stuck here:
Run the following command. We’ll explain what’s going on afterwards:
docker run -dp 3000:3000 \
-w /app -v "$(pwd):/app" \
node:12-alpine \
sh -c "yarn install && yarn run dev"
If you are using PowerShell then use this command:
docker run -dp 3000:3000 `
-w /app -v "$(pwd):/app" `
node:12-alpine `
sh -c "yarn install && yarn run dev"
When using Command Prompt, I get errors (tried multiple variations as shown below), and when using PowerShell, I don't appear to get errors but am not running anything as showed when executing docker ps.
Note that I would rather use Command Prompt and not PowerShell as I could use Linux commands with ComandPrompt on my PC.
What is the significance of backslashes when using Dockers with Command Prompt (and tick marks with PowerShell for that matter)?
I have since found that docker run -dp 3000:3000 -w /app -v "%cd%:/app" node:12-alpine sh -c "yarn install && yarn run dev" works without errors (got rid of backslashes, put on one line, and used %cd% instead of $(pwd)), but would still like to know why using the exact script in the example results in errors.
Using Command Prompt
C:\Users\michael\Documents\Docker\app>docker run -dp 3000:3000 \
docker: invalid reference format.
See 'docker run --help'.
C:\Users\michael\Documents\Docker\app> -w /app -v "$(pwd):/app" \
'-w' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\michael\Documents\Docker\app> node:12-alpine \
The filename, directory name, or volume label syntax is incorrect.
C:\Users\michael\Documents\Docker\app> sh -c "yarn install && yarn run dev"
sh: yarn: command not found
C:\Users\michael\Documents\Docker\app>docker run -dp 3000:3000 \ -w /app -v "$(pwd):/app" \ node:12-alpine \ sh -c "yarn install && yarn run dev"
docker: invalid reference format.
See 'docker run --help'.
C:\Users\michael\Documents\Docker\app>docker run -dp 3000:3000 -w /app -v "$(pwd):/app" node:12-alpine sh -c "yarn install && yarn run dev"
docker: Error response from daemon: create $(pwd): "$(pwd)" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended to pass a host directory, use absolute path.
See 'docker run --help'.
C:\Users\michael\Documents\Docker\app>
Using PowerShell
PS C:\Users\michael\Documents\Docker> docker run -dp 3000:3000 `
>> -w /app -v "$(pwd):/app" `
>> node:12-alpine `
>> sh -c "yarn install && yarn run dev"
849af42e78d4ab09242fdd6c3d03bcf1b6b58de984c4485a441a2e2c88603767
PS C:\Users\michael\Documents\Docker> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
PS C:\Users\michael\Documents\Docker>
would still like to know why using the exact script in the example results in errors.
Because the command with the line-ending \ characters is meant for POSIX-compatible shells such as bash, not for cmd.exe
POSIX-compatible shells (sh, bash, dash, ksh, zsh):
use \ for line-continuation (continuing a command on the following line) and escaping in general.
use $varName to reference both environment and shell-only variables.
support $(...) for embedding the output from a command (...) in command lines (command substitution).
support both double-quoted ("...", interpolating) and single-quoted ('...', verbatim) strings; use '\'' to - in effect - include a ' inside '...'.
(Additionally, in bash, ksh, and zsh, there are the rarely used ANSI C-quoted strings, $'...', and, in bash and ksh, perhaps even more rarely, localizable strings, $"...").
cmd.exe:
uses ^ for line-continuation and escaping in general (in unquoted arguments only).
uses %varName% to reference environment variables (the only variable type supported).
doesn't support command substitutions at all.
supports only "..." strings (interpolating).
PowerShell:
uses ` (the backtick) for line-continuation and escaping in general.
uses $env:varName to reference environment variables, $varName to reference shell-only variables.
supports $(...), called subexpressions, the equivalent of command substitutions (outside of double-quoted strings, (...) is usually sufficient).
supports both double-quoted ("...", interpolating) and single-quoted ('...', verbatim) strings; use '' to embed a ' inside '...'.
Note: A common pitfall is that PowerShell has more metacharacters compared to both POSIX-compatible shells and cmd.exe, notably including # { } , ;, which therefore require individual `-escaping in unquoted arguments or embedding in quoted strings - see this answer.
Potential line-continuation pitfall: in all of the shells discussed, the escape character must be the very last character on the line - not even trailing (intra-line) whitespace is allowed (because the escape character would then apply to it rather than to the newline).
The information above is summarized in the following table:
Feature
POSIX shells _
cmd.exe _
PowerShell _
Line-continuation / escape character
Backslash (\)
Caret (^)
Backtick (`)
Double-quoted strings (interpolating)
✅
✅
✅
Single-quoted strings (verbatim)
✅
❌
✅
Get / set environment variables
$varName /export varName=...
%varName% /set varName=...
$env:varName /$env:varName = ...
Get / set shell-only variables
$varName/varName=...
❌ (no such variables exist, but you can limit the scope of env. vars. with setlocal)
$varName/$varName = ...
Command substitutions, subexpressions
$(...)
❌
(...) / $(...), esp. in strings
Note re setting variables with respect to whitespace on either side of the = symbol:
In POSIX-like shells, there must not be whitespace around =.
In cmd.exe, such whitespace is significant and becomes part of the variable / value name, and is therefore usually to be avoided.
In PowerShell, such whitespace is optional - you may use it to enhance readability; any string value to be assigned requires quoting (e.g., $var = 'hi!')
See also:
https://hyperpolyglot.org/shell for a much more comprehensive juxtaposition of these shells, though note that - as of this writing - the information about PowerShell is incomplete.
Sage Pourpre's helpful answer for links to the line-continuation documentation of the respective shells.
This is character escaping.
The X Character (\ for Bash, backtick for Powershell and ^ for Windows terminal )are used to remove any specific meanings to the next characters.
When used at the end of a line, this mean that the next character (The newline character) is completely ignored.
This keep the command essentially a one-line command from the point of view of the interpreter, but allow you to break it on multiple lines for better readability.
References
Powershell - About special characters
Escape sequences begin with the backtick character [`], known as the grave
accent (ASCII 96), and are case-sensitive. The backtick character can
also be referred to as the escape character.
Bash manual
3.1.2.1 Escape Character
A non-quoted backslash \ is the Bash escape character. It preserves the literal value of the next character that
follows, with the exception of newline. If a \newline pair appears,
and the backslash itself is not quoted, the \newline is treated as a
line continuation (that is, it is removed from the input stream and
effectively ignored).
How-to: Escape Characters, Delimiters and Quotes at the Windows command line
Escaping CR/LF line endings. The ^ escape character can be used to
make long commands more readable by splitting them into multiple lines
and escaping the Carriage Return + Line Feed (CR/LF) at the end of a
line:
ROBOCOPY \\FileServ1\e$\users ^ \\FileServ2\e$\BackupUsers ^ /COPYALL /B /SEC /MIR ^ /R:0 /W:0 /LOG:MyLogfile.txt /NFL /NDL
[...]
A couple of things to be aware of:
A stray space at the end of a line (after the ^) will break the
command, this can be hard to spot unless you have a text editor that
displays spaces and tab characters. If you want comment something out
with REM, then EVERY line needs to be prefixed with REM. Alternatively
if you use a double colon :: as a REM comment, that will still parse
the caret at the end of a line, so in the example above changing the
first line to :: ROBOCOPY… will comment out the whole multi-line
command.
My problem is that I have this command:
sed -i -E 's/((^|\s)PATH=)([^\$]*)$/\1${PATH:-\3}/g' /foo
and as far as I use it with single quotes it works fine but now I need to use it with double-quotes (because Dockerfile doesn't like single ones) and when I try to translate it to:
"s/((^|\s)PATH=)([^\$]*)$/\1${PATH:-\3}/g"
I get this error: unknown option to `s'. So I read that I can try to replace the separators and I tried with #:
"s#((^|\s)PATH=)([^\$]*)$#\1${PATH:-\3}#g"
But with that I get: unterminated `s' command. I also tried to replace with ;:
"s;((^|\s)PATH=)([^\$]*)$;\1${PATH:-\3};g"
and that "succeeded" but the end result was different to what I expected. Any ideas on how can I translate this properly to use it with doubled-quotes?
Don't! You can easily translate it to a string with backslashes and no quotes:
$ printf "%q\n" 's/((^|\s)PATH=)([^\$]*)$/\1${PATH:-\3}/g'
s/\(\(\^\|\\s\)PATH=\)\(\[\^\\\$\]\*\)\$/\\1\$\{PATH:-\\3\}/g
$ sed -E s/\(\(\^\|\\s\)PATH=\)\(\[\^\\\$\]\*\)\$/\\1\$\{PATH:-\\3\}/g
As mentioned by #glenn jackman escaping backslashes and dollar signs does the work, this worked for me:
"s/((^|\\s)PATH=)([^\\\$]*)\$/\\1\${PATH:-\\3}/g"
Thanks!
I have a file with multiple lines of
When i use the connection and request TEXT1 using mystring1.service
When i use the connection and request TEXT2 using mystring2.service
When i use the connection and request TEXT3 using mystring3.service
and I would like to replace it with
When i send a systemctl command through ssh and request TEXT1 using mystring1.service
When i send a systemctl command through ssh and request TEXT2 using mystring2.service
When i send a systemctl command through ssh and request TEXT3 using mystring3.service
where i pass in the 2 words after request and using into the new string.
I am trying to use sed and replace them with groups, it is however not working:
sed "s/When i use the connection and request ([\S]+) using ([\S]+)/When I send a systemctl command through ssh and request \1 using \2/g" input.txt > output.txt
I would do the minimal thing first (as suggested by #NateT):
sed 's/use the connection/send a systemctl command through ssh/' input.txt > output.txt
There are a couple of problems with your sed command. First, you need to use the -r option, or you need to escape the parenthesis and the + characters. Also, if you're going to use the \S token with sed, you don't need to square brackets. Here is a command equivalent to the one that you presented, but without the errors:
sed -r 's/When i use the connection and request (\S+) using (\S+)/When i send a systemctl command through ssh and request \1 using \2/' input.txt > output.txt
Or escaping the characters without the -r option:
sed 's/When i use the connection and request \(\S\+\) using \(\S\+\)/When i send a systemctl command through ssh and request \1 using \2/' input.txt > output.txt
I have a CFN with cfn-init to deploy a Apache web server with specified virtual hosts. In the template, I use a AWS::CloudFormation::Init configset to replace local IPs with the Instance's private IP.
config:
packages:...
files:...
services:...
commands:
replacePrivateIP:
cwd: "/etc/httpd/conf"
command: !Sub |
sed -i 's#127.0.0.1#$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4)#g' httpd.conf
The sed command works fine outside the CFN template. But in the CFN-init process, it simply replace "127.0.0.1" with the whole $(curl -s http://...) string.
How can I feed the instance private IP correctly into the httpd.conf file through cfn-init?
The command-substitution syntax $(..) does not work when wrapped in single quotes which is as expected in bash or most other shells as they preserve the literal value present inside. For your substitution to happen, put it inside double-quotes as
sed -i 's#127.0.0.1#'"$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4)"'#g' httpd.conf
Compare the outputs of echo '$(date)' and echo "$(date)" for a simple example of your case.
I'm new to Wget. Following online examples, I am trying to log in to a simple page using the following command:
wget --post-data='entry=85482564&submit3=LOGIN' \ --save-cookies=my-cookies.txt --keep-session-cookies \ https://www.abczyx.com
I get the following error:
SYSTEM_WGETRC = c:/progra~1/wget/etc/wgetrc
syswgetrc = C:\Program Files (x86)\GnuWin32/etc/wgetrc
wget: missing URL
Usage: wget [OPTION]... [URL]...
Try `wget --help' for more options.
'submit3' is not recognized as an internal or external command, operable program or batch file.
I'm guessing that it doesn't quite recognize the &, but I am not sure how to fix it. I'm running Windows 7 cmd line. A side question, why use "\"? I see some examples with it, and some without it. I get issues with it.
After doing some reading, I found that because it is MS DOS, they do not interpret the special characters correctly. Adding quotes around it ("&") did the trick.
In Windows the escape sign is the caret, ^, not backslash, \. So in the batch file it should look like 'entry=85482564^&submit3=LOGIN'.
For me what worked was change & to %26
as in
--post-data 'login=foo%26pass=bar'
also if you are posting an email addrress be sure to change the # to %40
Other codes:
https://en.wikipedia.org/wiki/Percent-encoding
Yes, there is a mistake(I'd say a very serious mistake) in wget's manual. In the manual it says:
Log in to the server.
This can be done only once. wget --save-cookies cookies.txt
--post-data 'user=foo&password=bar'
http://example.com/auth.php
So you do something like
wget --save-cookies cookies.txt \
--post-data 'user=yourUser12%23125&password=yourPassword12%241' \
http://www.websitelink.com/
Which ovbiously doesn't work for multiple reasons. First, you have to remove the \ symbols because they get in the way, second, you have to remove line breaks themselves because when you paste it in your command line tool, it will execute them just as if you pressed enter after each of the lines, which will result in trying to execute that command as 3 separate commands:
First:
wget --save-cookies cookies.txt \
Second:
--post-data 'user=yourUser12%23125&password=yourPassword12%241' \
Third:
http://www.websitelink.com/
Ok, so you remove the slashes and then realize that you have to also remove line breaks by yourself, but it still doesn't work. At this point it's pepehands in the air. So what do you do now? Somehow you have to automagically realize that the & symbol should be also percent-encoded. So you turn
Log in to the server.
This can be done only once. wget --save-cookies cookies.txt
--post-data 'user=foo&password=bar'
http://example.com/auth.php
To this:
wget --save-cookies cookies.txt --post-data 'user=yourUser12%23125%26password=yourPassword12%241' http://www.websitelink.com/
And it starts working!