shell script to test condition on passed string

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












condition
string either start with upper or lower alphabets or underscore
for example _abc or xyz or Abc



string may contain digits after first character
like _f9 or f10 or car20 or top10cars



string should never contain special characters like ! @ # $ % ^ & * ( ) + - =



here my tiny script



if [[ $1 =~ ^[A-Za-z_]+$ ]]; then
echo it matches
else
echo does_not match
fi


when i pass argument like only & or * or ( , script don't work , sometimes cursor never comes backs , i need to press ctrl+c



for example ./script.sh (abc 
./script.sh &&
./script.sh &abc


whats wrong with script










share|improve this question























  • edited question
    – user143252
    Sep 25 '17 at 17:07














up vote
0
down vote

favorite












condition
string either start with upper or lower alphabets or underscore
for example _abc or xyz or Abc



string may contain digits after first character
like _f9 or f10 or car20 or top10cars



string should never contain special characters like ! @ # $ % ^ & * ( ) + - =



here my tiny script



if [[ $1 =~ ^[A-Za-z_]+$ ]]; then
echo it matches
else
echo does_not match
fi


when i pass argument like only & or * or ( , script don't work , sometimes cursor never comes backs , i need to press ctrl+c



for example ./script.sh (abc 
./script.sh &&
./script.sh &abc


whats wrong with script










share|improve this question























  • edited question
    – user143252
    Sep 25 '17 at 17:07












up vote
0
down vote

favorite









up vote
0
down vote

favorite











condition
string either start with upper or lower alphabets or underscore
for example _abc or xyz or Abc



string may contain digits after first character
like _f9 or f10 or car20 or top10cars



string should never contain special characters like ! @ # $ % ^ & * ( ) + - =



here my tiny script



if [[ $1 =~ ^[A-Za-z_]+$ ]]; then
echo it matches
else
echo does_not match
fi


when i pass argument like only & or * or ( , script don't work , sometimes cursor never comes backs , i need to press ctrl+c



for example ./script.sh (abc 
./script.sh &&
./script.sh &abc


whats wrong with script










share|improve this question















condition
string either start with upper or lower alphabets or underscore
for example _abc or xyz or Abc



string may contain digits after first character
like _f9 or f10 or car20 or top10cars



string should never contain special characters like ! @ # $ % ^ & * ( ) + - =



here my tiny script



if [[ $1 =~ ^[A-Za-z_]+$ ]]; then
echo it matches
else
echo does_not match
fi


when i pass argument like only & or * or ( , script don't work , sometimes cursor never comes backs , i need to press ctrl+c



for example ./script.sh (abc 
./script.sh &&
./script.sh &abc


whats wrong with script







bash shell-script






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 25 '17 at 17:06

























asked Sep 25 '17 at 17:00









user143252

95




95











  • edited question
    – user143252
    Sep 25 '17 at 17:07
















  • edited question
    – user143252
    Sep 25 '17 at 17:07















edited question
– user143252
Sep 25 '17 at 17:07




edited question
– user143252
Sep 25 '17 at 17:07










2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










Quoting.



In the script, use "$1" rather than just $1.



On the command line, use



./script '*(ontehu'


instead of



./script *(ontehu



  • ./script.sh (abc This is a syntax error in the shell grammar.


  • ./script.sh && This makes the shell think there is a conditional AND on the command line and it expects something on the right hand side of the && operator.


  • ./script.sh &abc This is two commands: ./script started as a background process (with &), and the command abc.


In all these cases, the argument should be quoted (single quoted, ideally, unless you need the shell to insert the value of a shell variable, in which case it should be double quoted).




Your script:



if [[ $1 =~ ^[A-Za-z_]+$ ]]; then
echo it matches
else
echo does_not match
fi


Quote $1:



if [[ "$1" =~ ^[A-Za-z_]+$ ]]; then
echo it matches
else
echo does_not match
fi


Allow digits in the tail end of the value:



if [[ "$1" =~ ^[A-Za-z_][A-Za-z0-9_]+$ ]]; then
echo it matches
else
echo does_not match
fi


Do proper reporting of errors (this is extra):



if [[ "$1" =~ ^[A-Za-z_][A-Za-z0-9_]+$ ]]; then
printf '"%s" is a valid variable namen' "$1"
else
printf '"%s" is not a proper variable namen' "$1" >&2
exit 1
fi





share|improve this answer





























    up vote
    1
    down vote













    That's because these are reserved characters.



    & means run command in the background



    * resolves to all files/dirs in the actual directory, which are then passed as arguments



    () is used for command order preference or function declarations



    If you want such a characters in the string, put it in quotes " " or ' '






    share|improve this answer




















      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',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      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%2f394355%2fshell-script-to-test-condition-on-passed-string%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote



      accepted










      Quoting.



      In the script, use "$1" rather than just $1.



      On the command line, use



      ./script '*(ontehu'


      instead of



      ./script *(ontehu



      • ./script.sh (abc This is a syntax error in the shell grammar.


      • ./script.sh && This makes the shell think there is a conditional AND on the command line and it expects something on the right hand side of the && operator.


      • ./script.sh &abc This is two commands: ./script started as a background process (with &), and the command abc.


      In all these cases, the argument should be quoted (single quoted, ideally, unless you need the shell to insert the value of a shell variable, in which case it should be double quoted).




      Your script:



      if [[ $1 =~ ^[A-Za-z_]+$ ]]; then
      echo it matches
      else
      echo does_not match
      fi


      Quote $1:



      if [[ "$1" =~ ^[A-Za-z_]+$ ]]; then
      echo it matches
      else
      echo does_not match
      fi


      Allow digits in the tail end of the value:



      if [[ "$1" =~ ^[A-Za-z_][A-Za-z0-9_]+$ ]]; then
      echo it matches
      else
      echo does_not match
      fi


      Do proper reporting of errors (this is extra):



      if [[ "$1" =~ ^[A-Za-z_][A-Za-z0-9_]+$ ]]; then
      printf '"%s" is a valid variable namen' "$1"
      else
      printf '"%s" is not a proper variable namen' "$1" >&2
      exit 1
      fi





      share|improve this answer


























        up vote
        2
        down vote



        accepted










        Quoting.



        In the script, use "$1" rather than just $1.



        On the command line, use



        ./script '*(ontehu'


        instead of



        ./script *(ontehu



        • ./script.sh (abc This is a syntax error in the shell grammar.


        • ./script.sh && This makes the shell think there is a conditional AND on the command line and it expects something on the right hand side of the && operator.


        • ./script.sh &abc This is two commands: ./script started as a background process (with &), and the command abc.


        In all these cases, the argument should be quoted (single quoted, ideally, unless you need the shell to insert the value of a shell variable, in which case it should be double quoted).




        Your script:



        if [[ $1 =~ ^[A-Za-z_]+$ ]]; then
        echo it matches
        else
        echo does_not match
        fi


        Quote $1:



        if [[ "$1" =~ ^[A-Za-z_]+$ ]]; then
        echo it matches
        else
        echo does_not match
        fi


        Allow digits in the tail end of the value:



        if [[ "$1" =~ ^[A-Za-z_][A-Za-z0-9_]+$ ]]; then
        echo it matches
        else
        echo does_not match
        fi


        Do proper reporting of errors (this is extra):



        if [[ "$1" =~ ^[A-Za-z_][A-Za-z0-9_]+$ ]]; then
        printf '"%s" is a valid variable namen' "$1"
        else
        printf '"%s" is not a proper variable namen' "$1" >&2
        exit 1
        fi





        share|improve this answer
























          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          Quoting.



          In the script, use "$1" rather than just $1.



          On the command line, use



          ./script '*(ontehu'


          instead of



          ./script *(ontehu



          • ./script.sh (abc This is a syntax error in the shell grammar.


          • ./script.sh && This makes the shell think there is a conditional AND on the command line and it expects something on the right hand side of the && operator.


          • ./script.sh &abc This is two commands: ./script started as a background process (with &), and the command abc.


          In all these cases, the argument should be quoted (single quoted, ideally, unless you need the shell to insert the value of a shell variable, in which case it should be double quoted).




          Your script:



          if [[ $1 =~ ^[A-Za-z_]+$ ]]; then
          echo it matches
          else
          echo does_not match
          fi


          Quote $1:



          if [[ "$1" =~ ^[A-Za-z_]+$ ]]; then
          echo it matches
          else
          echo does_not match
          fi


          Allow digits in the tail end of the value:



          if [[ "$1" =~ ^[A-Za-z_][A-Za-z0-9_]+$ ]]; then
          echo it matches
          else
          echo does_not match
          fi


          Do proper reporting of errors (this is extra):



          if [[ "$1" =~ ^[A-Za-z_][A-Za-z0-9_]+$ ]]; then
          printf '"%s" is a valid variable namen' "$1"
          else
          printf '"%s" is not a proper variable namen' "$1" >&2
          exit 1
          fi





          share|improve this answer














          Quoting.



          In the script, use "$1" rather than just $1.



          On the command line, use



          ./script '*(ontehu'


          instead of



          ./script *(ontehu



          • ./script.sh (abc This is a syntax error in the shell grammar.


          • ./script.sh && This makes the shell think there is a conditional AND on the command line and it expects something on the right hand side of the && operator.


          • ./script.sh &abc This is two commands: ./script started as a background process (with &), and the command abc.


          In all these cases, the argument should be quoted (single quoted, ideally, unless you need the shell to insert the value of a shell variable, in which case it should be double quoted).




          Your script:



          if [[ $1 =~ ^[A-Za-z_]+$ ]]; then
          echo it matches
          else
          echo does_not match
          fi


          Quote $1:



          if [[ "$1" =~ ^[A-Za-z_]+$ ]]; then
          echo it matches
          else
          echo does_not match
          fi


          Allow digits in the tail end of the value:



          if [[ "$1" =~ ^[A-Za-z_][A-Za-z0-9_]+$ ]]; then
          echo it matches
          else
          echo does_not match
          fi


          Do proper reporting of errors (this is extra):



          if [[ "$1" =~ ^[A-Za-z_][A-Za-z0-9_]+$ ]]; then
          printf '"%s" is a valid variable namen' "$1"
          else
          printf '"%s" is not a proper variable namen' "$1" >&2
          exit 1
          fi






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 25 '17 at 17:22

























          answered Sep 25 '17 at 17:09









          Kusalananda

          106k14209327




          106k14209327






















              up vote
              1
              down vote













              That's because these are reserved characters.



              & means run command in the background



              * resolves to all files/dirs in the actual directory, which are then passed as arguments



              () is used for command order preference or function declarations



              If you want such a characters in the string, put it in quotes " " or ' '






              share|improve this answer
























                up vote
                1
                down vote













                That's because these are reserved characters.



                & means run command in the background



                * resolves to all files/dirs in the actual directory, which are then passed as arguments



                () is used for command order preference or function declarations



                If you want such a characters in the string, put it in quotes " " or ' '






                share|improve this answer






















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  That's because these are reserved characters.



                  & means run command in the background



                  * resolves to all files/dirs in the actual directory, which are then passed as arguments



                  () is used for command order preference or function declarations



                  If you want such a characters in the string, put it in quotes " " or ' '






                  share|improve this answer












                  That's because these are reserved characters.



                  & means run command in the background



                  * resolves to all files/dirs in the actual directory, which are then passed as arguments



                  () is used for command order preference or function declarations



                  If you want such a characters in the string, put it in quotes " " or ' '







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 25 '17 at 17:10









                  Jaroslav Kucera

                  4,3904621




                  4,3904621



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f394355%2fshell-script-to-test-condition-on-passed-string%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?