![]() |
problem with assigning output of ls command to variable when ls produces error
Code:
count=`ls *.php -l | wc -l`ls: cannot access *.php: No such file or directory Typically I would use 2> /dev/null to handle output suppression but in this case it prevents the variable assignments. Any help would be appreciated. |
Code:
count=$(ls *.php 2>/dev/null | wc -l)A simpler way of doing this if you don't need to report how many files were moved would be: Code:
if ! mv *.php ~/Desktop/PHP 2>/dev/null; then |
That ls command is no good. The -l should come before the *.php
Where are you putting the redirection of stderr to /dev/null? If you put it after the ls command it would be fine as wc will then return 0. Use of backticks for variable assignment is depreciated. Use $() Code:
foo=$(command) |
On second thought, my code doesn't do exactly what yours is trying to do, and there can be more reasons than files not found for mv to fail, such as lack of disk space.
|
Quote:
|
Final code:
Code:
count=$(ls -l *.php 2>/dev/null | wc -l) |
| All times are GMT -5. The time now is 09:41 PM. |