Jump to content

write/execute/return output from bash


iamtheky
 Share

Recommended Posts

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

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

Link to comment
Share on other sites

  • 2 weeks later...

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

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

Link to comment
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 ?

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

  • Moderators

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

  • 10 months later...

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

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

Link to comment
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
 Share

×
×
  • Create New...