Powershell how to escape regex pipe in npx parameter - windows

I'm trying to execute npx command containing pipe in regex parameter:
npx depcruise --exclude 'node_modules"|"test' --output-type dot src
But I'm getting error:
'test' is not recognized as an internal or external command, operable program or batch file.
I tried all combinations described here https://octopus.com/blog/powershell-pipe-escaping but none works. Whats is the correct way to escape pipe in such command?

Related

How to run sh file in GIT Bash

Am trying to run physusr.sh file in GIT Bash in Windows. Am trying to set java home as below in physusr.sh file.
JAVA_HOME=C:/Program Files (x86)/IBM/WebSphere/AppServer
JAVA_EXE=$JAVA_HOME/bin/java
cd /H/US_L3/MLAdminBatchLocal/original
but am facing the error when I run the file GIT Bash
./physusr.sh: line 1: syntax error near unexpected token `('
./physusr.sh: line 1: `JAVA_HOME=C:/Program Files (x86)/IBM/WebSphere/AppServer'
I have tried using double quotes, back slash but I was getting no such file or directory error. How do I make this work. Should I run this sh file using any other tool.
Most probably the '(' bracket character of '(x86)' is causing the problem. When it executes the bash file it is maybe considering it as something else but not the path. So, to solve this, tell the executor that the whole thing is a path or we can say disable the different treatment of special characters like brackets, put the path inside single quotes.
So, change it to:
JAVA_HOME='C:/Program Files (x86)/IBM/WebSphere/AppServer'
JAVA_EXE=$JAVA_HOME/bin/java
cd /H/US_L3/MLAdminBatchLocal/original

How to escape single quotes in a bash script run from Jenkins?

