Sign in to follow this  
Followers 0
DigitalFacade82

Assistance with parsing large no of bash scripts through Windows cleanse? Works in cmd

1 post in this topic

Hi,

I am fairly new here at AutoIT forums. I have read a bit in here and been a serial lurker for quite sometime now. I am still not confident with all of the AutoIT stuff but have a general understanding of Windows command line which has helped.

The situation I am in is that I am currently using Cygwin for development work in Android. I use this to connect to Elcipse and Github. I had a full functioning Cygwin up until I merged in the WinGIT install for Cygwin/Ming that totally r*%^ed things (excuse the pun) badly, namely because idiots don't know how to clean their scripts before uploading them saying that they work in Cygwinshell for Windows when quite obviously they wrote them in Bash shell in Linux and just "thought" that they would work. A massive problem is line endings between Windows and Linux, and that is no problem if you set your editing environment up correctly to begin with. Anyway that cannot be solved now. What I usually do to bypass this is run some scripts to clean the bash script (be it an executable bash script or .sh it doesn't really matter).

Some backgroung here are the scripts that I run normally to complete the task from within Command prompt (yes it works from command prompt so it should work in AutoIT right?)
Note that the AutoIT script will be a Binary executable (compiled) and be located in the Cygwin bin directory.

This is just a sample/example of a cmd script named clean.cmd

@echo off
setlocal
 
if not exist "%~dpn0.sh" echo Script "%~dpn0.sh" not found & exit 2
 
set _CYGBIN=C:\cygwin\bin
if not exist "%_CYGBIN%" echo Couldn't find Cygwin at "%_CYGBIN%" & exit 3
 
:: Resolve ___.sh to /cygdrive based *nix path and store in %_CYGSCRIPT%
for /f "delims=" %%A in ('%_CYGBIN%\cygpath.exe "%~dpn0.sh"') do set _CYGSCRIPT=%%A
 
:: Throw away temporary env vars and invoke script, passing any args that were passed to us
endlocal & %_CYGBIN%\bash --login "%_CYGSCRIPT%" %*
pause
ren initialize.sh initialize_backup
ren initialize_new.sh initialize.sh
exit

Situated in the same directory directly next to the cmd script is a bash script with the same name clean.sh
Note: I have since learned that most executables for Cygwin can actually be executed from the command line in Command prompt and especially easy if they are run from within the bin directory! :)

clean.sh

#!/bin/sh
echo "Cleaning bash scripts"
cat initialize.sh | sed '/\015/d' >initialize_new.sh

