variable-expansion

Batch file FOR/f expansion

徘徊边缘 提交于 2019-12-11 01:25:07
问题 I have a file ( directories.txt ) with directory names, each on a single line and I like to expand the line C:\Documents and Settings\%USERNAME%\My Documents In my script to the real user name running the script. However the echo comes out exactly the same as the line and %USERNAME% does not expand. FOR /f "tokens=*" %%X IN (directories.txt) DO ( ECHO %%X ) The echo shows "C:\Documents and Settings\%USERNAME%\My Documents" instead of C:\Documents and Settings\ janco \My Documents Any ideas?

How to expand file content with powershell

大兔子大兔子 提交于 2019-12-08 20:43:29
I want to do this : $content = get-content "test.html" $template = get-content "template.html" $template | out-file "out.html" where template.html contains <html> <head> </head> <body> $content </body> </html> and test.html contains: <h1>Test Expand</h1> <div>Hello</div> I get weird characters in first 2 characters of out.html : �� and content is not expanded. How to fix this ? mklement0 To complement Mathias R. Jessen's helpful answer with a solution that: is more efficient. ensures that the input files are read as UTF-8, even if they don't have a (pseudo-) BOM (byte-order mark) . avoids the

How to expand file content with powershell

馋奶兔 提交于 2019-12-08 04:09:12
问题 I want to do this : $content = get-content "test.html" $template = get-content "template.html" $template | out-file "out.html" where template.html contains <html> <head> </head> <body> $content </body> </html> and test.html contains: <h1>Test Expand</h1> <div>Hello</div> I get weird characters in first 2 characters of out.html : �� and content is not expanded. How to fix this ? 回答1: To complement Mathias R. Jessen's helpful answer with a solution that: is more efficient. ensures that the

Replace variables in text: Suggestions?

孤者浪人 提交于 2019-12-07 21:05:53
问题 I'm looking for a nice template engine or short piece of code to expand Ant-like variables in a string in Java. Example: String result = expand ("${firstName} ${familyName}", map); It should at least support java.util.Map but something that can handle beans or recursive lookups or lookups in a list of maps/objects would be welcome, too. Suggestions? [EDIT] In reply to TofuBeer: No nesting, only valid Java identifiers within the {} . Anything outside of ${} should be copied verbatim. $$ should

Windows CMD Batch: FOR /R with DelayedExpansion

 ̄綄美尐妖づ 提交于 2019-12-07 10:08:42
问题 On my desktop, there is a folder named "test" . Inside this folder is two files, "file1.txt" and "file2.txt" . Take a look at this simple batch script: @ECHO OFF SET test="C:\Users\Tyler\Desktop\test" ECHO %test% FOR /R %test% %%F IN (*) DO ( ECHO %%F ) As you might expect, it outputs the following: "C:\Users\Tyler\Desktop\test" C:\Users\Tyler\Desktop\test\file1.txt C:\Users\Tyler\Desktop\test\file2.txt Now take a look at this variation: @ECHO OFF SETLOCAL ENABLEDELAYEDEXPANSION SET test="C:

Replace variables in text: Suggestions?

拟墨画扇 提交于 2019-12-06 13:45:41
I'm looking for a nice template engine or short piece of code to expand Ant-like variables in a string in Java. Example: String result = expand ("${firstName} ${familyName}", map); It should at least support java.util.Map but something that can handle beans or recursive lookups or lookups in a list of maps/objects would be welcome, too. Suggestions? [EDIT] In reply to TofuBeer: No nesting, only valid Java identifiers within the {} . Anything outside of ${} should be copied verbatim. $$ should become $``. If that's not possible ${dollar} should expand to a single $ (so you can express 15.00 $ )

In Bash, is there a way to expand variables twice in double quotes?

扶醉桌前 提交于 2019-12-06 06:33:15
问题 For debugging my scripts, I would like to add the internal variables $FUNCNAME and $LINENO at the beginning of each of my outputs, so I know what function and line number the output occurs on. foo(){ local bar="something" echo "$FUNCNAME $LINENO: I just set bar to $bar" } But since there will be many debugging outputs, it would be cleaner if I could do something like the following: foo(){ local trace='$FUNCNAME $LINENO' local bar="something" echo "$trace: I just set bar to $bar" } But the

Windows CMD Batch: FOR /R with DelayedExpansion

心不动则不痛 提交于 2019-12-05 15:55:00
On my desktop, there is a folder named "test" . Inside this folder is two files, "file1.txt" and "file2.txt" . Take a look at this simple batch script: @ECHO OFF SET test="C:\Users\Tyler\Desktop\test" ECHO %test% FOR /R %test% %%F IN (*) DO ( ECHO %%F ) As you might expect, it outputs the following: "C:\Users\Tyler\Desktop\test" C:\Users\Tyler\Desktop\test\file1.txt C:\Users\Tyler\Desktop\test\file2.txt Now take a look at this variation: @ECHO OFF SETLOCAL ENABLEDELAYEDEXPANSION SET test="C:\Users\Tyler\Desktop\test" ECHO !test! FOR /R !test! %%F IN (*) DO ( ECHO %%F ) ENDLOCAL I would expect

In Bash, is there a way to expand variables twice in double quotes?

拥有回忆 提交于 2019-12-04 14:54:33
For debugging my scripts, I would like to add the internal variables $FUNCNAME and $LINENO at the beginning of each of my outputs, so I know what function and line number the output occurs on. foo(){ local bar="something" echo "$FUNCNAME $LINENO: I just set bar to $bar" } But since there will be many debugging outputs, it would be cleaner if I could do something like the following: foo(){ local trace='$FUNCNAME $LINENO' local bar="something" echo "$trace: I just set bar to $bar" } But the above literally outputs: "$FUNCNAME $LINENO: I just set bar to something" I think it does this because

What does “${!var}” mean in shell script? [duplicate]

允我心安 提交于 2019-12-03 16:16:57
This question already has answers here : Closed 2 years ago . What is indirect expansion? What does ${!var*} mean? (5 answers) I have a code block with below condition, not sure what exactly it does. $var = "${args}_Some_Text" if [ "${!var}" == '' ];then echo "$var is not defined !!!" fi This is called variable indirect expansion. $ hello="this is some text" # we set $hello $ var="hello" # $var is "hello" $ echo "${!var}" # we print the variable linked by $var's content this is some text As you see, it is a way to define "variable variables". That is, to use variables whose content is the name