Sign in to follow this  
Followers 0
iamtheky

Bash On Windows

5 posts in this topic

#1 ·  Posted (edited)

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))

 

Edited by iamtheky

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

Share this post


Link to post
Share on other sites



Diff two files:

#include<array.au3>
#include <WinAPIFiles.au3>
_WinAPI_Wow64EnableWow64FsRedirection(False)

$file = "test_mount_output.txt"
$file2 = "test_mount_output_2.txt"
$outfile = "differences.txt"


$iPID = run("C:\Windows\System32\bash.exe | diff " & $file & " " & $file2 & "> " & $outfile , "" , @SW_HIDE)

Do
   sleep(100)
Until FileExists($outfile)

sleep(100)

processclose($iPID)

$aChanges = stringregexp(fileread($outfile) , ".*\s.*\s---\s>.*?\)" , 3)

_ArrayDisplay($aChanges)

 


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

Share this post


Link to post
Share on other sites

Dump Ntuser.pol to a readable text file on your desktop.

*Still needs cleaning but functional

#include<file.au3>
#include <WinAPIFiles.au3>

_WinAPI_Wow64EnableWow64FsRedirection(False)

$file = "/mnt/c/users/" & @UserName & "/ntuser.pol"
$outfile = "NTuserPol_Copy.txt"

$iPID = run("C:\Windows\System32\bash.exe | cat " & $file & " > /mnt/c/users/" & @UserName & "/desktop/" & $outfile , "" , @SW_HIDE)

While 1
   sleep(100)
      If FileExists("C:\users\" & @UserName & "\desktop\" & $outfile) Then
         ProcessClose($iPID)
         Exit
      EndIf
Wend

 


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

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

This wraps fdupes function so that you end with duplicate file results in the cmd.exe console, though still have to copy/paste it out until someone smart fixes the piping issue.  I have not benchmarked anything, but fdupes is pretty quick.

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

_fdupes("C:\Program Files (x86)\AutoIt3\" , 1)

Func _fdupes($DirToCheckForDupes , $Recurse = 0)
   FileDelete("c:\Windows\Temp\testbashfdupes.txt")
   $ErrFileWrite = $Recurse = 0 ? FileWrite("c:\Windows\Temp\testbashfdupes.txt" , "fdupes .") : FileWrite("c:\Windows\Temp\testbashfdupes.txt" , "fdupes -r .")
   run("cmd /k bash //mnt/c//Windows/Temp/testbashfdupes.txt" , $DirToCheckForDupes)
EndFunc

 

Edited by iamtheky

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

Share this post


Link to post
Share on other sites

Better method of capturing command line without affecting progress

#requireadmin
#include <WinAPIFiles.au3>
#include <Array.au3>
_WinAPI_Wow64EnableWow64FsRedirection(FALSE)
opt("wintitlematchmode" , 2)

_ArrayDisplay( _fdupes("C:\Program Files (x86)\AutoIt3\" , 1) )


Func _fdupes($DirToCheckForDupes , $Recurse = 0)

   FileDelete("c:\Windows\Temp\testbashfdupes.txt")
   $ErrFileWrite = $Recurse = 0 ? FileWrite("c:\Windows\Temp\testbashfdupes.txt" , "fdupes .") : FileWrite("c:\Windows\Temp\testbashfdupes.txt" , "fdupes -r .")
   $iPID = run("cmd /k bash //mnt/c//Windows/Temp/testbashfdupes.txt" , $DirToCheckForDupes)


   ClipPut("")

      Do
         winactivate("cmd.exe")
         winwaitactive("cmd.exe")
         WinSetOnTop("cmd.exe" , "" , 1)
         send("^a" )
         send("{ENTER}")
            sleep(2000)
         consolewrite(stringright(ClipGet() , 1) & @LF)
      Until stringright(ClipGet() , 1) = ">"

   ProcessClose($iPID)

  $aOut = stringsplit(clipget() , @LF , 2)

For $i = ubound($aOut) - 1 to 0 step -1
  If stringleft($aOut[$i] , 2) = "Pr" OR stringleft($aOut[$i] , 2) = "Bu" OR stringleft($aOut[$i] , 2) = "C:"  Then _ArrayDelete($aOut , $i)
Next

  return $aOut

EndFunc

 


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

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
Sign in to follow this  
Followers 0

  • Similar Content

    • iamtheky
      By iamtheky
      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  
    • 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