How to execute shell command and read back the result in XY?

Discuss and share scripts and script files...
Post Reply
avsfan
Posts: 554
Joined: 29 Jun 2006 09:00
Location: Fort Collins, Colorado

How to execute shell command and read back the result in XY?

Post by avsfan »

Hi,

I'm trying to figure out a way to get the current version number of a given file in my subversion repository from within XY. From the command line, I can run the following command:

Code: Select all

svn list --verbose myfile
which yields the result

Code: Select all

    274 me               2302 Feb 12 22:22 myfile
What I want to do is to get the "274" value into a script (the value will change depending on <myfile> is used).

I looked through the scripting command reference, and nothing jumped out at me. I don't do much scripting (yet!), so please be patient!

Thanks!

40k
Posts: 234
Joined: 09 Dec 2011 21:25

Re: How to execute shell command and read back the result in

Post by 40k »

You may be able to pipe the output from the command line away from stdout screen into a file.

for example:

Code: Select all

ffmpeg -help > help.txt
That will dump the output from the ffmpeg help command into a file called help.txt
You can then use the readfile function to read the output into a variable.

It's a bit of a workaround but perhaps it works.
I develop scripts that integrate media functionality into Xyplorer.
Hash - Twitch.tv in VLC (NEW 2.0!) - FFmpeg GUI - Youtube downloading
XYplorer for Linux! Tutorial

Marco
Posts: 2354
Joined: 27 Jun 2011 15:20

Re: How to execute shell command and read back the result in

Post by Marco »

Yes, with plain command line 40k's solution is the only way.
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]

avsfan
Posts: 554
Joined: 29 Jun 2006 09:00
Location: Fort Collins, Colorado

Re: How to execute shell command and read back the result in

Post by avsfan »

40k wrote:You may be able to pipe the output from the command line away from stdout screen into a file.

for example:

Code: Select all

ffmpeg -help > help.txt
That will dump the output from the ffmpeg help command into a file called help.txt
You can then use the readfile function to read the output into a variable.

It's a bit of a workaround but perhaps it works.
OK, so the redirect works, so that's encouraging.

So then how would I execute it and control "where" the command executes (in the current directory would be just fine), and then how do I read it back in and use the first token of that string?

Thanks!

serendipity
Posts: 3360
Joined: 07 May 2007 18:14
Location: NJ/NY

Re: How to execute shell command and read back the result in

Post by serendipity »

If purpose is to read command line output then one can make use of clipboard too with "| clip".
eg:

Code: Select all

ipconfig | clip
saves two steps, creating text file and reading from text file.

avsfan
Posts: 554
Joined: 29 Jun 2006 09:00
Location: Fort Collins, Colorado

Re: How to execute shell command and read back the result in

Post by avsfan »

serendipity wrote:If purpose is to read command line output then one can make use of clipboard too with "| clip".
eg:

Code: Select all

ipconfig | clip
saves two steps, creating text file and reading from text file.
That sounds really promising -- so how do I call that command from a script (or from the addressbar to verify)? (I feel like I should know this, but I don't!)

Thanks!

highend
Posts: 14946
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: How to execute shell command and read back the result in

Post by highend »

Code: Select all

	writefile("D:\test.bat", "ipconfig | clip");
	run """cmd.exe"" /c D:\test.bat", , 2, 0;
	text "<clipboard>";
One of my scripts helped you out? Please donate via Paypal

Marco
Posts: 2354
Joined: 27 Jun 2011 15:20

Re: How to execute shell command and read back the result in

Post by Marco »

Beware that clip:
-is not available in Win previous to 7
-clipboard may be overwritten
Source: http://ss64.com/nt/clip.html
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]

serendipity
Posts: 3360
Joined: 07 May 2007 18:14
Location: NJ/NY

Re: How to execute shell command and read back the result in

Post by serendipity »

@Marco:Thanks for pointing that clip is a incompatible with earlier windows. I did not know that.

@avstefan: I would use 40k's method to be sure that it works on all OSs, if you are absolutely sure you'll be using Win7 or later then clip is fine. If so, something like this will work:

Code: Select all

  run "ipconfig | clip";
  text "<clipboard>";
Note: not all commands that work inside cmd window will work in run command line window. So try this first:

Code: Select all

  run "svn list --verbose myfile";
  text "<clipboard>";
If it doesn't work try highend's method.

avsfan
Posts: 554
Joined: 29 Jun 2006 09:00
Location: Fort Collins, Colorado

Re: How to execute shell command and read back the result in

Post by avsfan »

Thanks -- I've got it working!

I just used trim(...) to get rid of the leading whitespace before using gettoken(...), and then things worked great!

I guess for Windows versions prior to 7, I could just use readfile(...) to get the data, right?

Thanks again!

serendipity
Posts: 3360
Joined: 07 May 2007 18:14
Location: NJ/NY

Re: How to execute shell command and read back the result in

Post by serendipity »

avsfan wrote: I guess for Windows versions prior to 7, I could just use readfile(...) to get the data, right?
Yes, write to a text file and read that file. Basically, a combination of 40k's and highend's script will do the trick.

Post Reply