variable-expansion

What is the 'reword' function in Rebol and how do I use it?

梦想的初衷 提交于 2019-12-03 12:22:49
I saw someone mention the reword function today, but documentation for it is very brief. It looks like shell script environment variable substitution, or maybe regex substitution, but different. How do I use this function and what kind of gotchas am I going to run into? Here there be dragons! The reword function is a bit of an experiment to add shell-style string interpolation to Rebol in a way that works with the way we do things. Unlike a lot of Rebol's series functions, it really is optimized for working on just string types, and the design reflects that. The current version is a design

How to obtain the first letter in a Bash variable?

我是研究僧i 提交于 2019-12-02 21:32:35
I have a Bash variable, $word , which is sometimes a word or sentence, e.g.: word="tiger" Or: word="This is a sentence." How can I make a new Bash variable which is equal to only the first letter found in the variable? E.g., the above would be: echo $firstletter t Or: echo $firstletter T initial="$(echo $word | head -c 1)" Every time you say "first" in your problem description, head is a likely solution. word="tiger" firstletter=${word:0:1} word=something first=${word::1} A portable way to do it is to use parameter expansion (which is a POSIX feature) : $ word='tiger' $ echo "${word%"${word#?}

How to use both pipes and prevent shell expansion in perl system function?

时光毁灭记忆、已成空白 提交于 2019-12-02 14:12:41
If multiple arguments are passed to perl's system function then the shell expansion will not work: # COMMAND $ perl -e 'my $s="*"; system("echo", "$s" )' # RESULT * If the command is passed as an one argument then the expansion will work: # COMMAND $ perl -e 'my $s="echo *"; system("$s")' # RESULT Desktop Documents Downloads The system function also allows to using multiple commands and connect them using pipes. This only works when argument is passed as an one command: # COMMAND $ perl -e 'my $s="echo * | cat -n"; system("$s")' # RESULT 1 Desktop Documents Downloads How can I combine

How to Create Text Files from an Array of Values in Powershell

霸气de小男生 提交于 2019-12-01 11:41:29
I have a text file "list.txt" with a list of hundreds of URL's that I want to parse, along with some common-to-all config data, into individual xml files (config files) using each value in "list.txt", like so: list.txt contains: line_1 line_2 line_3 The boilerplate config data looks like (using line_1 as an example): <?xml version="1.0"?> <Website xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Url>line_1.mydomain.com</Url> <Title>line_1</Title> <Enabled>true</Enabled> <PluginInName>Tumblr</PluginInName> </Website> So if "list.txt" contains

How to do a partial expand in Snakemake?

£可爱£侵袭症+ 提交于 2019-12-01 08:07:08
I'm trying to first generate 4 files, for the LETTERS x NUMS combinations, then summarize over the NUMS to obtain one file per element in LETTERS: LETTERS = ["A", "B"] NUMS = ["1", "2"] rule all: input: expand("combined_{letter}.txt", letter=LETTERS) rule generate_text: output: "text_{letter}_{num}.txt" shell: """ echo "test" > {output} """ rule combine text: input: expand("text_{letter}_{num}.txt", num=NUMS) output: "combined_{letter}.txt" shell: """ cat {input} > {output} """ Executing this snakefile results in the following error: WildcardError in line 19 of /tmp/Snakefile: No values given

get a default value when variable is unset in make

泄露秘密 提交于 2019-11-30 08:58:37
(edit: question more accurate based on @Michael feedback) In bash, I often use parameter expansion : the following commands print " default value " when $VARNAME is unset, otherwise it prints the VARNAME content. echo ${VARNAME:-default value} #if VARNAME empty => print "default value" echo ${VARNAME-default value} #if VARNAME empty => print "" (VARNAME string) I did not find a similar feature on GNU make . I finally wrote in my Makefile : VARNAME ?= "default value" all: echo ${VARNAME} But I am not happy with this solution: it always creates the variable VARNAME and this may change the

Expand string Variable stored via Single Quote in Powershell

一曲冷凌霜 提交于 2019-11-28 14:15:16
I have a scenario where I need to construct a powershell path as $RemotePath = '$($env:USERPROFILE)\Desktop\Shell.lnk' . This variable gets passed to a remote machine where it needs to be executed. The remote machine receives this as a string variable. How do I expand the string to evaluate $env:USERPROFILE ? Ansgar Wiechers Expand the string on the remote side: $ExecutionContext.InvokeCommand.ExpandString($RemotePath) By using a double quotes. PowerShell won't expand variables inside single-quoted strings. 来源: https://stackoverflow.com/questions/27226606/expand-string-variable-stored-via

Is there a way to prevent percent expansion of env variable in Windows command line?

可紊 提交于 2019-11-28 12:41:22
I'm using the following git command in git bash on Windows: git log --format="%C(cyan)%cd%Creset %s" --date=short -5 It displays commit date ( %cd ) followed by commit message ( %s ). Commit date is wrapped with color markers: %C(cyan) to start colored output and %Creset to stop colored output. While it works fine in git bash, it doesn't do well with cmd : %cd% is expanded by Windows shell into current working directory (equivalent of $PWD in bash). Hence when that command is run via cmd , I see current working directory displayed instead of commit date in the first column! git bash: 2015-10

Performance of variable expansion vs. sprintf in PHP

微笑、不失礼 提交于 2019-11-28 11:54:58
Regarding performance, is there any difference between doing: $message = "The request $request has $n errors"; and $message = sprintf('The request %s has %d errors', $request, $n); in PHP? I would say that calling a function involves more stuff, but I do not know what's PHP doing behind the scenes to expand variables names. Thanks! Maxim Krizhanovsky In all cases the second won't be faster, since you are supplying a double-quoted string, which have to be parsed for variables as well. If you are going for micro-optimization, the proper way is: $message = sprintf('The request %s has %d errors',

Word splitting in Bash with IFS set to a non-whitespace character

佐手、 提交于 2019-11-28 11:21:45
I'm going through a Bash tutorial , and specifically the subject of word splitting. This script, called "args", helps demonstrate word splitting examples: #!/usr/bin/env bash printf "%d args:" $# printf " <%s>" "$@" echo An example: $ ./args hello world, here is "a string of text!" 5 args: <hello> <world,> <here> <is> <a string of text!> So far so good. I understand how this works. However, when I replace IFS with a non-whitespace character, say : , the script does not perform word splitting if I pass the string directly as an argument. $ ./args one:two:three 1 args: <one:two:three> However,