problem in using if statement error -> [: missing `]'

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











up vote
0
down vote

favorite












#!/bin/bash

for (( x=7; x <= 65; x+=2 ))

do


if [ $x!=57 && $x!=59 && $x!=29 && $x!=53 ]
then
cd charged_$x
cp -f ~aman/user_ana_normal_mode/Testforaman/user_ana.so .
cd ..
fi

done


can't figure out the problem , please help







share|improve this question






















  • You may find www.shellcheck.net useful
    – steeldriver
    Jan 15 at 18:50














up vote
0
down vote

favorite












#!/bin/bash

for (( x=7; x <= 65; x+=2 ))

do


if [ $x!=57 && $x!=59 && $x!=29 && $x!=53 ]
then
cd charged_$x
cp -f ~aman/user_ana_normal_mode/Testforaman/user_ana.so .
cd ..
fi

done


can't figure out the problem , please help







share|improve this question






















  • You may find www.shellcheck.net useful
    – steeldriver
    Jan 15 at 18:50












up vote
0
down vote

favorite









up vote
0
down vote

favorite











#!/bin/bash

for (( x=7; x <= 65; x+=2 ))

do


if [ $x!=57 && $x!=59 && $x!=29 && $x!=53 ]
then
cd charged_$x
cp -f ~aman/user_ana_normal_mode/Testforaman/user_ana.so .
cd ..
fi

done


can't figure out the problem , please help







share|improve this question














#!/bin/bash

for (( x=7; x <= 65; x+=2 ))

do


if [ $x!=57 && $x!=59 && $x!=29 && $x!=53 ]
then
cd charged_$x
cp -f ~aman/user_ana_normal_mode/Testforaman/user_ana.so .
cd ..
fi

done


can't figure out the problem , please help









share|improve this question













share|improve this question




share|improve this question








edited Jan 15 at 18:42









francois P

914114




914114










asked Jan 15 at 18:40









AMAN SANGAL

11




11











  • You may find www.shellcheck.net useful
    – steeldriver
    Jan 15 at 18:50
















  • You may find www.shellcheck.net useful
    – steeldriver
    Jan 15 at 18:50















You may find www.shellcheck.net useful
– steeldriver
Jan 15 at 18:50




You may find www.shellcheck.net useful
– steeldriver
Jan 15 at 18:50










4 Answers
4






active

oldest

votes

















up vote
4
down vote













[ $x!=57 && $x!=59 && $x!=29 && $x!=53 ]


[ is a command, it obeys the same rules as, say ls. && is a shell way of conditionally grouping commands. In [ foo && bar ], the shell runs the command [ foo, and if it succeeds, it runs bar ]. The [ command fails because it expects to see a ] as its last argument, and so the second command does not run.



The special conditional syntax [[ .. ]] however does support && within it, so you could use [[ $x != 57 && $x != 59 && ... ]]. Note that it still requires whitespace around the operators. Plain $x!=57 would be a test to see if $x!=57 is a nonempty string, which it always is, regardless of the value of x.



Or, put the closing ] before the &&: [ "$x" != 57 ] && [ "$x" != 59 ] && ...






share|improve this answer




















  • Strictly speaking, you don't need whitespace aroung &&: [[ $x != 57&&$x != 59 ]] or [ "$x" != 57 ]&&[ "$x" != 59 ] work.
    – Stéphane Chazelas
    Jun 9 at 20:49

















up vote
2
down vote













The following code should work:



#!/bin/bash

for (( x=7; x <= 65; x+=2 ))
do
if [ "$x" -ne 57 ] && [ "$x" -ne 59 ] && [ "$x" -ne 29 ] && [ "$x" -ne 53 ]; then
cd charged_$x
cp -f ~aman/user_ana_normal_mode/Testforaman/user_ana.so .
cd ..
fi
done


  • Its better to have separate braces for each condition.

  • We should use -ne rather than !=, since we are comparing integers.





share|improve this answer



























    up vote
    0
    down vote













    you use too many conditions at same time so you need to change syntax for that .



    if [ $x!=57 ] && [ $x!=59 ] && [ $x!=29 ] && [ $x!=53 ]


    this will work






    share|improve this answer
















    • 2




      Also note that != is intended for strings, while numeric comparisons could/should use -ne.
      – Jeff Schaller
      Jan 15 at 19:08






    • 2




      by putting spaces around comparison operator != it worked fine. thanks everyone
      – AMAN SANGAL
      Jan 15 at 19:11

















    up vote
    0
    down vote













    Your code has two errors: The != operator needs spaces around it, and the && should be between individual [ ... ] tests.



    Rather than using a lot of individual tests:



    for (( x = 7; x <= 65; x += 2 )); do
    case $x in
    29|53|57|59) ;;
    *)
    cp -f ~aman/user_ana_normal_mode/Testforaman/user_ana.so "charged_$x"
    esac
    done


    I've also removed the half useless cd.






    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%2f417313%2fproblem-in-using-if-statement-error-missing%23new-answer', 'question_page');

      );

      Post as a guest






























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      4
      down vote













      [ $x!=57 && $x!=59 && $x!=29 && $x!=53 ]


      [ is a command, it obeys the same rules as, say ls. && is a shell way of conditionally grouping commands. In [ foo && bar ], the shell runs the command [ foo, and if it succeeds, it runs bar ]. The [ command fails because it expects to see a ] as its last argument, and so the second command does not run.



      The special conditional syntax [[ .. ]] however does support && within it, so you could use [[ $x != 57 && $x != 59 && ... ]]. Note that it still requires whitespace around the operators. Plain $x!=57 would be a test to see if $x!=57 is a nonempty string, which it always is, regardless of the value of x.



      Or, put the closing ] before the &&: [ "$x" != 57 ] && [ "$x" != 59 ] && ...






      share|improve this answer




















      • Strictly speaking, you don't need whitespace aroung &&: [[ $x != 57&&$x != 59 ]] or [ "$x" != 57 ]&&[ "$x" != 59 ] work.
        – Stéphane Chazelas
        Jun 9 at 20:49














      up vote
      4
      down vote













      [ $x!=57 && $x!=59 && $x!=29 && $x!=53 ]


      [ is a command, it obeys the same rules as, say ls. && is a shell way of conditionally grouping commands. In [ foo && bar ], the shell runs the command [ foo, and if it succeeds, it runs bar ]. The [ command fails because it expects to see a ] as its last argument, and so the second command does not run.



      The special conditional syntax [[ .. ]] however does support && within it, so you could use [[ $x != 57 && $x != 59 && ... ]]. Note that it still requires whitespace around the operators. Plain $x!=57 would be a test to see if $x!=57 is a nonempty string, which it always is, regardless of the value of x.



      Or, put the closing ] before the &&: [ "$x" != 57 ] && [ "$x" != 59 ] && ...






      share|improve this answer




















      • Strictly speaking, you don't need whitespace aroung &&: [[ $x != 57&&$x != 59 ]] or [ "$x" != 57 ]&&[ "$x" != 59 ] work.
        – Stéphane Chazelas
        Jun 9 at 20:49












      up vote
      4
      down vote










      up vote
      4
      down vote









      [ $x!=57 && $x!=59 && $x!=29 && $x!=53 ]


      [ is a command, it obeys the same rules as, say ls. && is a shell way of conditionally grouping commands. In [ foo && bar ], the shell runs the command [ foo, and if it succeeds, it runs bar ]. The [ command fails because it expects to see a ] as its last argument, and so the second command does not run.



      The special conditional syntax [[ .. ]] however does support && within it, so you could use [[ $x != 57 && $x != 59 && ... ]]. Note that it still requires whitespace around the operators. Plain $x!=57 would be a test to see if $x!=57 is a nonempty string, which it always is, regardless of the value of x.



      Or, put the closing ] before the &&: [ "$x" != 57 ] && [ "$x" != 59 ] && ...






      share|improve this answer












      [ $x!=57 && $x!=59 && $x!=29 && $x!=53 ]


      [ is a command, it obeys the same rules as, say ls. && is a shell way of conditionally grouping commands. In [ foo && bar ], the shell runs the command [ foo, and if it succeeds, it runs bar ]. The [ command fails because it expects to see a ] as its last argument, and so the second command does not run.



      The special conditional syntax [[ .. ]] however does support && within it, so you could use [[ $x != 57 && $x != 59 && ... ]]. Note that it still requires whitespace around the operators. Plain $x!=57 would be a test to see if $x!=57 is a nonempty string, which it always is, regardless of the value of x.



      Or, put the closing ] before the &&: [ "$x" != 57 ] && [ "$x" != 59 ] && ...







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Jan 15 at 21:18









      ilkkachu

      49.8k674137




      49.8k674137











      • Strictly speaking, you don't need whitespace aroung &&: [[ $x != 57&&$x != 59 ]] or [ "$x" != 57 ]&&[ "$x" != 59 ] work.
        – Stéphane Chazelas
        Jun 9 at 20:49
















      • Strictly speaking, you don't need whitespace aroung &&: [[ $x != 57&&$x != 59 ]] or [ "$x" != 57 ]&&[ "$x" != 59 ] work.
        – Stéphane Chazelas
        Jun 9 at 20:49















      Strictly speaking, you don't need whitespace aroung &&: [[ $x != 57&&$x != 59 ]] or [ "$x" != 57 ]&&[ "$x" != 59 ] work.
      – Stéphane Chazelas
      Jun 9 at 20:49




      Strictly speaking, you don't need whitespace aroung &&: [[ $x != 57&&$x != 59 ]] or [ "$x" != 57 ]&&[ "$x" != 59 ] work.
      – Stéphane Chazelas
      Jun 9 at 20:49












      up vote
      2
      down vote













      The following code should work:



      #!/bin/bash

      for (( x=7; x <= 65; x+=2 ))
      do
      if [ "$x" -ne 57 ] && [ "$x" -ne 59 ] && [ "$x" -ne 29 ] && [ "$x" -ne 53 ]; then
      cd charged_$x
      cp -f ~aman/user_ana_normal_mode/Testforaman/user_ana.so .
      cd ..
      fi
      done


      • Its better to have separate braces for each condition.

      • We should use -ne rather than !=, since we are comparing integers.





      share|improve this answer
























        up vote
        2
        down vote













        The following code should work:



        #!/bin/bash

        for (( x=7; x <= 65; x+=2 ))
        do
        if [ "$x" -ne 57 ] && [ "$x" -ne 59 ] && [ "$x" -ne 29 ] && [ "$x" -ne 53 ]; then
        cd charged_$x
        cp -f ~aman/user_ana_normal_mode/Testforaman/user_ana.so .
        cd ..
        fi
        done


        • Its better to have separate braces for each condition.

        • We should use -ne rather than !=, since we are comparing integers.





        share|improve this answer






















          up vote
          2
          down vote










          up vote
          2
          down vote









          The following code should work:



          #!/bin/bash

          for (( x=7; x <= 65; x+=2 ))
          do
          if [ "$x" -ne 57 ] && [ "$x" -ne 59 ] && [ "$x" -ne 29 ] && [ "$x" -ne 53 ]; then
          cd charged_$x
          cp -f ~aman/user_ana_normal_mode/Testforaman/user_ana.so .
          cd ..
          fi
          done


          • Its better to have separate braces for each condition.

          • We should use -ne rather than !=, since we are comparing integers.





          share|improve this answer












          The following code should work:



          #!/bin/bash

          for (( x=7; x <= 65; x+=2 ))
          do
          if [ "$x" -ne 57 ] && [ "$x" -ne 59 ] && [ "$x" -ne 29 ] && [ "$x" -ne 53 ]; then
          cd charged_$x
          cp -f ~aman/user_ana_normal_mode/Testforaman/user_ana.so .
          cd ..
          fi
          done


          • Its better to have separate braces for each condition.

          • We should use -ne rather than !=, since we are comparing integers.






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jun 9 at 19:59









          awsed r

          214




          214




















              up vote
              0
              down vote













              you use too many conditions at same time so you need to change syntax for that .



              if [ $x!=57 ] && [ $x!=59 ] && [ $x!=29 ] && [ $x!=53 ]


              this will work






              share|improve this answer
















              • 2




                Also note that != is intended for strings, while numeric comparisons could/should use -ne.
                – Jeff Schaller
                Jan 15 at 19:08






              • 2




                by putting spaces around comparison operator != it worked fine. thanks everyone
                – AMAN SANGAL
                Jan 15 at 19:11














              up vote
              0
              down vote













              you use too many conditions at same time so you need to change syntax for that .



              if [ $x!=57 ] && [ $x!=59 ] && [ $x!=29 ] && [ $x!=53 ]


              this will work






              share|improve this answer
















              • 2




                Also note that != is intended for strings, while numeric comparisons could/should use -ne.
                – Jeff Schaller
                Jan 15 at 19:08






              • 2




                by putting spaces around comparison operator != it worked fine. thanks everyone
                – AMAN SANGAL
                Jan 15 at 19:11












              up vote
              0
              down vote










              up vote
              0
              down vote









              you use too many conditions at same time so you need to change syntax for that .



              if [ $x!=57 ] && [ $x!=59 ] && [ $x!=29 ] && [ $x!=53 ]


              this will work






              share|improve this answer












              you use too many conditions at same time so you need to change syntax for that .



              if [ $x!=57 ] && [ $x!=59 ] && [ $x!=29 ] && [ $x!=53 ]


              this will work







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jan 15 at 18:44









              francois P

              914114




              914114







              • 2




                Also note that != is intended for strings, while numeric comparisons could/should use -ne.
                – Jeff Schaller
                Jan 15 at 19:08






              • 2




                by putting spaces around comparison operator != it worked fine. thanks everyone
                – AMAN SANGAL
                Jan 15 at 19:11












              • 2




                Also note that != is intended for strings, while numeric comparisons could/should use -ne.
                – Jeff Schaller
                Jan 15 at 19:08






              • 2




                by putting spaces around comparison operator != it worked fine. thanks everyone
                – AMAN SANGAL
                Jan 15 at 19:11







              2




              2




              Also note that != is intended for strings, while numeric comparisons could/should use -ne.
              – Jeff Schaller
              Jan 15 at 19:08




              Also note that != is intended for strings, while numeric comparisons could/should use -ne.
              – Jeff Schaller
              Jan 15 at 19:08




              2




              2




              by putting spaces around comparison operator != it worked fine. thanks everyone
              – AMAN SANGAL
              Jan 15 at 19:11




              by putting spaces around comparison operator != it worked fine. thanks everyone
              – AMAN SANGAL
              Jan 15 at 19:11










              up vote
              0
              down vote













              Your code has two errors: The != operator needs spaces around it, and the && should be between individual [ ... ] tests.



              Rather than using a lot of individual tests:



              for (( x = 7; x <= 65; x += 2 )); do
              case $x in
              29|53|57|59) ;;
              *)
              cp -f ~aman/user_ana_normal_mode/Testforaman/user_ana.so "charged_$x"
              esac
              done


              I've also removed the half useless cd.






              share|improve this answer
























                up vote
                0
                down vote













                Your code has two errors: The != operator needs spaces around it, and the && should be between individual [ ... ] tests.



                Rather than using a lot of individual tests:



                for (( x = 7; x <= 65; x += 2 )); do
                case $x in
                29|53|57|59) ;;
                *)
                cp -f ~aman/user_ana_normal_mode/Testforaman/user_ana.so "charged_$x"
                esac
                done


                I've also removed the half useless cd.






                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Your code has two errors: The != operator needs spaces around it, and the && should be between individual [ ... ] tests.



                  Rather than using a lot of individual tests:



                  for (( x = 7; x <= 65; x += 2 )); do
                  case $x in
                  29|53|57|59) ;;
                  *)
                  cp -f ~aman/user_ana_normal_mode/Testforaman/user_ana.so "charged_$x"
                  esac
                  done


                  I've also removed the half useless cd.






                  share|improve this answer












                  Your code has two errors: The != operator needs spaces around it, and the && should be between individual [ ... ] tests.



                  Rather than using a lot of individual tests:



                  for (( x = 7; x <= 65; x += 2 )); do
                  case $x in
                  29|53|57|59) ;;
                  *)
                  cp -f ~aman/user_ana_normal_mode/Testforaman/user_ana.so "charged_$x"
                  esac
                  done


                  I've also removed the half useless cd.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jun 9 at 20:07









                  Kusalananda

                  103k13203321




                  103k13203321






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f417313%2fproblem-in-using-if-statement-error-missing%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      Peggy Mitchell

                      Palaiologos

                      The Forum (Inglewood, California)