Number variables appear in directory when I run bash script

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











up vote
2
down vote

favorite












I have written a Bash script in a directory. When I run it and then run 'ls' a number appears in the directory level which I must then use 'rm' to get rid of.



This Bash script is meant to save the current files in the directory to logstore.txt then get the old number of lines stored in numlines.txt then find the new number of lines assigning this to numlines.txt and finally getting the new number of lines to compare to the old number (the server is adding new files over time if you couldn't guess) I then run a comparison between the old and new line lengths.



Here is the code in a .sh file.



ls /home/vcm > /home/vcm/logstore.txt
oldnumlines=$(cat /home/vcm/numlines.txt)
#log store takes the new ls of items and stores it as num lines after the old v$
cat logstore.txt | wc -l > /home/vcm/numlines.txt

newnumlines=$(cat /home/vcm/numlines.txt)

if [ ! "$newnumlines" > "$oldnumlines" ]
#[ "$a" -eq "$b" ]
#(( "$newnumlines" -le "$oldnumlines" ))
then
*do something*
fi


Why is this script creating new variables in the directory?







share|improve this question

























    up vote
    2
    down vote

    favorite












    I have written a Bash script in a directory. When I run it and then run 'ls' a number appears in the directory level which I must then use 'rm' to get rid of.



    This Bash script is meant to save the current files in the directory to logstore.txt then get the old number of lines stored in numlines.txt then find the new number of lines assigning this to numlines.txt and finally getting the new number of lines to compare to the old number (the server is adding new files over time if you couldn't guess) I then run a comparison between the old and new line lengths.



    Here is the code in a .sh file.



    ls /home/vcm > /home/vcm/logstore.txt
    oldnumlines=$(cat /home/vcm/numlines.txt)
    #log store takes the new ls of items and stores it as num lines after the old v$
    cat logstore.txt | wc -l > /home/vcm/numlines.txt

    newnumlines=$(cat /home/vcm/numlines.txt)

    if [ ! "$newnumlines" > "$oldnumlines" ]
    #[ "$a" -eq "$b" ]
    #(( "$newnumlines" -le "$oldnumlines" ))
    then
    *do something*
    fi


    Why is this script creating new variables in the directory?







    share|improve this question























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I have written a Bash script in a directory. When I run it and then run 'ls' a number appears in the directory level which I must then use 'rm' to get rid of.



      This Bash script is meant to save the current files in the directory to logstore.txt then get the old number of lines stored in numlines.txt then find the new number of lines assigning this to numlines.txt and finally getting the new number of lines to compare to the old number (the server is adding new files over time if you couldn't guess) I then run a comparison between the old and new line lengths.



      Here is the code in a .sh file.



      ls /home/vcm > /home/vcm/logstore.txt
      oldnumlines=$(cat /home/vcm/numlines.txt)
      #log store takes the new ls of items and stores it as num lines after the old v$
      cat logstore.txt | wc -l > /home/vcm/numlines.txt

      newnumlines=$(cat /home/vcm/numlines.txt)

      if [ ! "$newnumlines" > "$oldnumlines" ]
      #[ "$a" -eq "$b" ]
      #(( "$newnumlines" -le "$oldnumlines" ))
      then
      *do something*
      fi


      Why is this script creating new variables in the directory?







      share|improve this question













      I have written a Bash script in a directory. When I run it and then run 'ls' a number appears in the directory level which I must then use 'rm' to get rid of.



      This Bash script is meant to save the current files in the directory to logstore.txt then get the old number of lines stored in numlines.txt then find the new number of lines assigning this to numlines.txt and finally getting the new number of lines to compare to the old number (the server is adding new files over time if you couldn't guess) I then run a comparison between the old and new line lengths.



      Here is the code in a .sh file.



      ls /home/vcm > /home/vcm/logstore.txt
      oldnumlines=$(cat /home/vcm/numlines.txt)
      #log store takes the new ls of items and stores it as num lines after the old v$
      cat logstore.txt | wc -l > /home/vcm/numlines.txt

      newnumlines=$(cat /home/vcm/numlines.txt)

      if [ ! "$newnumlines" > "$oldnumlines" ]
      #[ "$a" -eq "$b" ]
      #(( "$newnumlines" -le "$oldnumlines" ))
      then
      *do something*
      fi


      Why is this script creating new variables in the directory?









      share|improve this question












      share|improve this question




      share|improve this question








      edited Jul 12 at 15:57









      slm♦

      233k65479651




      233k65479651









      asked Jul 11 at 2:22









      tbrick

      132




      132




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          Here's how to debug something like this. I have a simplified version of your script.



          $ cat var_dumper.bash
          #!/bin/bash

          var1=1
          var2=2

          if [ ! "$var1" > "$var2" ]; then
          echo "I'm inside the if/then"
          fi


          And we run it:



          $ ./var_dumper.bash
          $ ls
          var_dumper.bash 2


          So we can see your 2 showing up as a file. That's your issue.



          Debugging



          To debug a shell script, I always start with the -x switch to Bash. You can run your script like this:



          $ bash -x ./var_dumper.bash
          + var1=1
          + var2=2
          + '[' '!' 1 ']'


          This tells us where it's failing. The if condition. So what's wrong with it? Well the first clue should be the >. That's a redirect operator, in other usages in Bash, so that's what's likely producing the file being written.



          So let's reformulate your example into a single executable:



          $ var1=1 var2=2 [ "$var1" > "$var2" ] && echo success || echo failure
          success


          The next clue should be, why is it returning a success, 1 isn't greater than 2. So we google and we discover that to compare integers in Bash you don't use the > operator, you're actually suppose to use this one: -gt.



          $ var1=1 var2=2 [ "$var1" -gt "$var2" ] && echo success || echo failure
          failure


          And since you originally wanted it negated, we could put the ! back in:



          $ var1=1 var2=2 [ ! "$var1" -gt "$var2" ] && echo success || echo failure
          success


          And it works. So your corrected if statement would look like this:



          if [ ! "$var1" -gt "$var2" ]; then
          echo "I'm inside the if/then"
          fi


          So what was wrong with >



          In your scenario you were using if [ .... ]. This form of if/then is the POSIX compliant version. This one does not support the > operator. The form that does support that is this, if [[ .... ]]. The double bracketed version is the more capable extended test command.



          So as an alternative:



          $ cat var_dumper.bash
          #!/bin/bash

          var1=1
          var2=2

          if [[ ! "$var1" > "$var2" ]]; then
          echo "I'm inside the if/then"
          fi


          Would also work:



          $ bash -x var_dumper.bash
          + var1=1
          + var2=2
          + [[ ! 1 > 2 ]]
          + echo 'I'''m inside the if/then'
          I'm inside the if/then


          References



          • 7.3. Other Comparison Operators





          share|improve this answer






























            up vote
            0
            down vote













            Do not use > in a digital test. Because it is a redirection operator.



            His execution is always true (if the file is written) :



            $ if [ 2 > 30000 ]; then echo OK; else echo NO; fi
            OK
            $ ls
            $ 30000
            $ rm 30000


            And a 30000 file was written.



            There was no digital test but a file write, which returned true



            $ mkdir tmp
            $ chmod -w tmp
            $ cd tmp
            $ if [ 2 > 30000 ]; then echo OK; else echo NO; fi
            -bash: 30000: Permission non accordée
            NO
            $ cd ..
            $ rmdir tmp


            Use -gt :



            $ if [ 2 -gt 30000 ]; then echo OK; else echo NO; fi
            NO
            $ ls





            share|improve this answer





















            • gives the resolution to slim, thanks
              – alux
              Jul 12 at 20:56










            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%2f454597%2fnumber-variables-appear-in-directory-when-i-run-bash-script%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
            3
            down vote



            accepted










            Here's how to debug something like this. I have a simplified version of your script.



            $ cat var_dumper.bash
            #!/bin/bash

            var1=1
            var2=2

            if [ ! "$var1" > "$var2" ]; then
            echo "I'm inside the if/then"
            fi


            And we run it:



            $ ./var_dumper.bash
            $ ls
            var_dumper.bash 2


            So we can see your 2 showing up as a file. That's your issue.



            Debugging



            To debug a shell script, I always start with the -x switch to Bash. You can run your script like this:



            $ bash -x ./var_dumper.bash
            + var1=1
            + var2=2
            + '[' '!' 1 ']'


            This tells us where it's failing. The if condition. So what's wrong with it? Well the first clue should be the >. That's a redirect operator, in other usages in Bash, so that's what's likely producing the file being written.



            So let's reformulate your example into a single executable:



            $ var1=1 var2=2 [ "$var1" > "$var2" ] && echo success || echo failure
            success


            The next clue should be, why is it returning a success, 1 isn't greater than 2. So we google and we discover that to compare integers in Bash you don't use the > operator, you're actually suppose to use this one: -gt.



            $ var1=1 var2=2 [ "$var1" -gt "$var2" ] && echo success || echo failure
            failure


            And since you originally wanted it negated, we could put the ! back in:



            $ var1=1 var2=2 [ ! "$var1" -gt "$var2" ] && echo success || echo failure
            success


            And it works. So your corrected if statement would look like this:



            if [ ! "$var1" -gt "$var2" ]; then
            echo "I'm inside the if/then"
            fi


            So what was wrong with >



            In your scenario you were using if [ .... ]. This form of if/then is the POSIX compliant version. This one does not support the > operator. The form that does support that is this, if [[ .... ]]. The double bracketed version is the more capable extended test command.



            So as an alternative:



            $ cat var_dumper.bash
            #!/bin/bash

            var1=1
            var2=2

            if [[ ! "$var1" > "$var2" ]]; then
            echo "I'm inside the if/then"
            fi


            Would also work:



            $ bash -x var_dumper.bash
            + var1=1
            + var2=2
            + [[ ! 1 > 2 ]]
            + echo 'I'''m inside the if/then'
            I'm inside the if/then


            References



            • 7.3. Other Comparison Operators





            share|improve this answer



























              up vote
              3
              down vote



              accepted










              Here's how to debug something like this. I have a simplified version of your script.



              $ cat var_dumper.bash
              #!/bin/bash

              var1=1
              var2=2

              if [ ! "$var1" > "$var2" ]; then
              echo "I'm inside the if/then"
              fi


              And we run it:



              $ ./var_dumper.bash
              $ ls
              var_dumper.bash 2


              So we can see your 2 showing up as a file. That's your issue.



              Debugging



              To debug a shell script, I always start with the -x switch to Bash. You can run your script like this:



              $ bash -x ./var_dumper.bash
              + var1=1
              + var2=2
              + '[' '!' 1 ']'


              This tells us where it's failing. The if condition. So what's wrong with it? Well the first clue should be the >. That's a redirect operator, in other usages in Bash, so that's what's likely producing the file being written.



              So let's reformulate your example into a single executable:



              $ var1=1 var2=2 [ "$var1" > "$var2" ] && echo success || echo failure
              success


              The next clue should be, why is it returning a success, 1 isn't greater than 2. So we google and we discover that to compare integers in Bash you don't use the > operator, you're actually suppose to use this one: -gt.



              $ var1=1 var2=2 [ "$var1" -gt "$var2" ] && echo success || echo failure
              failure


              And since you originally wanted it negated, we could put the ! back in:



              $ var1=1 var2=2 [ ! "$var1" -gt "$var2" ] && echo success || echo failure
              success


              And it works. So your corrected if statement would look like this:



              if [ ! "$var1" -gt "$var2" ]; then
              echo "I'm inside the if/then"
              fi


              So what was wrong with >



              In your scenario you were using if [ .... ]. This form of if/then is the POSIX compliant version. This one does not support the > operator. The form that does support that is this, if [[ .... ]]. The double bracketed version is the more capable extended test command.



              So as an alternative:



              $ cat var_dumper.bash
              #!/bin/bash

              var1=1
              var2=2

              if [[ ! "$var1" > "$var2" ]]; then
              echo "I'm inside the if/then"
              fi


              Would also work:



              $ bash -x var_dumper.bash
              + var1=1
              + var2=2
              + [[ ! 1 > 2 ]]
              + echo 'I'''m inside the if/then'
              I'm inside the if/then


              References



              • 7.3. Other Comparison Operators





              share|improve this answer

























                up vote
                3
                down vote



                accepted







                up vote
                3
                down vote



                accepted






                Here's how to debug something like this. I have a simplified version of your script.



                $ cat var_dumper.bash
                #!/bin/bash

                var1=1
                var2=2

                if [ ! "$var1" > "$var2" ]; then
                echo "I'm inside the if/then"
                fi


                And we run it:



                $ ./var_dumper.bash
                $ ls
                var_dumper.bash 2


                So we can see your 2 showing up as a file. That's your issue.



                Debugging



                To debug a shell script, I always start with the -x switch to Bash. You can run your script like this:



                $ bash -x ./var_dumper.bash
                + var1=1
                + var2=2
                + '[' '!' 1 ']'


                This tells us where it's failing. The if condition. So what's wrong with it? Well the first clue should be the >. That's a redirect operator, in other usages in Bash, so that's what's likely producing the file being written.



                So let's reformulate your example into a single executable:



                $ var1=1 var2=2 [ "$var1" > "$var2" ] && echo success || echo failure
                success


                The next clue should be, why is it returning a success, 1 isn't greater than 2. So we google and we discover that to compare integers in Bash you don't use the > operator, you're actually suppose to use this one: -gt.



                $ var1=1 var2=2 [ "$var1" -gt "$var2" ] && echo success || echo failure
                failure


                And since you originally wanted it negated, we could put the ! back in:



                $ var1=1 var2=2 [ ! "$var1" -gt "$var2" ] && echo success || echo failure
                success


                And it works. So your corrected if statement would look like this:



                if [ ! "$var1" -gt "$var2" ]; then
                echo "I'm inside the if/then"
                fi


                So what was wrong with >



                In your scenario you were using if [ .... ]. This form of if/then is the POSIX compliant version. This one does not support the > operator. The form that does support that is this, if [[ .... ]]. The double bracketed version is the more capable extended test command.



                So as an alternative:



                $ cat var_dumper.bash
                #!/bin/bash

                var1=1
                var2=2

                if [[ ! "$var1" > "$var2" ]]; then
                echo "I'm inside the if/then"
                fi


                Would also work:



                $ bash -x var_dumper.bash
                + var1=1
                + var2=2
                + [[ ! 1 > 2 ]]
                + echo 'I'''m inside the if/then'
                I'm inside the if/then


                References



                • 7.3. Other Comparison Operators





                share|improve this answer















                Here's how to debug something like this. I have a simplified version of your script.



                $ cat var_dumper.bash
                #!/bin/bash

                var1=1
                var2=2

                if [ ! "$var1" > "$var2" ]; then
                echo "I'm inside the if/then"
                fi


                And we run it:



                $ ./var_dumper.bash
                $ ls
                var_dumper.bash 2


                So we can see your 2 showing up as a file. That's your issue.



                Debugging



                To debug a shell script, I always start with the -x switch to Bash. You can run your script like this:



                $ bash -x ./var_dumper.bash
                + var1=1
                + var2=2
                + '[' '!' 1 ']'


                This tells us where it's failing. The if condition. So what's wrong with it? Well the first clue should be the >. That's a redirect operator, in other usages in Bash, so that's what's likely producing the file being written.



                So let's reformulate your example into a single executable:



                $ var1=1 var2=2 [ "$var1" > "$var2" ] && echo success || echo failure
                success


                The next clue should be, why is it returning a success, 1 isn't greater than 2. So we google and we discover that to compare integers in Bash you don't use the > operator, you're actually suppose to use this one: -gt.



                $ var1=1 var2=2 [ "$var1" -gt "$var2" ] && echo success || echo failure
                failure


                And since you originally wanted it negated, we could put the ! back in:



                $ var1=1 var2=2 [ ! "$var1" -gt "$var2" ] && echo success || echo failure
                success


                And it works. So your corrected if statement would look like this:



                if [ ! "$var1" -gt "$var2" ]; then
                echo "I'm inside the if/then"
                fi


                So what was wrong with >



                In your scenario you were using if [ .... ]. This form of if/then is the POSIX compliant version. This one does not support the > operator. The form that does support that is this, if [[ .... ]]. The double bracketed version is the more capable extended test command.



                So as an alternative:



                $ cat var_dumper.bash
                #!/bin/bash

                var1=1
                var2=2

                if [[ ! "$var1" > "$var2" ]]; then
                echo "I'm inside the if/then"
                fi


                Would also work:



                $ bash -x var_dumper.bash
                + var1=1
                + var2=2
                + [[ ! 1 > 2 ]]
                + echo 'I'''m inside the if/then'
                I'm inside the if/then


                References



                • 7.3. Other Comparison Operators






                share|improve this answer















                share|improve this answer



                share|improve this answer








                edited Jul 11 at 2:58


























                answered Jul 11 at 2:53









                slm♦

                233k65479651




                233k65479651






















                    up vote
                    0
                    down vote













                    Do not use > in a digital test. Because it is a redirection operator.



                    His execution is always true (if the file is written) :



                    $ if [ 2 > 30000 ]; then echo OK; else echo NO; fi
                    OK
                    $ ls
                    $ 30000
                    $ rm 30000


                    And a 30000 file was written.



                    There was no digital test but a file write, which returned true



                    $ mkdir tmp
                    $ chmod -w tmp
                    $ cd tmp
                    $ if [ 2 > 30000 ]; then echo OK; else echo NO; fi
                    -bash: 30000: Permission non accordée
                    NO
                    $ cd ..
                    $ rmdir tmp


                    Use -gt :



                    $ if [ 2 -gt 30000 ]; then echo OK; else echo NO; fi
                    NO
                    $ ls





                    share|improve this answer





















                    • gives the resolution to slim, thanks
                      – alux
                      Jul 12 at 20:56














                    up vote
                    0
                    down vote













                    Do not use > in a digital test. Because it is a redirection operator.



                    His execution is always true (if the file is written) :



                    $ if [ 2 > 30000 ]; then echo OK; else echo NO; fi
                    OK
                    $ ls
                    $ 30000
                    $ rm 30000


                    And a 30000 file was written.



                    There was no digital test but a file write, which returned true



                    $ mkdir tmp
                    $ chmod -w tmp
                    $ cd tmp
                    $ if [ 2 > 30000 ]; then echo OK; else echo NO; fi
                    -bash: 30000: Permission non accordée
                    NO
                    $ cd ..
                    $ rmdir tmp


                    Use -gt :



                    $ if [ 2 -gt 30000 ]; then echo OK; else echo NO; fi
                    NO
                    $ ls





                    share|improve this answer





















                    • gives the resolution to slim, thanks
                      – alux
                      Jul 12 at 20:56












                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    Do not use > in a digital test. Because it is a redirection operator.



                    His execution is always true (if the file is written) :



                    $ if [ 2 > 30000 ]; then echo OK; else echo NO; fi
                    OK
                    $ ls
                    $ 30000
                    $ rm 30000


                    And a 30000 file was written.



                    There was no digital test but a file write, which returned true



                    $ mkdir tmp
                    $ chmod -w tmp
                    $ cd tmp
                    $ if [ 2 > 30000 ]; then echo OK; else echo NO; fi
                    -bash: 30000: Permission non accordée
                    NO
                    $ cd ..
                    $ rmdir tmp


                    Use -gt :



                    $ if [ 2 -gt 30000 ]; then echo OK; else echo NO; fi
                    NO
                    $ ls





                    share|improve this answer













                    Do not use > in a digital test. Because it is a redirection operator.



                    His execution is always true (if the file is written) :



                    $ if [ 2 > 30000 ]; then echo OK; else echo NO; fi
                    OK
                    $ ls
                    $ 30000
                    $ rm 30000


                    And a 30000 file was written.



                    There was no digital test but a file write, which returned true



                    $ mkdir tmp
                    $ chmod -w tmp
                    $ cd tmp
                    $ if [ 2 > 30000 ]; then echo OK; else echo NO; fi
                    -bash: 30000: Permission non accordée
                    NO
                    $ cd ..
                    $ rmdir tmp


                    Use -gt :



                    $ if [ 2 -gt 30000 ]; then echo OK; else echo NO; fi
                    NO
                    $ ls






                    share|improve this answer













                    share|improve this answer



                    share|improve this answer











                    answered Jul 12 at 20:45









                    alux

                    364




                    364











                    • gives the resolution to slim, thanks
                      – alux
                      Jul 12 at 20:56
















                    • gives the resolution to slim, thanks
                      – alux
                      Jul 12 at 20:56















                    gives the resolution to slim, thanks
                    – alux
                    Jul 12 at 20:56




                    gives the resolution to slim, thanks
                    – alux
                    Jul 12 at 20:56












                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f454597%2fnumber-variables-appear-in-directory-when-i-run-bash-script%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?