substitution

How to avoid heredoc expanding variables? [duplicate]

*爱你&永不变心* 提交于 2019-11-26 15:18:16
This question already has an answer here: How to cat <<EOF >> a file containing code? 3 answers I'm trying to create a script file using substitution string from ENV but want also to prevent some from escaping export PLACEHOLDER1="myPlaceholder1Value" export PLACEHOLDER2="myPlaceholder2Value" sudo /bin/su -c "cat << EOF > /etc/init.d/my-script #!/bin/bash # ### BEGIN INIT INFO # Provides: my-script # Required-Start: \$remote_fs \$syslog # Required-Stop: \$remote_fs \$syslog # Should-Start: \$network \$time # Should-Stop: \$network \$time # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short

How to use a variable in the replacement side of the Perl substitution operator?

半城伤御伤魂 提交于 2019-11-26 13:06:31
I would like to do the following: $find="start (.*) end"; $replace="foo \1 bar"; $var = "start middle end"; $var =~ s/$find/$replace/; I would expect $var to contain "foo middle bar", but it does not work. Neither does: $replace='foo \1 bar'; Somehow I am missing something regarding the escaping. I fixed the missing 's' On the replacement side, you must use $1, not \1. And you can only do what you want by making replace an evalable expression that gives the result you want and telling s/// to eval it with the /ee modifier like so: $find="start (.*) end"; $replace='"foo $1 bar"'; $var = "start

Remove all newlines from inside a string

爱⌒轻易说出口 提交于 2019-11-26 13:05:38
问题 I\'m trying to remove all newline characters from a string. I\'ve read up on how to do it, but it seems that I for some reason am unable to do so. Here is step by step what I am doing: string1 = \"Hello \\n World\" string2 = string1.strip(\'\\n\') print string2 And I\'m still seeing the newline character in the output. I\'ve tried with rstrip as well, but I\'m still seeing the newline. Could anyone shed some light on why I\'m doing this wrong? Thanks. 回答1: strip only removes characters from

Command substitution: backticks or dollar sign / paren enclosed? [duplicate]

谁说胖子不能爱 提交于 2019-11-26 12:05:30
This question already has an answer here: What is the difference between $(command) and `command` in shell programming? 6 answers What's the preferred way to do command substitution in bash? I've always done it like this: echo "Hello, `whoami`." But recently, I've often seen it written like this: echo "Hello, $(whoami)." What's the preferred syntax, and why? Or are they pretty much interchangeable? I tend to favor the first, simply because my text editor seems to know what it is, and does syntax highlighting appropriately. I read here that escaped characters act a bit differently in each case,

How to substitute text from files in git history?

。_饼干妹妹 提交于 2019-11-26 11:54:32
问题 I\'ve always used an interface based git client (smartGit) and thus don\'t have much experience with the git console. However, I now face the need to substitute a string in all .txt files from history (so, not erasing the whole file but just substituting a string). I found the following command: git filter-branch --tree-filter \'git ls-files -z \"*.php\" |xargs -0 perl -p -i -e \"s#(PASSWORD1|PASSWORD2|PASSWORD3)#xXxXxXxXxXx#g\"\' -- --all I tried this, and unfortunately noticed that while

JavaScript - Replace all commas in a string [duplicate]

妖精的绣舞 提交于 2019-11-26 10:05:27
问题 This question already has an answer here: How to replace all occurrences of a string? 59 answers I have a string with multiple commas, and the string replace method will only change the first one: var mystring = \"this,is,a,test\" mystring.replace(\",\",\"newchar\", -1) Result : \"thisnewcharis,a,test\" The documentation indicates that the default replaces all, and that \"-1\" also indicates to replace all, but it is unsuccessful. Any thoughts? 回答1: The third parameter of String.prototype

bash: Bad Substitution

自闭症网瘾萝莉.ら 提交于 2019-11-26 06:19:10
问题 #!/bin/bash jobname=\"job_201312161447_0003\" jobname_pre=${jobname:0:16} jobname_post=${jobname:17} This bash script gives me Bad substitution error on Ubuntu. Any help will be highly appreciated. 回答1: The default shell ( /bin/sh ) under Ubuntu points to dash , not bash . me@pc:~$ readlink -f $(which sh) /bin/dash So if you chmod +x your_script_file.sh and then run it with ./your_script_file.sh , or if you run it with bash your_script_file.sh , it should work fine. Running it with sh your

How to avoid heredoc expanding variables? [duplicate]

梦想的初衷 提交于 2019-11-26 04:43:49
问题 This question already has answers here : How to cat <<EOF >> a file containing code? (3 answers) Closed last year . I\'m trying to create a script file using substitution string from ENV but want also to prevent some from escaping export PLACEHOLDER1=\"myPlaceholder1Value\" export PLACEHOLDER2=\"myPlaceholder2Value\" sudo /bin/su -c \"cat << EOF > /etc/init.d/my-script #!/bin/bash # ### BEGIN INIT INFO # Provides: my-script # Required-Start: \\$remote_fs \\$syslog # Required-Stop: \\$remote

Command substitution: backticks or dollar sign / paren enclosed? [duplicate]

岁酱吖の 提交于 2019-11-26 03:32:38
问题 This question already has answers here : What is the difference between $(command) and `command` in shell programming? (6 answers) Closed 4 years ago . What\'s the preferred way to do command substitution in bash? I\'ve always done it like this: echo \"Hello, `whoami`.\" But recently, I\'ve often seen it written like this: echo \"Hello, $(whoami).\" What\'s the preferred syntax, and why? Or are they pretty much interchangeable? I tend to favor the first, simply because my text editor seems to

sed substitution with bash variables

吃可爱长大的小学妹 提交于 2019-11-25 21:55:57
问题 Trying to change values in a text file using sed in a bash script with the line, sed \'s/draw($prev_number;n_)/draw($number;n_)/g\' file.txt > tmp This will be in a for loop. Not sure why it\'s not working. Any suggestions? 回答1: Variables inside ' don't get substituted in bash. To get string substitution (or interpolation, if you're familiar with perl) you would need to change it to use double quotes " instead of the single quotes: $ # enclose entire expression in double quotes $ sed "s/draw(