Thread: Win-bash use

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    71

    Win-bash use

    Hello! Congratulations on the victorious holiday on May 9, a great victory!
    I am modifying the script, and I want it, for the first modification, not to download and unpack gcc-11.1.0, if it already exists.
    I am amazed at the syntax of the bash, and even get stumped.
    1.) The variable local loc_var = 23 is not declared in the second function, although it is local, I expect some human syntax, I cannot understand this.
    I looked for an error twice for an hour and a half, thinking, then closing one line at a time with a grid.
    2.) The sequential execution of the script is interrupted, part of the program is not executed

    Code:
    echo "loc_var"
    echo "\" loc_var \ "outside function = $ loc_var"
    echo "Os"
    
    echo
    echo "\" loc_var \ "outside function = $ loc_var"
                                          # $ loc_var outside function =
                                          # No, $ loc_var not visible globally.
    echo "\" global_var \ "outside function = $ global_var"
                                          # $ global_var outside function = 999
                                          # $ global_var is visible globally.
    
    
    echo
    interrupting my sequential execution, for no reason, as if jumping somewhere, without any logic, I'm shocked, I don't understand at all.



    This is an error message, and it's probably skewed to my second function, sstest2 (), line 62 of code.
    vse - is a message from the first function


    Code:
    vse
    local: =: not a legal variable name
    local: 23: not a legal variable name
    return: bad non-numeric arg `loc_var '
    
    D: \ src>
    How do you first write a function that compares the name of a folder with some text string and outputs a boolean yes or no to the surface?
    From all this I just do not know in which direction to dig!



    In the operating system Windows 7 I am on Win-Bash the script. I will use in a team strip so D: \ Win-Bash \ Bash.exe D: \ SRC \ build-gcc.sh


    Code:
    #! /bin/bash
    
    GCC_VERSION="11.1.0"
    WORKDIR="$HOME/src/"
    INSTALLDIR="/platform"
    
    ## NOTE: XCode must be installed (through App Store) and the following run to install command-line tools.
    ## THIS IS IMPORTANT! Among other things, it creates '/usr/include' and installs the system header files.
    # xcode-select --install
    
    # sudo apt install wget
    # wget https://google.com
    wget -V
    
    
    # get the source code
    cd $WORKDIR
    
    #yourfilenames=`ls ./*.txt`
    yourfilenames=`ls ./`
    for eachfile in $yourfilenames
    do
       echo $eachfile
    done
    
    
    stest(){
    
        local loc_var = 23
        local X="a"
        local Y="b"
        if [ "${X}" = "${Y}" ]; then
            echo "they're the same"
        else
            echo "they're not the same"
        fi
        local nazva=$1
       local bl=0
    
    echo "vse"
    return loc_var
    }
    
    stest
    
    echo "loc_var"
    echo "\"loc_var\" outside function = $loc_var"
    echo "Os"
    
    echo
    echo "\"loc_var\" outside function = $loc_var"
                                          # $loc_var outside function =
                                          # No, $loc_var not visible globally.
    echo "\"global_var\" outside function = $global_var"
                                          # $global_var outside function = 999
                                          # $global_var is visible globally.
    echo
    
      #function_name() {
      sstest2(){
       local loc_var2 = 24
       local loc_var = 23
       #local temp=`ls ./`
       local nazva=$1
       local bl=0
       echo "Ugu"
       for eachfile in $temp
    
    do
       if [ "${eachfile}" = "${nazva}" ]; then
       bl=1
       echo "Aga"
       fi
    done
    return bl
    }
    sstest2
    
    function_name
    
    [  -d  "D:\src\gcc-11.1.0"  ]  &&  echo  "Da. -D:\src\gcc-11.1.0"
    
    ##  ##
    [  !  -d  "D:\src\gcc-11.1.0"  ]  &&  echo  "Net. -D:\src\gcc-11.1.0"
    
    
    #[ $(ls -ld "$ISDIR" | cut -c1) = 'd' ] &&
    #    echo "YES, $ISDIR is a directory." ||
    #    echo "Sorry, $ISDIR is not a directory"
    #    echo "[ -d "$DIR/gcc-${GCC_VERSION}.tar.gz" ]"
    #    echo "&&"
    #    echo "[ -d "$1" ]"
    
    
    #if  ![ -d "$DIR/gcc-${GCC_VERSION}.tar.gz" ]; then
    if  function_name "$DIR/gcc-${GCC_VERSION}.tar.gz" ; then
    wget   https://ftp.gnu.org/gnu/gcc/gcc-${GCC_VERSION}/gcc-${GCC_VERSION}.tar.gz
    # wget   https://ftp.wayne.edu/gnu/gcc/gcc-${GCC_VERSION}/gcc-${GCC_VERSION}.tar.gz
    # gzip -d gcc-${GCC_VERSION}.tar.gz
    #tar --extract gcc-${GCC_VERSION}.tar
    
    
          tar xzvf gcc-${GCC_VERSION}.tar.gz
    elif ! function_name "$DIR/gcc-${GCC_VERSION}"  ; then
        # tar xzf gcc-${GCC_VERSION}.tar.gz
          gzip -d gcc-${GCC_VERSION}.tar.gz
        # tar -tf gcc-${GCC_VERSION}.tar
        # tar -xv gcc-${GCC_VERSION}.tar
          tar -xvf gcc-${GCC_VERSION}.tar
    fi
    
    # download the prerequisites
    cd gcc-${GCC_VERSION}
    ./contrib/download_prerequisites
    
    # create the build directory
    cd ..
    mkdir gcc-build
    cd gcc-build
    
    # build
    ../gcc-${GCC_VERSION}/configure                      \
        --prefix=${INSTALLDIR}                           \
        --enable-shared                                  \
        --enable-threads=posix                           \
        --enable-__cxa_atexit                            \
        --enable-clocale=gnu                             \
        --enable-languages=all                           \
    && make \
    && make install
    
    # Notes
    #
    #   --enable-shared --enable-threads=posix --enable-__cxa_atexit:
    #       These parameters are required to build the C++ libraries to published standards.
    #
    #   --enable-clocale=gnu:
    #       This parameter is a failsafe for incomplete locale data.
    #
    #   --disable-multilib:
    #       This parameter ensures that files are created for the specific
    #       architecture of your computer.
    #        This will disable building 32-bit support on 64-bit systems where the
    #        32 bit version of libc is not installed and you do not want to go
    #        through the trouble of building it. Diagnosis: "Compiler build fails
    #        with fatal error: gnu/stubs-32.h: No such file or directory"
    #
    #   --with-system-zlib:
    #       Uses the system zlib instead of the bundled one. zlib is used for
    #       compressing and uncompressing GCC's intermediate language in LTO (Link
    #       Time Optimization) object files.
    #
    #   --enable-languages=all
    #   --enable-languages=c,c++,fortran,go,objc,obj-c++:
    #       This command identifies which languages to build. You may modify this
    #       command to remove undesired language

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,002
    You do not have a function called "function_name" in the code you posted.
    Yet you call it several times; fix this and then retry!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Feb 2015
    Posts
    71
    Yes, I think I removed the excess. Here are the errors when running the script

    Code:
    D:\src>D:\win-bash\bash.exe  D:\src\build-gcc.sh
    they're not the same
    vse
    local: =: not a legal variable name
    local: 23: not a legal variable name
    return: bad non-numeric arg `loc_var'
    
    D:\src>

    Code:
    #! /bin/bash
    
    GCC_VERSION="11.1.0"
    WORKDIR="$HOME/src/"
    INSTALLDIR="/platform"
    
    ## NOTE: XCode must be installed (through App Store) and the following run to install command-line tools.
    ## THIS IS IMPORTANT! Among other things, it creates '/usr/include' and installs the system header files.
    # xcode-select --install
    
    # sudo apt install wget
    # wget https://google.com
    
    # get the source code
    cd $WORKDIR
    
    
    
    stest(){
    
        local loc_var = 23
        local X="a"
        local Y="b"
        if [ "${X}" = "${Y}" ]; then
            echo "they're the same"
        else
            echo "they're not the same"
        fi
        local nazva=$1
       local bl=0
    
    echo "vse"
    return loc_var
    }
    
    stest
    
    echo "loc_var = $loc_var"
    
    
      function_name() {
       local loc_var2 = 24
       local loc_var = 23
       #local temp=`ls ./`
       local nazva=$1
       local bl=0
       echo "Ugu"
       for eachfile in $temp
    
    do
       if [ "${eachfile}" = "${nazva}" ]; then
       bl=1
       echo "Aga"
       fi
    done
    return bl
    }
    
    
    if  ! [function_name "$DIR/gcc-${GCC_VERSION}.tar.gz" ]; then
       wget   https://ftp.gnu.org/gnu/gcc/gcc-${GCC_VERSION}/gcc-${GCC_VERSION}.tar.gz
       tar xzvf gcc-${GCC_VERSION}.tar.gz
    elif ! function_name "$DIR/gcc-${GCC_VERSION}"  ; then
       gzip -d gcc-${GCC_VERSION}.tar.gz
       tar -xvf gcc-${GCC_VERSION}.tar
    fi
    
    
    
    # download the prerequisites
    cd gcc-${GCC_VERSION}
    ./contrib/download_prerequisites
    
    # create the build directory
    cd ..
    mkdir gcc-build
    cd gcc-build
    
    # build
    ../gcc-${GCC_VERSION}/configure                      \
        --prefix=${INSTALLDIR}                           \
        --enable-shared                                  \
        --enable-threads=posix                           \
        --enable-__cxa_atexit                            \
        --enable-clocale=gnu                             \
        --enable-languages=all                           \
    && make \
    && make install
    
    # Notes
    #
    #   --enable-shared --enable-threads=posix --enable-__cxa_atexit:
    #       These parameters are required to build the C++ libraries to published standards.
    #
    #   --enable-clocale=gnu:
    #       This parameter is a failsafe for incomplete locale data.
    #
    #   --disable-multilib:
    #       This parameter ensures that files are created for the specific
    #       architecture of your computer.
    #        This will disable building 32-bit support on 64-bit systems where the
    #        32 bit version of libc is not installed and you do not want to go
    #        through the trouble of building it. Diagnosis: "Compiler build fails
    #        with fatal error: gnu/stubs-32.h: No such file or directory"
    #
    #   --with-system-zlib:
    #       Uses the system zlib instead of the bundled one. zlib is used for
    #       compressing and uncompressing GCC's intermediate language in LTO (Link
    #       Time Optimization) object files.
    #
    #   --enable-languages=all
    #   --enable-languages=c,c++,fortran,go,objc,obj-c++:
    #       This command identifies which languages to build. You may modify this
    #       command to remove undesired language

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,002
    I believe but do not know that local must be used only inside a bash function.

    Edit: Just relooked at your code and each local appears to be inside a function.

    Why don't you use someone else's code and modify it to do what you want?

    Edit2: Link to someone code A shell script to download packages for, configure, build and install a GCC cross-compiler. * GitHub

    Tim S.
    Last edited by stahta01; 4 Weeks Ago at 12:50 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Feb 2015
    Posts
    71
    Thank you! I am glad to link, in fact this is what I wanted to do (but not working).


    So I exactly and expected that Local should be used only inside the Bash function.
    I use inside, only inside the Bash function. I really expected that this variable nowhere can be seen from the outside.
    It seems that it is not. This is amazing.

    So I don't understand something in Bash syntax, or I made syntax errors, but I do not see them.


    The code for the link the same gives me a lot of errors, and will need a lot of corrections.

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    850
    Another thing: This is not valid:
    Code:
    $ x = 1
    x: command not found
    Assignments must not have spaces:
    Code:
    $ x=1
    $ echo $x
    1

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bash to cpp
    By setevoy in forum C++ Programming
    Replies: 4
    Last Post: 07-21-2014, 03:59 AM
  2. Bash troubles
    By Annonymous in forum Linux Programming
    Replies: 1
    Last Post: 09-24-2012, 01:09 AM
  3. Can't run Bash!
    By Yarin in forum Tech Board
    Replies: 3
    Last Post: 05-11-2009, 07:25 PM
  4. What company to bash..?
    By h_howee in forum A Brief History of Cprogramming.com
    Replies: 74
    Last Post: 02-03-2008, 01:13 PM
  5. BASH scripting.
    By Kennedy in forum Tech Board
    Replies: 6
    Last Post: 05-06-2007, 06:13 PM

Tags for this Thread