Error while using 'if' with the count in Bash [on hold]
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
3
down vote
favorite
What's wrong with the following? I get a [: too many arguments
error.
if [ 'wc -l pid.txt | awk 'print $1'' -ge "1" ]
then
for line in $(cat pid.txt)
do
kill $line
done
else
rm pid.txt
fi
bash shell-script shell
put on hold as off-topic by ilkkachu, Rui F Ribeiro, slm⦠Aug 4 at 0:45
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." â ilkkachu, Rui F Ribeiro, slm
add a comment |Â
up vote
3
down vote
favorite
What's wrong with the following? I get a [: too many arguments
error.
if [ 'wc -l pid.txt | awk 'print $1'' -ge "1" ]
then
for line in $(cat pid.txt)
do
kill $line
done
else
rm pid.txt
fi
bash shell-script shell
put on hold as off-topic by ilkkachu, Rui F Ribeiro, slm⦠Aug 4 at 0:45
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." â ilkkachu, Rui F Ribeiro, slm
1
The single quotes around your command should be back-ticks. Is that a typo in your post or are you using the wrong symbol?
â Andy Dalton
Aug 3 at 20:30
oops I feel dumb. I am using single quotes and thats the problem. Thanks a lot.
â Vas
Aug 3 at 20:36
1
@Vas Your "problem" was the single quotes, but there's a lot more that could be improved.
â Kusalananda
Aug 3 at 20:58
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
What's wrong with the following? I get a [: too many arguments
error.
if [ 'wc -l pid.txt | awk 'print $1'' -ge "1" ]
then
for line in $(cat pid.txt)
do
kill $line
done
else
rm pid.txt
fi
bash shell-script shell
What's wrong with the following? I get a [: too many arguments
error.
if [ 'wc -l pid.txt | awk 'print $1'' -ge "1" ]
then
for line in $(cat pid.txt)
do
kill $line
done
else
rm pid.txt
fi
bash shell-script shell
edited Aug 4 at 0:40
Peter Mortensen
76348
76348
asked Aug 3 at 20:19
Vas
203
203
put on hold as off-topic by ilkkachu, Rui F Ribeiro, slm⦠Aug 4 at 0:45
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." â ilkkachu, Rui F Ribeiro, slm
put on hold as off-topic by ilkkachu, Rui F Ribeiro, slm⦠Aug 4 at 0:45
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." â ilkkachu, Rui F Ribeiro, slm
1
The single quotes around your command should be back-ticks. Is that a typo in your post or are you using the wrong symbol?
â Andy Dalton
Aug 3 at 20:30
oops I feel dumb. I am using single quotes and thats the problem. Thanks a lot.
â Vas
Aug 3 at 20:36
1
@Vas Your "problem" was the single quotes, but there's a lot more that could be improved.
â Kusalananda
Aug 3 at 20:58
add a comment |Â
1
The single quotes around your command should be back-ticks. Is that a typo in your post or are you using the wrong symbol?
â Andy Dalton
Aug 3 at 20:30
oops I feel dumb. I am using single quotes and thats the problem. Thanks a lot.
â Vas
Aug 3 at 20:36
1
@Vas Your "problem" was the single quotes, but there's a lot more that could be improved.
â Kusalananda
Aug 3 at 20:58
1
1
The single quotes around your command should be back-ticks. Is that a typo in your post or are you using the wrong symbol?
â Andy Dalton
Aug 3 at 20:30
The single quotes around your command should be back-ticks. Is that a typo in your post or are you using the wrong symbol?
â Andy Dalton
Aug 3 at 20:30
oops I feel dumb. I am using single quotes and thats the problem. Thanks a lot.
â Vas
Aug 3 at 20:36
oops I feel dumb. I am using single quotes and thats the problem. Thanks a lot.
â Vas
Aug 3 at 20:36
1
1
@Vas Your "problem" was the single quotes, but there's a lot more that could be improved.
â Kusalananda
Aug 3 at 20:58
@Vas Your "problem" was the single quotes, but there's a lot more that could be improved.
â Kusalananda
Aug 3 at 20:58
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
6
down vote
Improved further thanks to Kusalananda.
- Testing for contents of the file is not necessary as the
while read
loop won't run on an empty file. - You could then simply use
[ -s pid.txt ] || rm pid.txt
to remove the pid file. However, do you really have a reason to keep the file after processing? It seems likely you would want to remove it regardless.
while read -r line
do
kill "$line"
done < pid.txt
[ -s pid.txt ] || rm pid.txt
A while read
loop is being used instead
of for $(cat ...)
. Don't read lines with for.
The -r
option specifies "do not allow backslashes to escape any characters" which shouldn't really be an issue in a file of pids, but it is best practice to always set unless there is a specific reason not to.
3
The initial test can be removed completely as the read loop won't run if it's an empty file. Therm
could be replaced by[ -s pid.txt ] && rm pid.txt
to remove the file if it's empty. The inner loop could be replaced byxargs -r kill <pid.txt
.
â Kusalananda
Aug 3 at 20:52
One could alsogrep . pid.txt | while read ...
if there's empty lines in thepid.txt
file that one wants to avoid.
â Kusalananda
Aug 3 at 21:09
1
[ -s pid.txt ] || rm pid.txt
to remove if empty, right?
â ilkkachu
Aug 3 at 23:48
Good catch @ilkkachu, thanks
â Jesse_b
Aug 3 at 23:55
@ilkkachu Sorry, got the test backwards... duh.
â Kusalananda
2 days ago
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
Improved further thanks to Kusalananda.
- Testing for contents of the file is not necessary as the
while read
loop won't run on an empty file. - You could then simply use
[ -s pid.txt ] || rm pid.txt
to remove the pid file. However, do you really have a reason to keep the file after processing? It seems likely you would want to remove it regardless.
while read -r line
do
kill "$line"
done < pid.txt
[ -s pid.txt ] || rm pid.txt
A while read
loop is being used instead
of for $(cat ...)
. Don't read lines with for.
The -r
option specifies "do not allow backslashes to escape any characters" which shouldn't really be an issue in a file of pids, but it is best practice to always set unless there is a specific reason not to.
3
The initial test can be removed completely as the read loop won't run if it's an empty file. Therm
could be replaced by[ -s pid.txt ] && rm pid.txt
to remove the file if it's empty. The inner loop could be replaced byxargs -r kill <pid.txt
.
â Kusalananda
Aug 3 at 20:52
One could alsogrep . pid.txt | while read ...
if there's empty lines in thepid.txt
file that one wants to avoid.
â Kusalananda
Aug 3 at 21:09
1
[ -s pid.txt ] || rm pid.txt
to remove if empty, right?
â ilkkachu
Aug 3 at 23:48
Good catch @ilkkachu, thanks
â Jesse_b
Aug 3 at 23:55
@ilkkachu Sorry, got the test backwards... duh.
â Kusalananda
2 days ago
add a comment |Â
up vote
6
down vote
Improved further thanks to Kusalananda.
- Testing for contents of the file is not necessary as the
while read
loop won't run on an empty file. - You could then simply use
[ -s pid.txt ] || rm pid.txt
to remove the pid file. However, do you really have a reason to keep the file after processing? It seems likely you would want to remove it regardless.
while read -r line
do
kill "$line"
done < pid.txt
[ -s pid.txt ] || rm pid.txt
A while read
loop is being used instead
of for $(cat ...)
. Don't read lines with for.
The -r
option specifies "do not allow backslashes to escape any characters" which shouldn't really be an issue in a file of pids, but it is best practice to always set unless there is a specific reason not to.
3
The initial test can be removed completely as the read loop won't run if it's an empty file. Therm
could be replaced by[ -s pid.txt ] && rm pid.txt
to remove the file if it's empty. The inner loop could be replaced byxargs -r kill <pid.txt
.
â Kusalananda
Aug 3 at 20:52
One could alsogrep . pid.txt | while read ...
if there's empty lines in thepid.txt
file that one wants to avoid.
â Kusalananda
Aug 3 at 21:09
1
[ -s pid.txt ] || rm pid.txt
to remove if empty, right?
â ilkkachu
Aug 3 at 23:48
Good catch @ilkkachu, thanks
â Jesse_b
Aug 3 at 23:55
@ilkkachu Sorry, got the test backwards... duh.
â Kusalananda
2 days ago
add a comment |Â
up vote
6
down vote
up vote
6
down vote
Improved further thanks to Kusalananda.
- Testing for contents of the file is not necessary as the
while read
loop won't run on an empty file. - You could then simply use
[ -s pid.txt ] || rm pid.txt
to remove the pid file. However, do you really have a reason to keep the file after processing? It seems likely you would want to remove it regardless.
while read -r line
do
kill "$line"
done < pid.txt
[ -s pid.txt ] || rm pid.txt
A while read
loop is being used instead
of for $(cat ...)
. Don't read lines with for.
The -r
option specifies "do not allow backslashes to escape any characters" which shouldn't really be an issue in a file of pids, but it is best practice to always set unless there is a specific reason not to.
Improved further thanks to Kusalananda.
- Testing for contents of the file is not necessary as the
while read
loop won't run on an empty file. - You could then simply use
[ -s pid.txt ] || rm pid.txt
to remove the pid file. However, do you really have a reason to keep the file after processing? It seems likely you would want to remove it regardless.
while read -r line
do
kill "$line"
done < pid.txt
[ -s pid.txt ] || rm pid.txt
A while read
loop is being used instead
of for $(cat ...)
. Don't read lines with for.
The -r
option specifies "do not allow backslashes to escape any characters" which shouldn't really be an issue in a file of pids, but it is best practice to always set unless there is a specific reason not to.
edited Aug 4 at 0:40
Peter Mortensen
76348
76348
answered Aug 3 at 20:38
Jesse_b
10.1k12658
10.1k12658
3
The initial test can be removed completely as the read loop won't run if it's an empty file. Therm
could be replaced by[ -s pid.txt ] && rm pid.txt
to remove the file if it's empty. The inner loop could be replaced byxargs -r kill <pid.txt
.
â Kusalananda
Aug 3 at 20:52
One could alsogrep . pid.txt | while read ...
if there's empty lines in thepid.txt
file that one wants to avoid.
â Kusalananda
Aug 3 at 21:09
1
[ -s pid.txt ] || rm pid.txt
to remove if empty, right?
â ilkkachu
Aug 3 at 23:48
Good catch @ilkkachu, thanks
â Jesse_b
Aug 3 at 23:55
@ilkkachu Sorry, got the test backwards... duh.
â Kusalananda
2 days ago
add a comment |Â
3
The initial test can be removed completely as the read loop won't run if it's an empty file. Therm
could be replaced by[ -s pid.txt ] && rm pid.txt
to remove the file if it's empty. The inner loop could be replaced byxargs -r kill <pid.txt
.
â Kusalananda
Aug 3 at 20:52
One could alsogrep . pid.txt | while read ...
if there's empty lines in thepid.txt
file that one wants to avoid.
â Kusalananda
Aug 3 at 21:09
1
[ -s pid.txt ] || rm pid.txt
to remove if empty, right?
â ilkkachu
Aug 3 at 23:48
Good catch @ilkkachu, thanks
â Jesse_b
Aug 3 at 23:55
@ilkkachu Sorry, got the test backwards... duh.
â Kusalananda
2 days ago
3
3
The initial test can be removed completely as the read loop won't run if it's an empty file. The
rm
could be replaced by [ -s pid.txt ] && rm pid.txt
to remove the file if it's empty. The inner loop could be replaced by xargs -r kill <pid.txt
.â Kusalananda
Aug 3 at 20:52
The initial test can be removed completely as the read loop won't run if it's an empty file. The
rm
could be replaced by [ -s pid.txt ] && rm pid.txt
to remove the file if it's empty. The inner loop could be replaced by xargs -r kill <pid.txt
.â Kusalananda
Aug 3 at 20:52
One could also
grep . pid.txt | while read ...
if there's empty lines in the pid.txt
file that one wants to avoid.â Kusalananda
Aug 3 at 21:09
One could also
grep . pid.txt | while read ...
if there's empty lines in the pid.txt
file that one wants to avoid.â Kusalananda
Aug 3 at 21:09
1
1
[ -s pid.txt ] || rm pid.txt
to remove if empty, right?â ilkkachu
Aug 3 at 23:48
[ -s pid.txt ] || rm pid.txt
to remove if empty, right?â ilkkachu
Aug 3 at 23:48
Good catch @ilkkachu, thanks
â Jesse_b
Aug 3 at 23:55
Good catch @ilkkachu, thanks
â Jesse_b
Aug 3 at 23:55
@ilkkachu Sorry, got the test backwards... duh.
â Kusalananda
2 days ago
@ilkkachu Sorry, got the test backwards... duh.
â Kusalananda
2 days ago
add a comment |Â
1
The single quotes around your command should be back-ticks. Is that a typo in your post or are you using the wrong symbol?
â Andy Dalton
Aug 3 at 20:30
oops I feel dumb. I am using single quotes and thats the problem. Thanks a lot.
â Vas
Aug 3 at 20:36
1
@Vas Your "problem" was the single quotes, but there's a lot more that could be improved.
â Kusalananda
Aug 3 at 20:58