Redirection in Shell()

In either the cmd console or a batch file, the following sends the executable file’s output to the clipboard.

<path>\foo.exe | clip

If I run this in Shell(), the executable file runs, but nothing is sent to the clipboard.

The following also doesn’t send the output to the clipboard.

Shell("cmd.exe /C ""<path>\foo.exe | clip""", 2,,True)

Edit: The above was originally typed from memory & left out some quote marks. I’ve corrected it. Also, <path> & foo are obviously stand ins for the actual path & file name.

I tried hitting the computer with a rock, but that didn’t help

Before I resign myself to using a batch file, does anyone know of a way to redirect output in Shell?

Looks like we have a xy-problem

please, please explain: WHAT exactly does this magic »foo.exe« and also WHAT is your plan to do with the clipboard then!!

:thinking:
image

I typed from memory. That should be

Shell("cmd.exe /C ""<path\foo | clip""", 2,,True)

never good :wink:

is clip in the PATH variable ?

@fpy Clip is the system command line utility for dumping output into the clipboard. It would be strange if it weren’t in the PATH. %SystemRoot%\system32 is in the PATH, and clip is in that directory, so clip is in the PATH. I can confirm that that by typing “clip /?” in the console and reading the help message. Or I can type “dir | clip” and see the contents of the directory in the clipboard.

I probably should have posted this originally. To demonstrate, type

Echo some text

into notepad & save it as foo.cmd. Type

<path>\foo.cmd | clip

into the cmd console & run it. If you check, you’ll see that the text is now in the clipboard.

In LO Basic, run the Shell function w/

Shell("<path>\foo.cmd | clip", 2,,True)

It runs but the output doesn’t show up in the clipboard.

Again, run the Shell function w/

Shell("cmd.exe /C ""<path>\foo.cmd | clip""", 2,,True)

It runs but the output doesn’t show up in the clipboard.

I did test “> test.txt” in place of “| clip”, & that works. So apparently it’s not all redirection, but only the pipe that doesn’t seem to work in Shell.

@fcsw again: what is »foo.exe« supposed to do, and why do you think its output needs to pushed into clipboard?

In my case, foo.exe sends http pings to a web site & returns the results. Grabbing the output from the clipboard is more convenient & faster than processing a text file containing the output.

so we come closer:

from subprocess import run

google_ping = run(["ping",'-c8', "8.8.8.8"], capture_output=True)
output = google_ping.stdout.decode('utf8')
print(output)
###
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=2 ttl=113 time=61.9 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=113 time=117 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=113 time=37.8 ms
64 bytes from 8.8.8.8: icmp_seq=5 ttl=113 time=64.3 ms
64 bytes from 8.8.8.8: icmp_seq=6 ttl=113 time=170 ms
64 bytes from 8.8.8.8: icmp_seq=7 ttl=113 time=51.6 ms
64 bytes from 8.8.8.8: icmp_seq=8 ttl=113 time=134 ms

--- 8.8.8.8 ping statistics ---
8 packets transmitted, 7 received, 12.5% packet loss, time 7012ms
rtt min/avg/max/mdev = 37.840/90.887/170.155/45.789 ms
###
1 Like

… and more problematic. Any concurrent interaction with the shared system resource, which clipboard is, will break your program flow. You (and users of your program) won’t be allowed to copypaste during its execution.

@karolus I appreciate the effort, but I said foo was running http pings, not icmp pings. In this case, an http ping is basically a HEAD request, which doesn’t put much burden on the web server but returns a status code; e.g. 302 redirected.

I don’t recognize the programming language. It’s been a long time since I coded in anything but Basic.

@mikekaganski
Yep. It will require code that checks the contents of the clipboard before processing it. There’s no perfect solution here. If the clipboard turns out to be too unreliable, it won’t be the first unsuccessful experiment I’ve made since I started this project. It’s the nature of programming that not everything works as well as the programmer thinks it will.

thats fine, so you dont need subprocess anymore, but some simple http-request, which can be done by python!

import urllib.request

with urllib.request.urlopen("https://www.google.com/") as g:
    out = g.status

print(out)
###
200

python urllib.request

as already said 16 hours ago:

Apparently I was right.

wow, marvelous tautology : you’re not using python, so you must use python :face_with_thermometer:

or only clip ?

on my linux box, both work :

 Shell("bash -c ""date > /tmp/xx"" ", 2,,True)
 Shell("bash -c ""date | tee /tmp/xx"" ", 2,,True)

$ cat /tmp/xx
Fri Aug 29 08:58:42 AM CEST 2025
Fri Aug 29 08:58:42 AM CEST 2025

@fpy YOU and @fcsw MUST not! you may continue playing around obfuscated shell -command-strings

but unfortunatly @fcsw OS is not a linux box!

just pretty standard (since the 70’s)

indeed.
apparantely I was right :wink:

It’s great that you’ve been working with shell commands since the 1970s! You have my respect for that!

Otherwise, I also like to do a lot in the terminal, but I would never try to execute these commands from basic via shell aslong I know that there is a more robust way to do so!

not that I’m looking for or needing it, but thanks for mentioning;
you also deserve my and our respect for all your positive contributions in general.

authority answer doesn’t make it right,
and XY problem is not about just robustness impressionism.
IMHO, this site is about sharing, not just dogmatism :wink:

if there are known bugs with Shell(), we should try to log and solve them;

nothing major at first glance …
153719 – 7.5 BASIC shell function fails (flatpak and snap versions)

yes… In this case, dogmatism is insisting on using “shell” because you absolutely want to continue working with “basic.”

Why are you trying to delegate this work to others?
Do it yourself!
I’m certainly not going to try to track down and fix all the side effects of “basic … shell” in the context of container formats such as (spap, flatpak, and appimages)!

you may need to double check the title of this topic ;

or go enjoy, there plenty of others to hijack : Topics tagged basic

weird twist :frowning: