I need echo a html string;
echo <html><body></body></html>;
result < it was unexpected
echo "<html><body></body></html>";
result "<html><body></body></html>";
I do not want the double quotation marks to be displayed;
echo ^<html^>^</html^>
result <html></html>
It works perfect for me, however the string is large, and if I give scape on all the characters, it will exceed the limit size of the prompt;
Note, I can not save this string to a file.
I think you can use SET command with quotation mark:
SET "string=<html><body></body></html>;"
then ECHO it goes ok
Related
I have Windows Batch code where I'm trying to test if the first character of value is a hyphen but the code fails when the first character of value is a double quote. (value is coming from elsewhere in the real code.)
set value=""This is a test""
set arg="%value%"
set prefix=%arg:~1,1%
if "%prefix%" == "-" ...
Evaluates to
if """ == "-"
and generates The syntax of the command is incorrect. I tried inserting a caret into both sides of the equality check
if "^%prefix%" == "^-" ...
but that generates the same error with
if "^"" == "^-"
You can use the caret to escape a single character, like
if ^"^%prefix%^" == "-" ...
The trick is to escape the quotes, too, else the inner caret has no special meaning.
This can't work for more than one character, but you could switch to the even simple delayed expansion. It's expansion is always safe.
setlocal EnableDelayedExpansion
set "value="This is a test""
set "arg=!value!"
set "prefix=!arg:~1,1!"
if "!prefix!" == "-" ...
test if the first character of value is a hyphen
Another approach (using the original string, no additional variables):
#echo off
setlocal EnableDelayedExpansion
set value1=-correct
set value2=xfail
set value3="fail
set value
echo ---------
echo !value1!|findstr /bc:"-" >nul && echo hyphen || echo something else
echo !value2!|findstr /bc:"-" >nul && echo hyphen || echo something else
echo !value3!|findstr /bc:"-" >nul && echo hyphen || echo something else
I am trying to debug a bash shell script where I am trying to surround a string/variable with single quotes. I am seeing the following results and am stumped on how to debug this. It obviously has something to do with the content of the variable. I thought the variable may be an array hence some of the echo statements. IN_JSON is being constructed via calls to "jq" to construct some JSON.
echo "IN_JSON = ${IN_JSON}"
echo "IN_JSON = ${IN_JSON[*]}"
echo "IN_JSON = '${IN_JSON[*]}'"
echo "IN_JSON = '" ${IN_JSON} "'"
echo "${#IN_JSON[#]}"
Output:
IN_JSON = {"name":"RX-CLAIM-FILLED"}
IN_JSON = {"name":"RX-CLAIM-FILLED"}
'N_JSON = '{"name":"RX-CLAIM-FILLED"}
'_JSON = ' {"name":"RX-CLAIM-FILLED"}
1
What's going on here and how do I troubleshoot this? It obviously has something to do with the contents of IN_JSON, but I'm not sure why or what is going on here.
The expansion of ${IN_JSON[*]} contains a carriage return character that resets the position of the cursor to beginning of the line, so that the next character ' is printed on beginning of the line.
Most probably, you want to run your file via dos2unix.
In Bash, I am trying to create a path with two variables within:
/path/to/my/file/${variable1_-}${variable2}/Still/some/path
My variable2 is always set, but the variable1 might be empty and in that case I don't want to print the "_"
I have tried the line above, but doesn't seem to be correct.
Can someone help in getting the right path printed?
Thanks in advance for your suggestions!
You have a simple typo (the underscore should be after the separator, not part of the variable name) and you want to include the underscore if variable1 is set, not it it's unset (so plus instead of minus in the parameter expansion; and add a colon to also cover the set but empty case). Presumably you also want to include the actual value of variable1 when it's set.
/path/to/my/file/${variable1}${variable1:+_}${variable2}/Still/some/path
or equivalently, nested
/path/to/my/file/${variable1:+${variable1}_}${variable2}/Still/some/path
where the braces before the underscore are necessary to separate the variable name from the literal text.
You can use this.
https://linux.die.net/man/1/bash
${parameter:+word}
set also variable1
variable1=VAR1
variable2=VAR2
variable3=${variable1:+_}
echo /path/to/my/file/${variable1}${variable3}${variable2}/Still/some/path
set only variable2
variable1=
variable2=VAR2
variable3=${variable1:+_}
echo /path/to/my/file/${variable1}${variable3}${variable2}/Still/some/path
With a few more lines of code this could work:
run () {
prefix="" # empty
if [ -n "$variable1" ]; then
prefix="${variable1}_"
fi
echo "/path/to/my/file/${prefix}${variable2}/Still/some/path"
}
# set only variable2
variable2=var2
run
# set also variable1
variable1=var1
run
output:
/path/to/my/file/var2/Still/some/path
/path/to/my/file/var1_var2/Still/some/path
description:
-n tests if the string is not empty, in that case I fill prefix with variable1 and the underscore
echo %time:~-5% returns a 5-character string, such as
18.09
However, i cannot seem to set that string to a variable:
set start = %time:~-5%
echo %start%
returns
10:07:18.09
what's simple way to get those last 5 chars into a variable?
thx
Windows doesn't like space next to =. So try this ...
set start=%time:~-5%
echo %start%
I am trying to randomize the echo character in the Highline gem's ask method, but could not get it to work. Did I not do this right?
srand
ask("password: ") { |q| q.echo = ('a'.ord+rand(26)).chr }
The character is randomized for each ask() call, but not each character. The first run will echo the same character, i.e. 'cccc'. The next run will echo 'mmmm', etc.
echo is a variable value used to determine whether to echo output. From the highline source:
# [echo] Can be set to +true+ or +false+ to control whether or not input will
# be echoed back to the user. A setting of +true+ will cause echo to
# match input, but any other true value will be treated as a String to
# echo for each character typed.
Your code (('a'.ord+rand(26)).chr) is being evaluated once per ask, stored in the echo variable within highline, and then printed out for each character entered.
You can't get it to print a different random character per input character without modifying highline.