I am trying to escape single quotes in parameters that are passed to a Bash script which is run on Jenkins. I am wondering if there is a way for me to escape these single quotes through Jenkins, or do I need to change the code to escape them.
I have already tried passing parameters like you're or we've, by doing this: you\'re as well as you\'\re but the Jenkins job automatically fails, but youre and weve works just fine. I can try running this manually in the command line and escaping the apostrophes there, however, I would like this process to be simplified.
"sed -ie 's/lms.facebook.keywords=.*\$/lms.facebook.keywords=${KEYWORDS}/g'
The parameters being passed to Jenkins become populated in: ${KEYWORDS}.
When trying to pass a parameter that has an apostrophe, the job automatically fails and returns this:
bash: -c: line 8: unexpected EOF while looking for matching `''
bash: -c: line 11: syntax error: unexpected end of file
Build step 'Execute shell' marked build as failure
Finished: FAILURE
When you are passing the Jenkin's parameter you're
use double quotes for that variable
like
"$apostrophes"
String concatenation in shell script works with double quotes.
"sed -ie 's/lms.facebook.keywords=.*\$/lms.facebook.keywords="${KEYWORDS}"/g'"

How to escape pipe character from PowerShell command line to pass into non-PowerShell command

I'm in a PowerShell console session, trying to run a Gradle script that exists in the cwd from the PowerShell command line.
The Gradle command accepts an argument which includes quotes that can contain one or more pipe characters, and I can't for the life of me figure out how to get PowerShell to escape it using just one line (or any number of lines, actually - but I'm not interested in multi-line solutions).
Here's the command:
./gradlew theTask -PmyProperty="thisThing|thatThing|theOtherThing"
...which produces this error:
'thatThing' is not recognized as an internal or external command, operable program or batch file.
I've tried all of these variants, none of which work. Any idea?
./gradlew theTask -PmyProperty="thisThing`|thatThing`|theOtherThing"
./gradlew theTask -PmyProperty=#"thisThing|thatThing|theOtherThing"
./gradlew theTask -PmyProperty="thisThing\|thatThing\|theOtherThing"
./gradlew theTask -PmyProperty="thisThing\\|thatThing\\|theOtherThing"
./gradlew theTask -PmyProperty="thisThing^|thatThing^|theOtherThing"
Well, I have found that:
First call your script like this as I first suggested:
./gradlew theTask -PmyProperty="thisThing^|thatThing^|theOtherThing"
Then modify your gradlew.bat script by adding quotes:
set CMD_LINE_ARGS="%*"
The problem is: now CMD_LINE_ARGS must be called within quotes or the same error will occur.
So I assume that your command line arguments cannot be something else and I'm handling each parameter one by one
rem now remove the quotes or there will be too much quotes
set ARG1="%1"
rem protect or the pipe situation will occur
set ARG1=%ARG1:""=%
set ARG2="%2"
set ARG2=%ARG2:""=%
set ARG3="%3"
set ARG3=%ARG3:""=%
echo %ARG1% %ARG2% %ARG3%
The output is (for my mockup command):
theTask -PmyProperty "thisThing|thatThing|theOtherThing"
The "=" has gone, because it has separated parameters from PowerShell. I suppose this won't be an issue if your command has standard argument parsing.
Add as many arguments as you want, but limit it to 9 anyway.
Found another possibility here. One can use single quotes and double quotes like this. Not sure if that helps to solve the original problem but it helped to solve mine.
'"thisThing|thatThing|theOtherThing"'

using wget to get excel file

I'm sorry I do not speak English very well.
I want to use wget to get an excel file with a link :
http://app/sip/rkn/export.php?p1=1&p2=613&p3=01&p4=31&p5=01&p6=2013
but I'm getting an error message :
'p2' is not recognized as an internal or external command,
operable program or batch file.
'p3' is not recognized as an internal or external command,
operable program or batch file.
'p4' is not recognized as an internal or external command,
operable program or batch file.
'p5' is not recognized as an internal or external command,
operable program or batch file.
'p6' is not recognized as an internal or external command,
operable program or batch file.
What can I do to solve this issue? Thanks in advance!
The problem is that the & character is interpreted by the shell, for which it has a special meaning (see here).
Try the following commands in your shell to understand what it does:
$ sleep 2 && echo "2 seconds have passed"
$ sleep 2 && echo "2 seconds have passed" &
Simplified, the syntax runs the specified command before the & in the background, which means the string that comes after the & is interpreted as a new command by the shell. Try this example to understand what happens:
$ wget foo&ls
To overcome the problem, you need to enclose your link in quotes. Double quotes ("") might not be sufficient, because a string enclosed in double quotes is subject to e.g. parameter expansion, so if your link includes something that looks like a shell variable (e.g. $FOO) your link will be mangled.
Enclose it in single quotes instead to make sure wget sees the link "as it is":
$ wget 'http://app/sip/rkn/export.php?p1=1&p2=613&p3=01&p4=31&p5=01&p6=2013'
Your link is broken, if it is a fake link (used in order to do an example), you can try quoting the URL like this:
wget "http://www.example.com/export.php?p1=1&p2=613&p3=01&p4=31&p5=01&p6=2013"

how to pass a parameter containing & to a bat file from cygwin bash

Here's a simple bat file:
C:\temp>type a.bat
#echo off
rem try
echo %1
With some difficulty, I am able to pass a&b as parameter to it:
C:\temp>a.bat a&b
a
'b' is not recognized as an internal or external command,
operable program or batch file.
C:\temp>a.bat "a&b"
"a&b"
The paramater has the " character; I can live with it. But I can't figure out how to call it from a cygwin shell:
C:\temp>c:\cygwin\bin\sh
sh-4.1$ ./a.bat a&b
[1] 7760
a
sh: b: command not found
sh-4.1$ ./a.bat a\&b
a
'b' is not recognized as an internal or external command,
operable program or batch file.
[1]+ Done ./a.bat a
sh-4.1$ ./a.bat \"a\&b\"
"\"a
'b\""' is not recognized as an internal or external command,
operable program or batch file.
sh-4.1$ ./a.bat "a\&b"
a\
'b' is not recognized as an internal or external command,
operable program or batch file.
Windows CMD uses ^ to escape most special characters. So you can use that to pass your argument without enclosing quotes.
C:\temp>a.bat a^&b
But the parameter that is received will be a&b. Your batch script will give an error when it attempts to ECHO the value of %1 because the & is not quoted or escaped.
You could safely echo the value if you enclose it in quotes:
echo "%1"
But if you pass the value already enclosed in quotes "a&b", then you get an error again. That is why many batch script use the ~ modifier to remove any existing enclosing quotes (if they exist), and then explicitly add quotes. The following will work if the value of %1 is quoted or unquoted.
echo "%~1"
You still can have problems with something like "a&b"&c, but that is another story :-)
Another option would be to double escape the & in the original command line:
C:\temp>a.bat a^^^&b
Your batch script will recieve a^&b, and then the echo will work.
With regard to Cygwin, I know very little, but I believe I mostly understand tests 1, 2 and 3.
Test 1:
sh-4.1$ ./a.bat a&b
[1] 7760
a
sh: b: command not found
Cygwin is passing a to your batch script and failing to execute command b
Test 2:
sh-4.1$ ./a.bat a\&b
a
'b' is not recognized as an internal or external command, operable program or batch file.
I'm not sure if CMD parses the command line passed from Cygwin before the batch script is executed or not.
If so, then a is getting passed to your batch script, and then CMD.EXE is failing to execute b.
If not, then Cygwin is successfully executing your script and passing a&b, but your ECHO statement is failing as I explained earlier.
One of the following should work with your script, but I'm not sure which:
sh-4.1$ ./a.bat a^\&b
or
sh-4.1$ ./a.bat a^^^\&b
One of the above will pass a^&b and your ECHO should work.
Test 3:
sh-4.1$ ./a.bat \"a\&b\"
"\"a
'b\""' is not recognized as an internal or external command, operable program or batch file.
I have no idea what Cygwin is doing. Somehow it is introducing additional double quotes.
Test 4:
sh-4.1$ ./a.bat "a\&b"
a\
'b' is not recognized as an internal or external command, operable program or batch file.
Cygwin strips the quotes, and the backslash is preserved. Again, either the & is causing problems when CMD.EXE is launching the script, or it is causing problems within the script when you ECHO it.
One of the following should work, passing a^&b to your script. I'm just not sure which one:
sh-4.1$ ./a.bat "a^&b"
or
sh-4.1$ ./a.bat "a^^^&b"
I believe the following will successfully pass "a&b" to your script:
sh-4.1$ ./a.bat '"a&b"'
I also think the following will do the same, though I am not as confident due to the result of Test 3.
sh-4.1$ ./a.bat "\"a&b\""

Resources