Need help SET /P variable=[promptString]

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
brh56
Posts: 8
Joined: 29 Apr 2015 08:59

Need help SET /P variable=[promptString]

#1 Post by brh56 » 26 Apr 2017 14:07

Good day.

I've been trying to load the DOS prompt with the following Linux command using echo, set /p and clip in a DOS prompt session.

Here is the Linux one-liner to check the uptime for 14 blades on a server:

Code: Select all

for b in 01 02 03 04 05 06 07 08 09 10 11 12 13 14; do echo;echo "BLADE ${b}";ssh -q `hostname | cut -d "-" -f 1`-s00c${b}h0 "echo -n 'BOOT TIME: ';who -b | tr -s ' ' | tr -d '\n';echo -en '\nUPTIME: ';uptime"; done"


The result is as follows:

Code: Select all

C:\Utils>echo|set /p="for b in 01 02 03 04 05 06 07 08 09 10 11 12 13 14; do echo;echo "BLADE ${b}";ssh -q `hostname | cut -d "-" -f 1`-s00c${b}h0 "echo -n 'BOOT TIME: ';who -b | tr -s ' ' | tr -d '\n';echo -en '\nUPTIME: ';uptime"; done"clip
'tr' is not recognized as an internal or external command,
operable program or batch file.


If I omit the 'tr' command, it works, however, I would like to use the 'tr' command on the Linux session. I did try using quotes around the 'tr' commands but the results are not correct.

Any ideas?

Bjoern

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Need help SET /P variable=[promptString]

#2 Post by penpen » 26 Apr 2017 17:11

The pipe character '|' has a special meaning (the same as in linux) and has to be escaped (using '^'), if you want to use it as a simple text (outside of doublequotes).
Because you redirect the output of a command to clip (using the pipe) the command line interpreter processes the command twice:
1. Process "command|clip" to create the pipe.
2. Process "command" and "clip" to execute the left and right command.

So you have to escape the "text pipes" in the left command twice:

Code: Select all

echo|set /p "=for b in 01 02 03 04 05 06 07 08 09 10 11 12 13 14; do echo;echo "BLADE ${b}";ssh -q `hostname | cut -d "-" -f 1`-s00c${b}h0 "echo -n 'BOOT TIME: ';who -b ^^^| tr -s ' ' ^^^| tr -d '\n';echo -en '\nUPTIME: ';uptime"; done" | clip

Beside this you could remove "echo|" and assign input from the nul device to "set/p":

Code: Select all

<nul set /p "=for b in 01 02 03 04 05 06 07 08 09 10 11 12 13 14; do echo;echo "BLADE ${b}";ssh -q `hostname | cut -d "-" -f 1`-s00c${b}h0 "echo -n 'BOOT TIME: ';who -b ^^^| tr -s ' ' ^^^| tr -d '\n';echo -en '\nUPTIME: ';uptime"; done" | clip


penpen

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Need help SET /P variable=[promptString]

#3 Post by aGerman » 26 Apr 2017 17:45

