How to Make the Say Command Echo a Variable Value in a Script

How to make the say command echo a variable value in a script?

Remove backticks (Kernel#`):

system("say \"#{my_variable}\"")

or

system("say '#{my_variable}'")

How do I get the result of a command in a variable in windows?

If you have to capture all the command output you can use a batch like this:

@ECHO OFF
IF NOT "%1"=="" GOTO ADDV
SET VAR=
FOR /F %%I IN ('DIR *.TXT /B /O:D') DO CALL %0 %%I
SET VAR
GOTO END

:ADDV
SET VAR=%VAR%!%1

:END

All output lines are stored in VAR separated with "!".

But if only a single-line console-output is expected, try:

@ECHO off
@SET MY_VAR=
FOR /F %%I IN ('npm prefix') DO @SET "MY_VAR=%%I"

@REM Do something with MY_VAR variable...

@John: is there any practical use for this? I think you should watch PowerShell or any other programming language capable to perform scripting tasks easily (Python, Perl, PHP, Ruby)

How can pass the value of a variable to the standard input of a command?

Simple, but error-prone: using echo

Something as simple as this will do the trick:

echo "$blah" | my_cmd

Do note that this may not work correctly if $blah contains -n, -e, -E etc; or if it contains backslashes (bash's copy of echo preserves literal backslashes in absence of -e by default, but will treat them as escape sequences and replace them with corresponding characters even without -e if optional XSI extensions are enabled).

More sophisticated approach: using printf

printf '%s\n' "$blah" | my_cmd

This does not have the disadvantages listed above: all possible C strings (strings not containing NULs) are printed unchanged.

Windows Batch: How to set the output of one command as a variable and use it in another command?

When you refer to "store each one in a variable", the involved concept here is array. You may split the words of NAMELIST variable into 3 array elements this way:

setlocal EnableDelayedExpansion
set i=0
for %%a in (%namelist%) do (
set /A i=i+1
set VAR!i!=%%a
)

This way, you may use each array element directly:

perl.exe C:\action.pl %VAR1%
perl.exe C:\action.pl %VAR2%
perl.exe C:\action.pl %VAR3%

Or, in a simpler way using a loop:

for /L %%i in (1,1,3) do perl.exe C:\action.pl !VAR%%i!

EDIT: You may use this method with an unlimited number of values in the NAMELIST variable, just use the previous value of %i% instead the 3 (better yet, change it by "n"). I also suggest you to use the standard array notation this way: VAR[%%i]:

setlocal EnableDelayedExpansion
set namelist=AAA BBB CCC DDD EEE FFF
set n=0
for %%a in (%namelist%) do (
set /A n+=1
set VAR[!n!]=%%a
)
for /L %%i in (1,1,%n%) do perl.exe C:\action.pl !VAR[%%i]!

Executing bash commands in Ruby with variable interpolation?

You need to use double quotes " instead of single ones. Single quotes will not interpolate variables.

foo = 'bar'

puts '#{foo}'
#{foo}

puts "#{foo}"
bar

batch files calling %~1 and getting the variable's current value/string

Another method is using command CALL to get a double parsing of a command line as shown below:

@echo off
goto :MainFunction

:Func01
echo/
echo Running Func01
call echo Variable %~1 current value is %%%~1%%
echo/
set /P "%~1=Set new value for Variable %~1: "
goto :EOF

:MainFunction
echo This is the main function!
set "Var01=string01"
set "var02=string02"
echo Var01 is equal to %Var01%
echo Var02 is equal to %Var02%
call :Func01 Var01
call :Func01 Var02
echo Var01 is now equal to %Var01%
echo Var02 is now equal to %Var02%
goto :EOF

The command line

call echo Variable %~1 current value is %%%~1%%

is first modified before running CALL by Windows command interpreter for example on first execution of subroutine Func01 to

call echo Variable Var01 current value is %Var01%

On parsing this command line a second time on running CALL the command line changes to:

echo Variable Var01 current value is string01

The character % must be escaped with one more % to be interpreted as literal character in a batch file which is the reason for the strange looking syntax %%%~1%%.

And take a look on DosTips forum topic ECHO. FAILS to give text or blank line - Instead use ECHO/ for the explanation on using echo/ instead of echo. to output an empty line.

Understanding what happens with variables when I call one batch script from another (largely terminology/semantics specific)?

_result variable is set in the environment test.bat is called in, and that's why test2.bat can access it (and modify it), and also why you can echo _result after test.bat is finished.

When you call test2.bat %_result% you are just passing in the value of _result to test2's first argument (read by %1).

You can add setlocal at the start of test.bat to get the environment variables back where they were when you're done.



Related Topics



Leave a reply



Submit