Error while using 'if' with the count in Bash [on hold]

The name of the pictureThe name of the pictureThe name of the pictureClash 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






share|improve this question













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
If this question can be reworded to fit the rules in the help center, please edit the question.








  • 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
















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






share|improve this question













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
If this question can be reworded to fit the rules in the help center, please edit the question.








  • 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












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






share|improve this question













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








share|improve this question












share|improve this question




share|improve this question








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
If this question can be reworded to fit the rules in the help center, please edit the question.




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
If this question can be reworded to fit the rules in the help center, please edit the question.







  • 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




    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










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.






share|improve this answer



















  • 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











  • 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




    [ -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

















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.






share|improve this answer



















  • 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











  • 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




    [ -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














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.






share|improve this answer



















  • 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











  • 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




    [ -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












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.






share|improve this answer















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.







share|improve this answer















share|improve this answer



share|improve this answer








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. 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






  • 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




    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






  • 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


Popular posts from this blog

How to check contact read email or not when send email to Individual?

Christian Cage

How to properly install USB display driver for Fresco Logic FL2000DX on Ubuntu?