How to test whether wc -l is even or odd in shell?

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











up vote
0
down vote

favorite












say I have a file called file1



$ a=$(wc -c file1)
$ echo $a
233 file1


now how would I do this



$ b=$(expr $a % 2)
$ echo $b
1


I guess the file1 in wc -c make's this crash?







share|improve this question
























    up vote
    0
    down vote

    favorite












    say I have a file called file1



    $ a=$(wc -c file1)
    $ echo $a
    233 file1


    now how would I do this



    $ b=$(expr $a % 2)
    $ echo $b
    1


    I guess the file1 in wc -c make's this crash?







    share|improve this question






















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      say I have a file called file1



      $ a=$(wc -c file1)
      $ echo $a
      233 file1


      now how would I do this



      $ b=$(expr $a % 2)
      $ echo $b
      1


      I guess the file1 in wc -c make's this crash?







      share|improve this question












      say I have a file called file1



      $ a=$(wc -c file1)
      $ echo $a
      233 file1


      now how would I do this



      $ b=$(expr $a % 2)
      $ echo $b
      1


      I guess the file1 in wc -c make's this crash?









      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 26 at 0:35









      Tinler

      1295




      1295




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Another way to remove the filename from wc is to use wc -c <file1. This avoids the requirement of a second process cat.






          share|improve this answer



























            up vote
            2
            down vote













            I would use something like:



            if [ $(( $(wc -c < your-filehere ) % 2)) -eq 1 ]
            then
            echo file has odd number of bytes
            else
            echo file has even number of bytes
            fi





            share|improve this answer



























              up vote
              0
              down vote













              One way to go:



              a=$(cat file1 | wc -c)


              this doesn't output the file name and your expr will work.






              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%2f426571%2fhow-to-test-whether-wc-l-is-even-or-odd-in-shell%23new-answer', 'question_page');

                );

                Post as a guest






























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                1
                down vote



                accepted










                Another way to remove the filename from wc is to use wc -c <file1. This avoids the requirement of a second process cat.






                share|improve this answer
























                  up vote
                  1
                  down vote



                  accepted










                  Another way to remove the filename from wc is to use wc -c <file1. This avoids the requirement of a second process cat.






                  share|improve this answer






















                    up vote
                    1
                    down vote



                    accepted







                    up vote
                    1
                    down vote



                    accepted






                    Another way to remove the filename from wc is to use wc -c <file1. This avoids the requirement of a second process cat.






                    share|improve this answer












                    Another way to remove the filename from wc is to use wc -c <file1. This avoids the requirement of a second process cat.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Feb 26 at 0:41









                    dsstorefile1

                    1,576212




                    1,576212






















                        up vote
                        2
                        down vote













                        I would use something like:



                        if [ $(( $(wc -c < your-filehere ) % 2)) -eq 1 ]
                        then
                        echo file has odd number of bytes
                        else
                        echo file has even number of bytes
                        fi





                        share|improve this answer
























                          up vote
                          2
                          down vote













                          I would use something like:



                          if [ $(( $(wc -c < your-filehere ) % 2)) -eq 1 ]
                          then
                          echo file has odd number of bytes
                          else
                          echo file has even number of bytes
                          fi





                          share|improve this answer






















                            up vote
                            2
                            down vote










                            up vote
                            2
                            down vote









                            I would use something like:



                            if [ $(( $(wc -c < your-filehere ) % 2)) -eq 1 ]
                            then
                            echo file has odd number of bytes
                            else
                            echo file has even number of bytes
                            fi





                            share|improve this answer












                            I would use something like:



                            if [ $(( $(wc -c < your-filehere ) % 2)) -eq 1 ]
                            then
                            echo file has odd number of bytes
                            else
                            echo file has even number of bytes
                            fi






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Feb 26 at 1:15









                            Jeff Schaller

                            31.2k846105




                            31.2k846105




















                                up vote
                                0
                                down vote













                                One way to go:



                                a=$(cat file1 | wc -c)


                                this doesn't output the file name and your expr will work.






                                share|improve this answer
























                                  up vote
                                  0
                                  down vote













                                  One way to go:



                                  a=$(cat file1 | wc -c)


                                  this doesn't output the file name and your expr will work.






                                  share|improve this answer






















                                    up vote
                                    0
                                    down vote










                                    up vote
                                    0
                                    down vote









                                    One way to go:



                                    a=$(cat file1 | wc -c)


                                    this doesn't output the file name and your expr will work.






                                    share|improve this answer












                                    One way to go:



                                    a=$(cat file1 | wc -c)


                                    this doesn't output the file name and your expr will work.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Feb 26 at 0:39









                                    man0v

                                    30917




                                    30917






















                                         

                                        draft saved


                                        draft discarded


























                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f426571%2fhow-to-test-whether-wc-l-is-even-or-odd-in-shell%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?

                                        Bahrain

                                        Postfix configuration issue with fips on centos 7; mailgun relay