bash. read input “words” string between symbols [duplicate]





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-1
















This question already has an answer here:




  • How do I split a string on a delimiter in Bash?

    34 answers




I have this kind of input in a bash I'm creating:



cat-dog-lion



I need my program to do something with each word like this



`if first_word == "cat" 
do stuff
if second_word == "dog"
do other stuff`


How should I do this?
Thank you










share|improve this question













marked as duplicate by georgexsh, tripleee bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 12:13


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

























    -1
















    This question already has an answer here:




    • How do I split a string on a delimiter in Bash?

      34 answers




    I have this kind of input in a bash I'm creating:



    cat-dog-lion



    I need my program to do something with each word like this



    `if first_word == "cat" 
    do stuff
    if second_word == "dog"
    do other stuff`


    How should I do this?
    Thank you










    share|improve this question













    marked as duplicate by georgexsh, tripleee bash
    Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 22 '18 at 12:13


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





















      -1












      -1








      -1









      This question already has an answer here:




      • How do I split a string on a delimiter in Bash?

        34 answers




      I have this kind of input in a bash I'm creating:



      cat-dog-lion



      I need my program to do something with each word like this



      `if first_word == "cat" 
      do stuff
      if second_word == "dog"
      do other stuff`


      How should I do this?
      Thank you










      share|improve this question















      This question already has an answer here:




      • How do I split a string on a delimiter in Bash?

        34 answers




      I have this kind of input in a bash I'm creating:



      cat-dog-lion



      I need my program to do something with each word like this



      `if first_word == "cat" 
      do stuff
      if second_word == "dog"
      do other stuff`


      How should I do this?
      Thank you





      This question already has an answer here:




      • How do I split a string on a delimiter in Bash?

        34 answers








      linux bash shell






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 10:13









      LanodisoftLanodisoft

      154




      154




      marked as duplicate by georgexsh, tripleee bash
      Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 22 '18 at 12:13


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by georgexsh, tripleee bash
      Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 22 '18 at 12:13


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          2 Answers
          2






          active

          oldest

          votes


















          0














          BASH solution:



          input='cat-dog-lion'
          if [[ "${input}" =~ "^cat-?" ]]
          then
          echo "do stuff"
          if [[ "${input}" =~ "^cat-dog-?" ]]
          then
          echo "do other stuff"
          fi
          fi


          If wanna AWK solution here it is:



          awk -F'-' '{ if ($1=="cat") { print("do stuff"); if ($2=="dog") print("do other stuff"); } }'


          Test:



          $ echo 'cat-dog-lion' | awk -F'-' '{ if ($1=="cat") { print("do stuff"); if ($2=="dog") print("do other stuff"); } }'
          do stuff
          do other stuff
          $ echo 'cat-wolf-lion' | awk -F'-' '{ if ($1=="cat") { print("do stuff"); if ($2=="dog") print("do other stuff"); } }'
          do stuff
          $ echo 'lynx-dog-lion' | awk -F'-' '{ if ($1=="cat") { print("do stuff"); if ($2=="dog") print("do other stuff"); } }'
          $





          share|improve this answer

































            -1














            If the input field separator is a -, you should call read with IFS set appropriately:



            echo 'cat-dog-lion' | { IFS=- read first_word second_word third_word other_stuff
            case $first_word in
            cat) ... ;;
            *) ... ;;
            esac
            case $second_word in
            ...
            esac
            ...
            }


            Note that the enclosing { and } are important, as the variables go out of scope after the closing {. (The pipe creates a subprocess in which the variables exist.)






            share|improve this answer






























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              BASH solution:



              input='cat-dog-lion'
              if [[ "${input}" =~ "^cat-?" ]]
              then
              echo "do stuff"
              if [[ "${input}" =~ "^cat-dog-?" ]]
              then
              echo "do other stuff"
              fi
              fi


              If wanna AWK solution here it is:



              awk -F'-' '{ if ($1=="cat") { print("do stuff"); if ($2=="dog") print("do other stuff"); } }'


              Test:



              $ echo 'cat-dog-lion' | awk -F'-' '{ if ($1=="cat") { print("do stuff"); if ($2=="dog") print("do other stuff"); } }'
              do stuff
              do other stuff
              $ echo 'cat-wolf-lion' | awk -F'-' '{ if ($1=="cat") { print("do stuff"); if ($2=="dog") print("do other stuff"); } }'
              do stuff
              $ echo 'lynx-dog-lion' | awk -F'-' '{ if ($1=="cat") { print("do stuff"); if ($2=="dog") print("do other stuff"); } }'
              $





              share|improve this answer






























                0














                BASH solution:



                input='cat-dog-lion'
                if [[ "${input}" =~ "^cat-?" ]]
                then
                echo "do stuff"
                if [[ "${input}" =~ "^cat-dog-?" ]]
                then
                echo "do other stuff"
                fi
                fi


                If wanna AWK solution here it is:



                awk -F'-' '{ if ($1=="cat") { print("do stuff"); if ($2=="dog") print("do other stuff"); } }'


                Test:



                $ echo 'cat-dog-lion' | awk -F'-' '{ if ($1=="cat") { print("do stuff"); if ($2=="dog") print("do other stuff"); } }'
                do stuff
                do other stuff
                $ echo 'cat-wolf-lion' | awk -F'-' '{ if ($1=="cat") { print("do stuff"); if ($2=="dog") print("do other stuff"); } }'
                do stuff
                $ echo 'lynx-dog-lion' | awk -F'-' '{ if ($1=="cat") { print("do stuff"); if ($2=="dog") print("do other stuff"); } }'
                $





                share|improve this answer




























                  0












                  0








                  0







                  BASH solution:



                  input='cat-dog-lion'
                  if [[ "${input}" =~ "^cat-?" ]]
                  then
                  echo "do stuff"
                  if [[ "${input}" =~ "^cat-dog-?" ]]
                  then
                  echo "do other stuff"
                  fi
                  fi


                  If wanna AWK solution here it is:



                  awk -F'-' '{ if ($1=="cat") { print("do stuff"); if ($2=="dog") print("do other stuff"); } }'


                  Test:



                  $ echo 'cat-dog-lion' | awk -F'-' '{ if ($1=="cat") { print("do stuff"); if ($2=="dog") print("do other stuff"); } }'
                  do stuff
                  do other stuff
                  $ echo 'cat-wolf-lion' | awk -F'-' '{ if ($1=="cat") { print("do stuff"); if ($2=="dog") print("do other stuff"); } }'
                  do stuff
                  $ echo 'lynx-dog-lion' | awk -F'-' '{ if ($1=="cat") { print("do stuff"); if ($2=="dog") print("do other stuff"); } }'
                  $





                  share|improve this answer















                  BASH solution:



                  input='cat-dog-lion'
                  if [[ "${input}" =~ "^cat-?" ]]
                  then
                  echo "do stuff"
                  if [[ "${input}" =~ "^cat-dog-?" ]]
                  then
                  echo "do other stuff"
                  fi
                  fi


                  If wanna AWK solution here it is:



                  awk -F'-' '{ if ($1=="cat") { print("do stuff"); if ($2=="dog") print("do other stuff"); } }'


                  Test:



                  $ echo 'cat-dog-lion' | awk -F'-' '{ if ($1=="cat") { print("do stuff"); if ($2=="dog") print("do other stuff"); } }'
                  do stuff
                  do other stuff
                  $ echo 'cat-wolf-lion' | awk -F'-' '{ if ($1=="cat") { print("do stuff"); if ($2=="dog") print("do other stuff"); } }'
                  do stuff
                  $ echo 'lynx-dog-lion' | awk -F'-' '{ if ($1=="cat") { print("do stuff"); if ($2=="dog") print("do other stuff"); } }'
                  $






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 22 '18 at 11:00

























                  answered Nov 22 '18 at 10:33









                  KubatorKubator

                  1,02413




                  1,02413

























                      -1














                      If the input field separator is a -, you should call read with IFS set appropriately:



                      echo 'cat-dog-lion' | { IFS=- read first_word second_word third_word other_stuff
                      case $first_word in
                      cat) ... ;;
                      *) ... ;;
                      esac
                      case $second_word in
                      ...
                      esac
                      ...
                      }


                      Note that the enclosing { and } are important, as the variables go out of scope after the closing {. (The pipe creates a subprocess in which the variables exist.)






                      share|improve this answer




























                        -1














                        If the input field separator is a -, you should call read with IFS set appropriately:



                        echo 'cat-dog-lion' | { IFS=- read first_word second_word third_word other_stuff
                        case $first_word in
                        cat) ... ;;
                        *) ... ;;
                        esac
                        case $second_word in
                        ...
                        esac
                        ...
                        }


                        Note that the enclosing { and } are important, as the variables go out of scope after the closing {. (The pipe creates a subprocess in which the variables exist.)






                        share|improve this answer


























                          -1












                          -1








                          -1







                          If the input field separator is a -, you should call read with IFS set appropriately:



                          echo 'cat-dog-lion' | { IFS=- read first_word second_word third_word other_stuff
                          case $first_word in
                          cat) ... ;;
                          *) ... ;;
                          esac
                          case $second_word in
                          ...
                          esac
                          ...
                          }


                          Note that the enclosing { and } are important, as the variables go out of scope after the closing {. (The pipe creates a subprocess in which the variables exist.)






                          share|improve this answer













                          If the input field separator is a -, you should call read with IFS set appropriately:



                          echo 'cat-dog-lion' | { IFS=- read first_word second_word third_word other_stuff
                          case $first_word in
                          cat) ... ;;
                          *) ... ;;
                          esac
                          case $second_word in
                          ...
                          esac
                          ...
                          }


                          Note that the enclosing { and } are important, as the variables go out of scope after the closing {. (The pipe creates a subprocess in which the variables exist.)







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 22 '18 at 12:06









                          William PursellWilliam Pursell

                          134k33210244




                          134k33210244















                              Popular posts from this blog

                              鏡平學校

                              ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

                              Why https connections are so slow when debugging (stepping over) in Java?