If the quotation mark next to done belongs to your command line you have to add it also with three carets escaped (because it's an unbalanced quotation mark).

Code: Select all

<nul set /p "=for b in 01 02 03 04 05 06 07 08 09 10 11 12 13 14; do echo;echo "BLADE ${b}";ssh -q `hostname | cut -d "-" -f 1`-s00c${b}h0 "echo -n 'BOOT TIME: ';who -b ^^^| tr -s ' ' ^^^| tr -d '\n';echo -en '\nUPTIME: ';uptime"; done"^^^" | clip


Steffen

brh56
Posts: 8
Joined: 29 Apr 2015 08:59

Re: Need help SET /P variable=[promptString]

#4 Post by brh56 » 27 Apr 2017 08:51

penpen wrote:The pipe character '|' has a special meaning (the same as in linux) and has to be escaped (using '^'), if you want to use it as a simple text (outside of doublequotes).
Because you redirect the output of a command to clip (using the pipe) the command line interpreter processes the command twice:
1. Process "command|clip" to create the pipe.
2. Process "command" and "clip" to execute the left and right command.

So you have to escape the "text pipes" in the left command twice:

Code: Select all

echo|set /p "=for b in 01 02 03 04 05 06 07 08 09 10 11 12 13 14; do echo;echo "BLADE ${b}";ssh -q `hostname | cut -d "-" -f 1`-s00c${b}h0 "echo -n 'BOOT TIME: ';who -b ^^^| tr -s ' ' ^^^| tr -d '\n';echo -en '\nUPTIME: ';uptime"; done" | clip

Beside this you could remove "echo|" and assign input from the nul device to "set/p":

Code: Select all

<nul set /p "=for b in 01 02 03 04 05 06 07 08 09 10 11 12 13 14; do echo;echo "BLADE ${b}";ssh -q `hostname | cut -d "-" -f 1`-s00c${b}h0 "echo -n 'BOOT TIME: ';who -b ^^^| tr -s ' ' ^^^| tr -d '\n';echo -en '\nUPTIME: ';uptime"; done" | clip


penpen

Thank you, penpen, both solutions work perfectly.

Bjoern

brh56
Posts: 8
Joined: 29 Apr 2015 08:59

Re: Need help SET /P variable=[promptString]

#5 Post by brh56 » 27 Apr 2017 08:53

aGerman wrote:If the quotation mark next to done belongs to your command line you have to add it also with three carets escaped (because it's an unbalanced quotation mark).

Code: Select all

<nul set /p "=for b in 01 02 03 04 05 06 07 08 09 10 11 12 13 14; do echo;echo "BLADE ${b}";ssh -q `hostname | cut -d "-" -f 1`-s00c${b}h0 "echo -n 'BOOT TIME: ';who -b ^^^| tr -s ' ' ^^^| tr -d '\n';echo -en '\nUPTIME: ';uptime"; done"^^^" | clip


Steffen

Oops, thanks Steffen. I cut off the first quotation mark by accident.

Bjoern

brh56
Posts: 8
Joined: 29 Apr 2015 08:59

Re: Need help SET /P variable=[promptString]

#6 Post by brh56 » 27 Apr 2017 09:01

Now I find the above a bit confusing because in the next command, there is no problem with the pipe to egrep.

Code: Select all

echo|set /p="clear;echo Hub7;ssh -q 169.254.64.112 ethtool eth2|egrep 'Speed|Duplex|Auto-neg|Link';echo Hub8;ssh -q 169.254.64.128 ethtool eth2|egrep 'Speed|Duplex|Auto-neg|Link'"|clip


Bjoern

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Need help SET /P variable=[promptString]

#7 Post by penpen » 27 Apr 2017 10:09

Characters outside doublequotes might be operators or normal text depending on the characters themselves;
examples (without surrounding single quotes):
- operators: '&&' '&', '||', '|', '(', '^', '>', ...
- normal text: 'a', 'b', 'c', ..., '0', '1', '2', ... , 'ABC', ...

Text inside doublequotes ('"') or after circumflex accent ('^') always is treated as normal text
(except the exclamation mark ('!'), when using delayed expansion, which is treated as an operator during the delayed expansion);
example (without surrounding single quotes):
normal text: 'abc"&& & > < "012'

Now mark the text inside doublequotes with green, the text outside of doublequotes with red, and the doublequotes itselves with black:
1) echo|set /p="for b in 01 02 03 04 05 06 07 08 09 10 11 12 13 14; do echo;echo "BLADE ${b}";ssh -q `hostname | cut -d "-" -f 1`-s00c${b}h0 "echo -n 'BOOT TIME: ';who -b | tr -s ' ' | tr -d '\n';echo -en '\nUPTIME: ';uptime
"; done"clip

2) echo|set /p="clear;echo Hub7;ssh -q 169.254.64.112 ethtool eth2|egrep 'Speed|Duplex|Auto-neg|Link';echo Hub8;ssh -q 169.254.64.128 ethtool eth2|egrep 'Speed|Duplex|Auto-neg|Link'"|clip

You have to escape all operator characters in the red sections (twice for the above given reason), that are not meant to be interpreted as operators by the batch command.


penpen

Post Reply