I'm using perl on windows and am trying to do a one liner using perl to substitute a placeholder in a file using a windows variable that contains a dollar sign. Does anyone know what the correct usage is to make it work with the dollar sign. I've tried various ways and can't seem to get it to work.
For example, I have a properties file that has a token in it (!MYPASSWORD!) that I'm trying to replace like:
somevalue="!MYPASSWORD!"
I have a batch file that looks up a variable say called NEWPASSWORD that contains the password $abc12345$ and I want to use perl substitution to replace the value like the following. Note I may not always know where the $ signs are so I cant escape them. For example another password may be abc$124$563:
echo %NEWPASSWORD% <-- this would contain $abc12345$
perl -p -i.bak -e "s/!MYPASSWORD!/%NEWPASSWORD%/g" a.properties
When its done I want a.properties to be :
somevalue="$abc12345$"
Thanks in advance
Use ' as regexp delimeter symbol. It will disable all variable substitution:
perl -p -i.bak -e "s'!MYPASSWORD!'%NEWPASSWORD%'g" a.properties
I presume you are getting password from user input. why not just do that in Perl without having to go through batch since you are already using Perl? Its easier. you can then use modules like Term::Inkey to mask password and stuff.
simply use escape before dollar, like that :
\$
Related
We have an application that keeps some info in an encrypted file. To edit the file we have to put the text editor name in an environment variable in bash, for example, EDITOR=vi. Then we run the application and it opens the decrypted file in vi. I am trying to come up with a bash script that updates the encrypted file. The only solution that I can think of is passing sed command instead of vi to the EDITOR variable. It works perfectly for something like EDITOR='sed -i s#aaaa#bbbb#'.
Problem starts when I need space and regular expression. For example: EDITOR='sed -i -r "s#^(\s*masterkey: )(.*)#\1xxxxx#"' which return error. I tried running the EDITOR in bash with $EDITOR test.txt and I can see the problem. It doesn't like double quotes and space between them so I added a backslash before the double quotes and \s instead of space. Now it says unterminated address regex. For several hours I googled and couldn't find any solution. I tried replacing single quotes with double quotes and vice versa and everything that I could find on the internet but no luck.
How can I escape and which characters should I escape here?
Update:
Maybe if I explain the whole situation somebody could suggest an alternative solution. There is an application written by Ruby and it is inside a container. Ruby application has a secret_key_base for production and we supposed to change the key with EDITOR=vi rails credentials:edit --environment=production. I don't know Ruby and google did not return any ruby solution for automation so I could only think about sending sed instead of vi to Ruby.
How can I escape and which characters should I escape here?
That is not possible. Word splitting on the result of expansion cannot be escaped from inside the result of that expansion, it will always run. Note that filename expansion is also running over the result of the expansion.
Create an executable file with the script content and set EDITOR to it.
You could export a bash shell function, after some tries I got to:
myeditor() {
sed -i -E 's#^(\s*masterkey: )(.*)#\1xxxxx#' "$#"
}
export -f myeditor
EDITOR='bash -c "$#" _ myeditor'
I am looking to replace the line export KEY_COUNTRY="US" with the data read in the variable $COUNTRY in the file /etc/openvpn/easy-rsa/vars
In my previous builds which I used with CentOS6, i used the command replace which is bundled with mysql-server. But now that function is no longer available, i am looking for ideas on alternate command to replace the data.
Thanks in advance.
You can use sed, but keep in mind that it can fail if $COUNTRY contrains control characters.
sed -i 's/KEY_COUNTRY="US"/KEY_COUNTRY="'"$COUNTRY"'"/' file
Using Perl would be safer, as it doesn't interpret variable contents as part of the syntax:
perl -i -pe 's/KEY_COUNTRY="US"/KEY_COUNTRY="$ENV{COUNTRY}"/' -- file
$COUNTRY must be accessible in the perl process, i.e. you need to export it or assign to it
COUNTRY=$COUNTRY perl ...
I've a bash script that simple has to add new user and sign a password that is passed when script is called:
./adduser_script username password
and the password is then used as a parameter in the script like this:
/usr/sbin/useradd ... -p `openssl passwd -1 "$2"` ...
the problem occurs of course when password contains special characters like $#, $* itd. So when i call the script:
/adduser_script username aa$#bbb
and after script ends password looks like: aabbb (so the special charakters are removed from original password). The question is how can I correctly pass the original password with special charakters to the script?
Thanks in advance,
Regards
have you tried strong qoutes ??
use 'aa$#bb' instead of weak qoutes i.e. "aa$#bb"
for example: check with echo command
echo "aa$#bb" will print aabb
while
echo 'aa$#bb' will print aa$#bb
In your script use
/usr/sbin/useradd ... -p `openssl passwd -1 '$2'` ...
now you need not to worry about qoutes while passing password as argument.
The problem is probably not in your script at all, but rather on how you call it. At least from the snippets you provide, it doesn't seem like the password field is being evaluated.
So, when you call the script, if an argument contains something like $a, bash will replace it with the value of the variable a, or an empty string if it is unset.
If $ needs to be in the password, then it needs to be in single quotes.
./adduser_script username 'password$#aaa'
You can also use double quotes with escape .
For example: set password "MyComplexP\#\$\$word"
I have been facing similar issue and here is my in take on it.
One thing we need to consider is the user of single and double quotes in our script file.
if we put a value, variable in single quote the value will be as it is and it will not be replaced with actual value we reference.
example,
name='java'
echo '$name' ---> prints $name to console , but
echo "$name" ---> prints java to console.
So, play around on which quote is best to used based on your circumstance.
/usr/sbin/useradd ... -p "$(openssl passwd -1 '$2')"
I would like to write the $ symbol in a shell script.
I use echo for doing it, but now I need that special character in order to load
the text file and compute a plot modifying the columns in gnuplot
Thanks in advance,
Adrian
try using "\$" instead of "$", escape the character $
I am trying to batch fix some .scc files using vim and need to pass a substitution sting to it.
I'm trying to use vim -E to pass this line ":s/\r/\r/g"
I've tried embedding individual characters as a variable within the quotes but the \r messing it up.
Any advice would be appreciated.
Guessing at what you really want to do as it doesn't sound like a one-off change. The following will remove all \rs.
NOTE it does an inplace edit - no need to redirect etc.
perl -i.bak -p -e 's/\r+//g' filename(s)...
If you want to replace it with a single \r do
perl -i.bak -p -e 's/\r+/\r/g' filename(s)...
Perl will keep a backup but so should you.
It's not using vim but then perhaps you shouldn't want to.
In Vim, you must use \n in the search part and \r in the replacement part. But replacing \n with \r sounds a lot like doing nothing to me.
Anyway, launching Vim just for a substitution is probably a bit too much. You could use sed:
$ sed -i.orig 's/foo/bar/' foo.txt
Read $ man sed for more info.
got it. ':s/\\r/\\r/g'
I just needed the right level of quotation. The issue as I touched on earlier is that we get files that include PC double line breaks presumable made on a mac and the PC's can't read them properly.
So using sed to s/\r/\r\n/g wasn't really working for some reason, and maybe I could explore it more. But I think passing these to vim will help. I must use vim to send :set format=dos to the file.
Thanks all.