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

Clash 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
shell-script
add a comment |Â
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
shell-script
You may find www.shellcheck.net useful
â steeldriver
Jan 15 at 18:50
add a comment |Â
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
shell-script
#!/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
shell-script
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
add a comment |Â
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
add a comment |Â
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 ] && ...
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
add a comment |Â
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
-nerather than!=, since we are comparing integers.
add a comment |Â
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
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
add a comment |Â
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.
add a comment |Â
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 ] && ...
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
add a comment |Â
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 ] && ...
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
add a comment |Â
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 ] && ...
[ $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 ] && ...
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
add a comment |Â
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
add a comment |Â
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
-nerather than!=, since we are comparing integers.
add a comment |Â
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
-nerather than!=, since we are comparing integers.
add a comment |Â
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
-nerather than!=, since we are comparing integers.
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
-nerather than!=, since we are comparing integers.
answered Jun 9 at 19:59
awsed r
214
214
add a comment |Â
add a comment |Â
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
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
add a comment |Â
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
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
add a comment |Â
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
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
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
add a comment |Â
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
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
answered Jun 9 at 20:07
Kusalananda
103k13203321
103k13203321
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
You may find www.shellcheck.net useful
â steeldriver
Jan 15 at 18:50