问题
Creating this question based on the advice of another users advice, it is a follow-up of this.
The scenario is that I am currently trying to create a Powershell script, that a first-time user can run, which will install a bunch of programs and complete several configuration changes. One of the configuration changes is to create a file which has two shell commands (posted below), which will then be executed everytime GitBash is loaded.
The commands I'm trying to input into the .sh file are.. (With " and ' removed)
alias proxyon=source $HOMEPATH/.proxy/proxy-switch.sh on
alias proxyoff=source $HOMEPATH/.proxy/proxy-switch.sh off
I'm trying to do this using the following command
add-content "${env:homepath}\.proxy\TempProxy.bat" "alias proxyon='source "$HOMEPATH/.proxy/proxy-switch.sh on"'"
But, the execution of the ps1 script throws an error, with the top line being
Add-Content : A positional parameter cannot be found that accepts argument '/.proxy/proxy-switch.sh'.
Now, another user told me that if i was going to use double " I would need to escape the inner quotes. The escape char in Powershell from what I can tell is ` , so I have tried the following.
add-content "${env:homepath}\.proxy\TempProxy.bat" "alias proxyon='source `"$HOMEPATH/.proxy/proxy-switch.sh on"'"
Which gave the following
The string starting: At C:\Chef\windowsdevbox-master\test.ps1:2 char:113 + add-content "${env:homepath}.proxy\TempProxy.bat" "alias proxyon='source `"$HOMEPATH/.proxy/proxy-switch.sh on" <<<< '" is missing the terminator: '.
and I also tried
add-content "${env:homepath}\.proxy\TempProxy.bat" "alias proxyon='source `"$HOMEPATH/.proxy/proxy-switch.sh on`"'"
Which doesn't error but, it doesn't add the contents of $HOMEPATH and only adds the following
alias proxyon='source "/.proxy/proxy-switch.sh on"'
Any suggestions on this would be greatly appriciated.
回答1:
I think the issue is that you want the literal $homepath
in your output file in order to let GitBash deal with it. Many ways to try and escape and deal with this but lets work with you last one you made that was almost ready.
add-content "${env:homepath}\.proxy\TempProxy.bat" "alias proxyon='source `"`$HOMEPATH/.proxy/proxy-switch.sh on`"'"
The only thing that is different here is the backtick in front of $HOMEPATH
. Since PowerShell uses the $ to declare and reference variables you need to escape it, like you have done with your quotes, to prevent PowerShell from interpeting it.
Since you do not have a variable called $HOMEPATH
it is created at that time with the value of null. That is why your output from your last example looked the way it did.
Use the format operator
Another option that might have been easier on the eyes is to use the format operator. If you wanted to store it in a variable temporarily you could have this.
$content = "alias proxyon='{0}'" -f 'source "$HOMEPATH/.proxy/proxy-switch.sh on"'
add-content "${env:homepath}\.proxy\TempProxy.bat" $content
or inline it would be
add-content "${env:homepath}\.proxy\TempProxy.bat" ("alias proxyon='{0}'" -f 'source "$HOMEPATH/.proxy/proxy-switch.sh on"')
来源:https://stackoverflow.com/questions/29920934/embedding-in-the-powershell-script-issue