set CMD output to variable - cmd

This is what I have:
> set email=echo abc#xyz.com
> echo %email%
the above snippet echoes echo abc#xyz.com. I want it to print abc#xyz.com instead.
I tried echo abc#xyz.com | set email as well. same result.
How do I do that?

You could let cmd eval it:
#ECHO OFF
set email=echo abc#xyz.com
%email%
I use this on our Windows 10, 2016 and 2019 regulary. I don't know if it works for older versions though.

Related

How to get master and slave version gitlab-ci.yml

I have my pippeline where:
- echo 'Komponente;Version' >> $VERSION_CSV
- echo 'Master; \"$MASTER_VERSION\"' >> $VERSION_CSV
- echo 'Slave; \"$SLAVE_VERSION\"' >> $VERSION_CSV
- 'eval "$DEPLOY_CURL_COMMAND_4"'
but in the output displays this and not the version:
someone know how to display the version or tell me what am I doing wrong?
This has nothing to do with Gitlab CI, what you are doing is Shell-Scripting and your problem is in Shell-Skripting. As you didn't write which Shell we can only guess. For Bash this should give you the right result:
echo "Master; $MASTER_VERSION" >> $VERSION_CSV
Pro-Tip: Don't show your real paths on Stackoverflow.

How to consistently record command output in a variable in UNIX

In some cases what I see in a console output is different from what I get recorded after redirection. I see this on Linux/bash but this example is ksh/OpenBSD. Is there a way around this?
For example:
# pfctl -ttable -Ttest 123.123.123.123 > result.txt
0/1 addresses match.
# more result.txt
result.txt (END)
In other words the "0/1 addresses match." is printed on the console, but I cannot for the life of me get it into a file, variable or anything. I've used $() and > which work for most commands, but every now and then there is a command that spits out stuff on the screen but I get nothing via the redirect/pipe. I hope someone can shed light on this peculiarity.
So again contrast this:
# OUTP=$(pfctl -tscanners -Ttest 123.123.123.123)
0/1 addresses match.
# echo $OUTP
#
(nothing echoing, the variable does not hold the console output) with this:
# OUTP=$(date)
# echo $OUTP
Sun Aug 21 08:33:37 PDT 2016
#
(the variable contains the entire console output)
Thanks again for any help.
Your command has 2 different output streams.
You need to rederict the second (stderr) to the first.
pfctl -ttable -Ttest 123.123.123.123 > result.txt 2>&1

Change standard separator comma for attachments

I know that comma will be used to separate files and other stuff with Blat mailer.
But I must send files with a comma in their filenames and have no possibility to change this circumstance.
Is there a way to change the standard separator?
Or any other way without changing the filename?
Here my example what I try to do:
set SERVER=127.0.0.1:2525
set USER=sendout#ralfbb-test.local
set PW=secret
set FROMNAME=sendout#ralfbb-test.local
set TO=nurmail1#ralfbb-test.local
set SUBJECT="Subject test - attached file!"
set BODY="Oh yes my dear lad!"
set BODYF="c:\Mailbody.txt"
set ATTACH="c:\517-BS24430-210416-NULL-0,750.pdf"
blat -server %SERVER% -f %FROMNAME% -u %USER% -pw %PW% -to %TO% -subject %SUBJECT% -bodyF %BODYF% -attach %ATTACH% -debug -log blat.log
Add a "" when calling the variable:
"%ATTACH%"

Windows CMD - use numbered variables

If this has been asked here before, I'm not finding it.
I think I know a way around my actual problem, but it'd get a bit unwieldy; so this is more of a puzzle, I guess.
I want to set 12 variables:
set 01=01 - January
set 02=02 - February
set 03=03 - March
set 04=04 - April
set 05=05 - May
set 06=06 - June
set 07=07 - July
set 08=08 - August
set 09=09 - September
set 10=10 - October
set 11=11 - November
set 12=12 - December
If I use a CMD file to ask what's contained in %01%, I'm returned the name of the CMD file (which is always %0) plus the number 1. For %10% through %12%, I receive the content of %1, which in my case so far has been empty, plus the second digit of the variable name.
I'm moving files based on date to a storage server. The files are named "2015-01-01 Blah, Blah, Blah.txt", for instance. I can use code obtained here elsewhere to find out the year as token1 and the month as token2.
The storage destination has a folder for 2015, and inside that are subfolders, "01 - January", "02 - February", and so on.
I can always do a thorough "if token2=" each freaking month, for every single file, but I'm wondering whether there's a way to streamline the logic: Move %1 token1\token2, where 01 is expanded to "01 - January", without needing to IF my way through each month.
Code to start from:
#echo off
set 01=01 - January
set 02=02 - February
set 03=03 - March
set 04=04 - April
set 05=05 - May
set 06=06 - June
set 07=07 - July
set 08=08 - August
set 09=09 - September
set 10=10 - October
set 11=11 - November
set 12=12 - December
:: set /p cr_mo= "Pick a month, any month! "
:: echo CR_Mo=%CR_Mo%
:: echo Destination folder: %CR_Mo%
echo 01=%01%
echo 02=%02%
echo 03=%03%
echo 04=%04%
echo 05=%05%
echo 06=%06%
echo 07=%07%
echo 08=%08%
echo 09=%09%
echo 10=%10%
echo 11=%11%
echo 12=%12%
easiest way is to use delayed expansion:
#echo off
setlocal enabledelayedexpansion
set 01=01 - January
echo 01=!01!

bash script variable array name

Hello I want to ask a question that is repeated here.
I have four servers in bash script defined like in the code below.
For each server, I want to maintain the ID of the process I have started on it.
Just for testing, I wanted to initialize each array with 10 20 30 40.
And see if I can access these elements as expected. However I cannot access the elements.
Could someone tell me what exactly I am doing wrong.
#!/bin/bash
SERVER_LIST=("server1" "server2" "server3")
for server in ${SERVER_LIST[#]} ; do
echo $server
arrayName=$server"process"
echo $arrayName
set -a "$arrayName=(10 20 30 40)"
done
current_sever=${SERVER_LIST[0]}
arrayName=$current_server"process"
# The attempt below is failing.
eval "echo Server ${current_server} \${$arrayName[*]}"
echo $(eval echo \${arrayName[*]})Server server1
server1process
It gives me output as follows -
Server server1
server1process
Can someone help please.
Also can you please tell me how to append new element to the array?
I tried the following, but it doesn't work -
sleep 10 &
arrayName=$current_server"process"
eval "\${$arrayName[*]}+=$!"
Try replacing line 8:
set -a "$arrayName=(10 20 30 40)"
with:
eval "$arrayName=(10 20 30 40)"

Resources