Bash script fails where command line works2019 Community Moderator ElectionHow can I run a “grep | grep” command as a string in a bash function?Howto run interactive commands, as another useraliasing a slightly complex script on Linux/bashGetting wrong $LINENO for a trapped functionHow can I run a bash variable as a command exactly without additional quotation?functions argumentsBash script inheritance? Calling a function from another script?How is a bash script executed via its filename as command name with and without shebang?Correct way of building variable length argument line to external command in bashCorrectly Escape echo -ne

Multi tool use
Multi tool use

How would you translate "more" for use as an interface button?

Language involving irrational number is not a CFL

Unable to disable Microsoft Store in domain environment

What does "Scientists rise up against statistical significance" mean? (Comment in Nature)

Anime with legendary swords made from talismans and a man who could change them with a shattered body

How do you justify more code being written by following clean code practices?

How to reduce predictors the right way for a logistic regression model

If Captain Marvel (MCU) were to have a child with a human male, would the child be human or Kree?

What is this high flying aircraft over Pennsylvania?

How to preserve electronics (computers, iPads and phones) for hundreds of years

Giving feedback to someone without sounding prejudiced

Did I make a mistake by ccing email to boss to others?

Can I cause damage to electrical appliances by unplugging them when they are turned on?

The Digit Triangles

What does "tick" mean in this sentence?

Is there anyway, I can have two passwords for my wi-fi

Visualizing the difference curve in a 2D plot?

Why the "ls" command is showing the permissions of files in a FAT32 partition?

El Dorado Word Puzzle II: Videogame Edition

Proving an identity involving cross products and coplanar vectors

Why didn't Voldemort know what Grindelwald looked like?

Make a Bowl of Alphabet Soup

Sound waves in different octaves

Does the Crossbow Expert feat's extra crossbow attack work with the reaction attack from a Hunter ranger's Giant Killer feature?



Bash script fails where command line works



2019 Community Moderator ElectionHow can I run a “grep | grep” command as a string in a bash function?Howto run interactive commands, as another useraliasing a slightly complex script on Linux/bashGetting wrong $LINENO for a trapped functionHow can I run a bash variable as a command exactly without additional quotation?functions argumentsBash script inheritance? Calling a function from another script?How is a bash script executed via its filename as command name with and without shebang?Correct way of building variable length argument line to external command in bashCorrectly Escape echo -ne










0















I wrote a basic bash function to test for the occurence of a string search argument in another string. Running it from the command line gives expected results. Running the same from a shell script fails. Can somebody show me the errors of my ways?



me@mylaptop $ line='someidentifier 123 another identifier 789 065 theend'
me@mylaptop $ sarg='+([0-9])+([ ])another identifier'
me@mylaptop $ type strstr
strstr is a function
strstr ()

[ "$1#*$2*" = "$1" ] && return 1;
return 0

me@mylaptop $ strstr "$line" "$sarg" ; echo "$?"
0
me@mylaptop $


My script:



me@mylaptop $ cat l.sh
#!/bin/bash

function strstr ()

[ "$1#*$2*" = "$1" ] && return 1;
return 0


line='someidentifier 123 another identifier 789 065 theend'
sarg='+([0-9])+([ ])another identifier'

echo '==='"$line"'==='

echo '==='"$sarg"'==='

strstr "$line" "$sarg"

echo "$?"

me@mylaptop $ ./l.sh
===someidentifier 123 another identifier 789 065 theend===
===+([0-9])+([ ])another identifier===
1
me@mylaptop $









