iamtheky

write/execute/return output from bash

5 posts in this topic

#1 ·  Posted (edited)

This thread will focus on the scripts that write/execute bash scripts, and return the data (whereas my other thread is more just a spit sink).  Currently you are relegated to piping to a file from the bash script, hopefully there are other options coming.

This AutoIt script writes a bash script containing an fdupes command based off the Directory you want to search, and whether or not you want it to recurse.  It writes the script in Temp, runs it, writes the stdout to a file in Temp, then parses that file and returns you an array.

* You need to sudo apt-get install fdupes for this to work, btw.  fdupes is stupid fast at what it does and being able to leverage linux features, not hunt down a windows equivalent, is great fun.

#requireadmin
#include <WinAPIFiles.au3>
#include <Array.au3>
_WinAPI_Wow64EnableWow64FsRedirection(FALSE)

_ArrayDisplay(_fdupes("c:\Users\" & @UserName & "\Desktop" , 1))

Func _fdupes($DirToCheckForDupes , $Recurse = 0)

$bashscript = "c:\Windows\Temp\PRODfdupes.txt"
$bashoutput = "c:\Windows\Temp\PRODfdupes_STDOUT.txt"

;format Windows Dir name to linux Dir name - DUPESDIR
   $aDupesDir = stringsplit($DirToCheckForDupes , "\" , 2)
   $sDupesDir = "//mnt/" & stringleft($DirToCheckForDupes , 1)
      for $i = 1 to ubound($aDupesDir) - 1
         $sDupesDir &= "/" & $aDupesDir[$i]
      Next

;format Windows Dir name to linux Dir name - ScriptDIR
   $aScriptDir = stringsplit($bashscript , "\" , 2)
   $sScriptDir = "//mnt/" & stringleft($bashscript , 1)
      for $i = 1 to ubound($aScriptDir) - 1
         $sScriptDir &= "/" & $aScriptDir[$i]
      Next

;format Windows Dir name to linux Dir name - OUTDIR
   $aOutDir = stringsplit($bashoutput , "\" , 2)
   $sOutDir = "//mnt/" & stringleft($bashoutput , 1)
      for $i = 1 to ubound($aOutDir) - 1
         $sOutDir &= "/" & $aOutDir[$i]
      Next


   FileDelete($bashscript)
   FileDelete($bashoutput)

   $ErrFileWrite = $Recurse = 0 ? FileWrite($bashscript , "fdupes " & $sDupesDir & " > " & $sOutDir) : FileWrite($bashscript , "fdupes -R " & $sDupesDir & " > " & $sOutDir)

   $iPID = runwait('cmd /c bash ' & $sScriptDir)

   $Finalarr = stringsplit(fileread($bashoutput) , chr("10") , 2)

   return $Finalarr


EndFunc ;fdupes

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Similar to fdupes, but much faster in my testing (1.5GB in 1.5 seconds!!).

RDFIND

#requireadmin
#include <WinAPIFiles.au3>
#include <Array.au3>
_WinAPI_Wow64EnableWow64FsRedirection(FALSE)

_ArrayDisplay(_rdfind("c:\Users\" & @UserName & "\Desktop"))






Func _rdfind($DirToCheckForDupes) ;rdfind

$bashscript = "c:\Windows\Temp\PRODrdfind.txt"
$bashoutput = "c:\Windows\Temp\PRODrdfind_STDOUT.txt"

;format Windows Dir name to linux Dir name - DUPESDIR
   $aDupesDir = stringsplit($DirToCheckForDupes , "\" , 2)
   $sDupesDir = "//mnt/" & stringleft($DirToCheckForDupes , 1)
      for $i = 1 to ubound($aDupesDir) - 1
         $sDupesDir &= "/" & $aDupesDir[$i]
      Next

;format Windows Dir name to linux Dir name - ScriptDIR
   $aScriptDir = stringsplit($bashscript , "\" , 2)
   $sScriptDir = "//mnt/" & stringleft($bashscript , 1)
      for $i = 1 to ubound($aScriptDir) - 1
         $sScriptDir &= "/" & $aScriptDir[$i]
      Next

;format Windows Dir name to linux Dir name - OUTDIR
   $aOutDir = stringsplit($bashoutput , "\" , 2)
   $sOutDir = "//mnt/" & stringleft($bashoutput , 1)
      for $i = 1 to ubound($aOutDir) - 1
         $sOutDir &= "/" & $aOutDir[$i]
      Next


   FileDelete($bashscript)
   FileDelete($bashoutput)

   $ErrFileWrite = FileWrite($bashscript , "rdfind " & $sDupesDir & " -outputname " & $sOutDir)

   $iPID = runwait('cmd /c bash ' & $sScriptDir)

   $Finalarr = stringsplit(fileread($bashoutput) , chr("10") , 2)

   return $Finalarr


EndFunc ;rdfind

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites
C:\Users\Owner>sudo apt-get install fdupes
'sudo' is not recognized as an internal or external command,
operable program or batch file.

why "c:\Users\" & @UserName & "\Desktop", why not just @Desktop and why c:\Windows\Temp, why not @Temp 
How can I get bash for windows ? Or is this something you'd use in *nix environment ?

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

ruby regex evaluation via the puts command, returning both position of match and the match. 

*requires both the Linux subsystem and that you sudo apt-get install ruby first.

#include<WinAPIFiles.au3>

_WinAPI_Wow64EnableWow64FsRedirection(FALSE)

$proc = runwait('c:\Windows\System32\cmd.exe /c "bash testruby.txt" > testout.txt')

msgbox(0, '' , stringregexp(fileread('testout.txt') , "(\A\d+?)" , 3)[0] & @CR & stringregexpreplace(fileread('testout.txt') , "(\A\d+?)" , ""))

testruby.txt

ruby -e 'puts "IraIvanJeffJohnJoshKenKory"=~/(J\D+?)K/,$1'

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Similar Content

    • DLS
      By DLS
      I have a working script that changes the core affinity and process priority of multiples of a specific application I have running.  
      I have that part figured out.

      I would like to make a little modification to it.

      Windows10 introduced virtual desktops.  I am trying to have different core affinity and priority of processes on the visible and non-visible desktops.  
      What I need is a bool function that could be described as IsWindowOnCurrentDesktop($hWnd).  I have searched the winAPI.au3 but I do not believe anything like that exists built it.
      I have searched multiple places before asking for help. I found a Microsoft supplied example of the function I need using C#, but I am unfamiliar with C# to a degree that I cannot port the system call over.
      https://blogs.msdn.microsoft.com/winsdk/2015/09/10/virtual-desktop-switching-in-windows-10/
      Help is appreciated but not expected.  Thanks in advanced.   In the meantime I will be learning C# syntax and class structure.
    • Skysnake
      By Skysnake
      Hi
      I need help.
      I have re-installed AutoIt, but the problem persists.
      My script uses an #include <tourney_mymain.a3x> which Scite fails to read.  To be clear, as long as it is in .au3 format, it works.  The moment I build it as .a3x, it fails.
      ;#include <tourney_mymain.a3x> #include <tourney_mymain.au3> _MyMainFunc() The file tourney_mymain.au3 exists in the same folder, and the header looks like this
      #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Outfile_type=a3x #AutoIt3Wrapper_Outfile=C:\AutoIt\tourney\tourney_mymain.a3x #AutoIt3Wrapper_Run_AU3Check=n #AutoIt3Wrapper_Run_Tidy=y #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** The error looks like this:
      >"C:\Program Files (x86)\AutoIt3\SciTE\..\AutoIt3.exe" "C:\Program Files (x86)\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.au3" /run /prod /ErrorStdOut /in "C:\AutoIt\tourney\tourney.au3" /UserParams +>16:13:57 Starting AutoIt3Wrapper v.16.306.1237.0 SciTE v.3.6.2.0 Keyboard:00000409 OS:WIN_10/ CPU:X64 OS:X64 Environment(Language:0409) CodePage:0 utf8.auto.check:4 # detect ascii high characters and if none found set default encoding to UTF8 and do not add BOM +> SciTEDir => C:\Program Files (x86)\AutoIt3\SciTE UserDir => C:\Users\user\AppData\Local\AutoIt v3\SciTE\AutoIt3Wrapper SCITE_USERHOME => C:\Users\user\AppData\Local\AutoIt v3\SciTE >Running Tidy (16.306.1237.0) from:C:\Program Files (x86)\AutoIt3\SciTE\tidy +>16:13:57 Tidy ended.rc:0 >Running AU3Check (3.3.14.2) from:C:\Program Files (x86)\AutoIt3 input:C:\AutoIt\tourney\tourney.au3 "C:\AutoIt\tourney\tourney.au3"(55,13) : error: _MyMainFunc(): undefined function. _MyMainFunc() ~~~~~~~~~~~~^ C:\AutoIt\tourney\tourney.au3 - 1 error(s), 0 warning(s) !>16:13:57 AU3Check ended. Press F4 to jump to next error.rc:2 +>16:13:57 AutoIt3Wrapper Finished. >Exit code: 2 Time: 1.028 When I toggle back to the .au3 file and compile/run, it works.
      >"C:\Program Files (x86)\AutoIt3\SciTE\..\AutoIt3.exe" "C:\Program Files (x86)\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.au3" /run /prod /ErrorStdOut /in "C:\AutoIt\tourney\tourney.au3" /UserParams +>16:28:05 Starting AutoIt3Wrapper v.16.306.1237.0 SciTE v.3.6.2.0 Keyboard:00000409 OS:WIN_10/ CPU:X64 OS:X64 Environment(Language:0409) CodePage:0 utf8.auto.check:4 # detect ascii high characters and if none found set default encoding to UTF8 and do not add BOM +> SciTEDir => C:\Program Files (x86)\AutoIt3\SciTE UserDir => C:\Users\user\AppData\Local\AutoIt v3\SciTE\AutoIt3Wrapper SCITE_USERHOME => C:\Users\user\AppData\Local\AutoIt v3\SciTE >Running Tidy (16.306.1237.0) from:C:\Program Files (x86)\AutoIt3\SciTE\tidy +>16:28:06 Tidy ended.rc:0 >Running AU3Check (3.3.14.2) from:C:\Program Files (x86)\AutoIt3 input:C:\AutoIt\tourney\tourney.au3 +>16:28:06 AU3Check ended.rc:0 >Running:(3.3.14.2):C:\Program Files (x86)\AutoIt3\autoit3.exe "C:\AutoIt\tourney\tourney.au3" --> Press Ctrl+Alt+Break to Restart or Ctrl+Break to Stop I was here! +>16:28:06 AutoIt3.exe ended.rc:0 +>16:28:06 AutoIt3Wrapper Finished. >Exit code: 0 Time: 1.066 Building the .a3x file, returns this:
      >"C:\Program Files (x86)\AutoIt3\SciTE\..\AutoIt3.exe" "C:\Program Files (x86)\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.au3" /NoStatus /prod /in "C:\AutoIt\tourney\tourney_mymain.au3" +>16:29:01 Starting AutoIt3Wrapper v.16.306.1237.0 SciTE v.3.6.2.0 Keyboard:00000409 OS:WIN_10/ CPU:X64 OS:X64 Environment(Language:0409) CodePage:0 utf8.auto.check:4 # detect ascii high characters and if none found set default encoding to UTF8 and do not add BOM +> SciTEDir => C:\Program Files (x86)\AutoIt3\SciTE UserDir => C:\Users\user\AppData\Local\AutoIt v3\SciTE\AutoIt3Wrapper SCITE_USERHOME => C:\Users\user\AppData\Local\AutoIt v3\SciTE >Running Tidy (16.306.1237.0) from:C:\Program Files (x86)\AutoIt3\SciTE\tidy +>16:29:01 Tidy ended.rc:0 >Running:(3.3.14.2):C:\Program Files (x86)\AutoIt3\aut2exe\aut2exe.exe /in "C:\AutoIt\tourney\tourney_mymain.au3" /out "C:\Users\user\AppData\Local\AutoIt v3\Aut2exe\~AUDA33.tmp.a3x" /nopack /comp 2 +>16:29:02 Aut2exe.exe ended.C:\Users\user\AppData\Local\AutoIt v3\Aut2exe\~AUDA33.tmp.a3x. rc:0 +>16:29:02 Created program:C:\AutoIt\tourney\tourney_mymain.a3x +>16:29:02 AutoIt3Wrapper Finished. >Exit code: 0 Time: 1.718 And the header of the  contents of the .a3x file looks like this:
      £HK¾˜lJ©™LS †ÖH}AU3!EA06M¨ÿs$§<öz�ñg¬Á“çkCÊR¦­ á»:!¥)ãìç�˜.@½ášÞ€F±�k;!Ô±Öu:È=ÆÐ3÷�¯Ë�¢”���ˆþd•aç¶M�ø ,p~©=�ã™Õˆ ¬C�9˜�8öþ8_Yr©0h�d It looks like this in pure AutoIt:
      Func _MainFunc() ConsoleWrite("I was here!" & @CRLF) Local $sMainstatus = 'The database is empty' $Main = GUICreate("Roster", 640, 480) ; --- Top level menu Global $idHelpmenu = GUICtrlCreateMenu(" Help ") I have a similar AutoIT installation running on Windows 7, no problem.  The machine giving problems is Win 10.
      Any ideas would be appreciated.  This is a new pet project of mine and this is just the start, so I am willing to zip ALL of it and send it along if anyone wants to see inside.
      Thanks.
      Skysnake
    • iamtheky
      By iamtheky
      This thread will house the scripts i (or anyone who contributes) create while playing with Bash on Windows.
      First up, a simple mount command.  I am still trying to get the stdout, but piping to files will suffice for now.  I am running Win 10 Build 14342.rs1_release.160506-1708
      **Do -- Until will be cleaned up (or completely scrapped) when i can find a reliable way to deal with output.
       
       
      #include<array.au3> #include <WinAPIFiles.au3> _WinAPI_Wow64EnableWow64FsRedirection(False) $outfile = "test_mount_output.txt" $iPID = run("C:\Windows\System32\bash.exe | mount > " & $outfile , "" , @SW_HIDE) Do sleep(100) Until FileExists($outfile) sleep(100) processclose($iPID) _ArrayDisplay(stringsplit(stringtrimright(fileread($outfile) , 1) , @LF , 2))  
    • tremolux66
      By tremolux66
      Before I dive into the specifics of my problem, are there known, fatal issues when running a compiled AutoIt script (.exe file) from a Cygwin Bash script? Am I wasting my time trying to get it to work?
      If not, then here's my problem: I have 2 compiled AU3 scripts that are normally executed in the background by a Windows service. The Start script is executed by the service with a directory path as a command-line argument, does some processing, and creates a database record. After an external (human) activity finishes, the Stop script is executed, does some more processing, and updates the database record for the directory. The Bash script emulates the service for purposes of re-processing directories that had errors during the "live" pass: it loops through a text file containing a list of failed directories and executes the Start and Stop scripts.
      If the AU3 script executions are commented-out (e.g., to test other logic in the loop), the Bash script reads through the entire directory-list file; the Start and Stop scripts are treated as if they had run successfully. But if the AU3 scripts are actually executed, the loop exits after the first full iteration (i.e., without skips caused by error-detection).
      Subsequent runs of the Bash script process one directory each and exit (the list file shrinks with each pass). It seems like something about the execution of the AU3 scripts is changing the Bash loop execution (e.g., causing it to see end-of-file for the directory list file). A puzzling aspect is that an earlier version of the Bash script executed successfully (all directories processed), but in that case the directory list was taken from a Bash array variable rather than a file. Since there could be hundreds (or more) directories requiring re-processing, I modified the Bash script to read from a file, with the intention of making it more robust.
      Any suggestions for troubleshooting this will be welcome.
      Outline of the script; the actual script is much longer:
      #!/bin/bash # Initialize variables... export START_EXEPATH="C:/mydir/StartScript.exe" export STOP_EXEPATH="C:/mydir/StopScript.exe" # Generate (unix) text file containing (unix) directory paths, one per line find C:/somedir -type d [some other criteria...] -print >/tmp/dir_list.out # Core code: let errcnt=0 while IFS='' read -r dpath do # Validate the last component of dpath... dpath_base="$(basename "$dpath")" regexp="some-extended-regexp" if [[ "$dpath_base" =~ $regexp ]]; then dpath_bad=0; else dpath_bad=1; fi if [ $dpath_bad -eq 1 ]; then let errcnt+=1; echo "error message"; continue; fi win_dpath="$(cygpath -a -w "$dpath")" # Run 1st AutoIt script "$START_EXEPATH" "$win_dpath" "another-argument" if [ $? -ne 0 ]; then let errcnt+=1; echo "error message"; continue; fi # Run 2nd AutoIt script "$STOP_EXEPATH" "$win_dpath" if [ $? -ne 0 ]; then let errcnt+=1; echo "error message"; fi done </tmp/dir_list.out echo "$errcnt errors" if [ $errcnt -ne 0 ]; then exit 2; fi exit 0  
    • SorryButImaNewbie
      By SorryButImaNewbie
      Hi, long time since my last spot!
      I'm not sure if I should ask this in GH or here, but as I saw from the downloads my fav scripting language isnt yet compatible with win10? I can understand it, its a "fresh" thing (I try to say tht I'm not suprised, I only have a few questions about this)
      1
      If autoit will be comaptible with win10 I will be able to write scripts to older versions of windows right? Testing them would be pretty tricky, and I would like to test my script in the envirement they will run, but i could code in win10 then take my script to a win 7 or vista and use it (considering that of course i wrote the script to run on vista/win 8 for example), right?
      2
      When can we expect autoit to be running in win10? I see that last update was in July, and I'm not sure how much of a hassle it is to make it run on the new OS, but I would like to know if there is something of a notion of a compatable version (like next 2-8 months, or something)
      Thank you for your help, and keep up the good work!