Because these guys that made the WinGIT right royally screwed my clean working Cygwin with all of stuff working and I do need to have GIT working aswell I kind of need to pass all the bash scripts through the cleaning mill. That was ok, I started with the first one initialize and then that worked so I went on to the next one, and then I thought? "Hey how many bash scripts are there in Cygwin anyway?" And then I nearly fainted :( .....after doing a *.sh search in the windows search bar.

I have attempted to use one of the forum members recursive functions to search and seek all bash scripts within a specified directory and search all folders amd sub folders for any files with file extension .sh and store these into an array and then feed each of these files in command line cleansing function.

Here is what I have so far:

 

; ----------------------------------------------------------------------------
;
; AutoIt Version: 1.0
; Author: Digital Facade82
;
; Script Function: To groom my ruined Cygwin install and for ever more clean it of its dirty line endings
;
; ----------------------------------------------------------------------------


$Title = "SearchSHclean"

#include <GUIConstantsEx.au3>
#include <File.au3>

#cs ----------------------------------------------------------------------------
    AutoIt Version: 3.2.10.0
    Author: WeaponX
    Updated: 2/21/08
    Script Function: Recursive file search

    2/21/08 - Added pattern for folder matching, flag for return type
    1/24/08 - Recursion is now optional

    Parameters:

    RFSstartdir: Path to starting folder

    RFSFilepattern: RegEx pattern to match
    "\.(mp3)" - Find all mp3 files - case sensitive (by default)
    "(?i)\.(mp3)" - Find all mp3 files - case insensitive
    "(?-i)\.(mp3|txt)" - Find all mp3 and txt files - case sensitive

    RFSFolderpattern:
    "(Music|Movies)" - Only match folders named Music or Movies - case sensitive (by default)
    "(?i)(Music|Movies)" - Only match folders named Music or Movies - case insensitive
    "(?!(Music|Movies)\:)\b.+" - Match folders NOT named Music or Movies - case sensitive (by default)

    RFSFlag: Specifies what is returned in the array
    0 - Files and folders
    1 - Files only
    2 - Folders only

    RFSrecurse: TRUE = Recursive, FALSE = Non-recursive

    RFSdepth: Internal use only

#ce ----------------------------------------------------------------------------

Global $pathentered1 = FileSelectFolder("Please select file Input location", "")
Global $RFSstartdir = $pathentered1 & "\"
Global $RFSFilepattern = ".sh"

Func RecursiveFileSearch($RFSstartdir, $RFSFilepattern = ".sh", $RFSFolderpattern = "*.*", $RFSFlag = 1, $RFSrecurse = True, $RFSdepth = 0)

    ;Ensure starting folder has a trailing slash
    If StringRight($RFSstartdir, 1) <> "\" Then $RFSstartdir &= "\"

    If $RFSdepth = 0 Then
        ;Get count of all files in subfolders for initial array definition
        $RFSfilecount = DirGetSize($RFSstartdir, 1)

        ;File count + folder count (will be resized when the function returns)
        Global $RFSarray[$RFSfilecount[1] + $RFSfilecount[2] + 1]
    EndIf

    $RFSsearch = FileFindFirstFile($RFSstartdir & "*.*")
    If @error Then Return

    ;Search through all files and folders in directory
    While 1
        $RFSnext = FileFindNextFile($RFSsearch)
        If @error Then ExitLoop

        ;If folder and recurse flag is set and regex matches
        If StringInStr(FileGetAttrib($RFSstartdir & $RFSnext), "D") Then

            If $RFSrecurse And StringRegExp($RFSnext, $RFSFolderpattern, 0) Then
                RecursiveFileSearch($RFSstartdir & $RFSnext, $RFSFilepattern, $RFSFolderpattern, $RFSFlag, $RFSrecurse, $RFSdepth + 1)
                If $RFSFlag <> 1 Then
                    ;Append folder name to array
                    $RFSarray[$RFSarray[0] + 1] = $RFSstartdir & $RFSnext
                    $RFSarray[0] += 1
                EndIf
            EndIf
        ElseIf StringRegExp($RFSnext, $RFSFilepattern, 0) And $RFSFlag <> 2 Then
            ;Append file name to array
            $RFSarray[$RFSarray[0] + 1] = $RFSstartdir & $RFSnext
            $RFSarray[0] += 1
        EndIf
    WEnd
    FileClose($RFSsearch)

    If $RFSdepth = 0 Then
        ReDim $RFSarray[$RFSarray[0] + 1]
        Return $RFSarray
    EndIf
EndFunc   ;==>RecursiveFileSearch

Func Clean($RFSarray)
    Local $sfile, $nfile, $rnj, $sCommand

    $rnj = FileFindFirstFile($RFSarray)
    While 1
        $sfile = FileFindNextFile($rnj)
        $nfile = StringTrimRight($sfile, 4)
        If StringRight($sfile, 4) == $RFSFilepattern Then
            If $nfile = -1 Then
                MsgBox(64, "File does not exist", "Sorry the extension" & $RFSFilepattern & "does not exist" & @CRLF & "Please choose a different location")
                Exit
            EndIf
            $sCommand = 'cat ' & $sfile & '|' & 'sed ' & '/\015/d ' & '> ' & $nfile
            Run(@ComSpec & " /c " & $sCommand, $pathentered1, @SW_HIDE, 0x8)
            FileClose($nfile)
        EndIf
    WEnd
    Exit
EndFunc   ;==>Clean
Clean($RFSarray)

What seems to be my sticking point is being able to pass the Array variable to the next function to do the actual work? 

This is my first script posted here, so go easy on me. I have a great deal of respect for all the 'greats' on this forum and their Wisdom. I hope to someday be as great as they are:)

Thanks in advance


J.

 

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