share|improve this question




























    0















    I wrote a basic bash function to test for the occurence of a string search argument in another string. Running it from the command line gives expected results. Running the same from a shell script fails. Can somebody show me the errors of my ways?



    me@mylaptop $ line='someidentifier 123 another identifier 789 065 theend'
    me@mylaptop $ sarg='+([0-9])+([ ])another identifier'
    me@mylaptop $ type strstr
    strstr is a function
    strstr ()

    [ "$1#*$2*" = "$1" ] && return 1;
    return 0

    me@mylaptop $ strstr "$line" "$sarg" ; echo "$?"
    0
    me@mylaptop $


    My script:



    me@mylaptop $ cat l.sh
    #!/bin/bash

    function strstr ()

    [ "$1#*$2*" = "$1" ] && return 1;
    return 0


    line='someidentifier 123 another identifier 789 065 theend'
    sarg='+([0-9])+([ ])another identifier'

    echo '==='"$line"'==='

    echo '==='"$sarg"'==='

    strstr "$line" "$sarg"

    echo "$?"

    me@mylaptop $ ./l.sh
    ===someidentifier 123 another identifier 789 065 theend===
    ===+([0-9])+([ ])another identifier===
    1
    me@mylaptop $









    share|improve this question


























      0












      0








      0








      I wrote a basic bash function to test for the occurence of a string search argument in another string. Running it from the command line gives expected results. Running the same from a shell script fails. Can somebody show me the errors of my ways?



      me@mylaptop $ line='someidentifier 123 another identifier 789 065 theend'
      me@mylaptop $ sarg='+([0-9])+([ ])another identifier'
      me@mylaptop $ type strstr
      strstr is a function
      strstr ()

      [ "$1#*$2*" = "$1" ] && return 1;
      return 0

      me@mylaptop $ strstr "$line" "$sarg" ; echo "$?"
      0
      me@mylaptop $


      My script:



      me@mylaptop $ cat l.sh
      #!/bin/bash

      function strstr ()

      [ "$1#*$2*" = "$1" ] && return 1;
      return 0


      line='someidentifier 123 another identifier 789 065 theend'
      sarg='+([0-9])+([ ])another identifier'

      echo '==='"$line"'==='

      echo '==='"$sarg"'==='

      strstr "$line" "$sarg"

      echo "$?"

      me@mylaptop $ ./l.sh
      ===someidentifier 123 another identifier 789 065 theend===
      ===+([0-9])+([ ])another identifier===
      1
      me@mylaptop $









      share|improve this question
















      I wrote a basic bash function to test for the occurence of a string search argument in another string. Running it from the command line gives expected results. Running the same from a shell script fails. Can somebody show me the errors of my ways?



      me@mylaptop $ line='someidentifier 123 another identifier 789 065 theend'
      me@mylaptop $ sarg='+([0-9])+([ ])another identifier'
      me@mylaptop $ type strstr
      strstr is a function
      strstr ()

      [ "$1#*$2*" = "$1" ] && return 1;
      return 0

      me@mylaptop $ strstr "$line" "$sarg" ; echo "$?"
      0
      me@mylaptop $


      My script:



      me@mylaptop $ cat l.sh
      #!/bin/bash

      function strstr ()

      [ "$1#*$2*" = "$1" ] && return 1;
      return 0


      line='someidentifier 123 another identifier 789 065 theend'
      sarg='+([0-9])+([ ])another identifier'

      echo '==='"$line"'==='

      echo '==='"$sarg"'==='

      strstr "$line" "$sarg"

      echo "$?"

      me@mylaptop $ ./l.sh
      ===someidentifier 123 another identifier 789 065 theend===
      ===+([0-9])+([ ])another identifier===
      1
      me@mylaptop $






      bash bash-expansion






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 20 hours ago









      Rui F Ribeiro

      41.6k1483141




      41.6k1483141










      asked 20 hours ago









      JdeHaanJdeHaan

      359214




      359214




















          1 Answer
          1






          active

          oldest

          votes


















          4














          You have enabled the extglob shell option in your interactive shell, but not in your script:



          $ strstr "$line" "$sarg" ; echo "$?"
          1
          $ shopt -s extglob
          $ strstr "$line" "$sarg" ; echo "$?"
          0


          Note that your function could be simplified into



          strstr () 
          [ "$1#*$2*" != "$1" ]






          share|improve this answer























          • Correct. Perfect. Thanks. Both for the solution and the improvement.

            – JdeHaan
            19 hours ago










          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f507369%2fbash-script-fails-where-command-line-works%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          You have enabled the extglob shell option in your interactive shell, but not in your script:



          $ strstr "$line" "$sarg" ; echo "$?"
          1
          $ shopt -s extglob
          $ strstr "$line" "$sarg" ; echo "$?"
          0


          Note that your function could be simplified into



          strstr () 
          [ "$1#*$2*" != "$1" ]






          share|improve this answer























          • Correct. Perfect. Thanks. Both for the solution and the improvement.

            – JdeHaan
            19 hours ago















          4














          You have enabled the extglob shell option in your interactive shell, but not in your script:



          $ strstr "$line" "$sarg" ; echo "$?"
          1
          $ shopt -s extglob
          $ strstr "$line" "$sarg" ; echo "$?"
          0


          Note that your function could be simplified into



          strstr () 
          [ "$1#*$2*" != "$1" ]






          share|improve this answer























          • Correct. Perfect. Thanks. Both for the solution and the improvement.

            – JdeHaan
            19 hours ago













          4












          4








          4







          You have enabled the extglob shell option in your interactive shell, but not in your script:



          $ strstr "$line" "$sarg" ; echo "$?"
          1
          $ shopt -s extglob
          $ strstr "$line" "$sarg" ; echo "$?"
          0


          Note that your function could be simplified into



          strstr () 
          [ "$1#*$2*" != "$1" ]






          share|improve this answer













          You have enabled the extglob shell option in your interactive shell, but not in your script:



          $ strstr "$line" "$sarg" ; echo "$?"
          1
          $ shopt -s extglob
          $ strstr "$line" "$sarg" ; echo "$?"
          0


          Note that your function could be simplified into



          strstr () 
          [ "$1#*$2*" != "$1" ]







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 19 hours ago









          KusalanandaKusalananda

          136k17257426




          136k17257426












          • Correct. Perfect. Thanks. Both for the solution and the improvement.

            – JdeHaan
            19 hours ago

















          • Correct. Perfect. Thanks. Both for the solution and the improvement.

            – JdeHaan
            19 hours ago
















          Correct. Perfect. Thanks. Both for the solution and the improvement.

          – JdeHaan
          19 hours ago





          Correct. Perfect. Thanks. Both for the solution and the improvement.

          – JdeHaan
          19 hours ago

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f507369%2fbash-script-fails-where-command-line-works%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          B5Zus2C mWGZ kc4KupnXdrWHq2fnX P1IX0N jCRPH72HZMK Vy,x jAv1iGEBxQS62wjkN3lGEIMWAHJCfaqL n70UjA3BU
          vq1Bo5FsX W vvlb K1znaljNjVYou2es1mVFiMT1TmDgw8knKlI0Z7ax,8VY,wvYaB9Opq,g

          Popular posts from this blog

          getting Checkpoint VPN SSL Network Extender working in the command lineHow to connect to CheckPoint VPN on Ubuntu 18.04LTS?Will the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayVPN SSL Network Extender in FirefoxLinux Checkpoint SNX tool configuration issuesCheck Point - Connect under Linux - snx + OTPSNX VPN Ububuntu 18.XXUsing Checkpoint VPN SSL Network Extender CLI with certificateVPN with network manager (nm-applet) is not workingWill the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayImport VPN config files to NetworkManager from command lineTrouble connecting to VPN using network-manager, while command line worksStart a VPN connection with PPTP protocol on command linestarting a docker service daemon breaks the vpn networkCan't connect to vpn with Network-managerVPN SSL Network Extender in FirefoxUsing Checkpoint VPN SSL Network Extender CLI with certificate

          NetworkManager fails with “Could not find source connection”Trouble connecting to VPN using network-manager, while command line worksHow can I be notified about state changes to a VPN adapterBacktrack 5 R3 - Refuses to connect to VPNFeed all traffic through OpenVPN for a specific network namespace onlyRun daemon on startup in Debian once openvpn connection establishedpfsense tcp connection between openvpn and lan is brokenInternet connection problem with web browsers onlyWhy does NetworkManager explicitly support tun/tap devices?Browser issues with VPNTwo IP addresses assigned to the same network card - OpenVPN issues?Cannot connect to WiFi with nmcli, although secrets are provided

          Marilyn Monroe Ny fiainany manokana | Jereo koa | Meny fitetezanafanitarana azy.