LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   problem with assigning output of ls command to variable when ls produces error (http://www.linuxquestions.org/questions/linux-newbie-8/problem-with-assigning-output-of-ls-command-to-variable-when-ls-produces-error-878044/)

tanasqui 04-30-2011 02:35 PM

problem with assigning output of ls command to variable when ls produces error
 
Code:

count=`ls *.php -l | wc -l`
  if [ "$count" -ne "0" ]; then
    mv  *.php ~/Desktop/PHP
    echo "moved $count php files"
  else
    echo "$count php files found"
  fi

With this code I am attempting to ensure a php file exists, then attempting to move it to another folder. My script has 40 or so extensions, this is one of many. My problem is this: if the current folder contains no php files i receive an error.

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.

penguiniator 04-30-2011 04:24 PM

Code:

count=$(ls *.php 2>/dev/null | wc -l)
This should not be a problem. Wc will count 0 lines before EOF, and redirecting error output should not affect the variable assignment. What shell are you using? I'm using bash here. BTW, you don't need -l with ls if all you are doing is counting lines of output.

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
    echo "0 php files found"
fi


arizonagroovejet 04-30-2011 04:24 PM

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)

penguiniator 04-30-2011 04:31 PM

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.

tanasqui 04-30-2011 04:58 PM

Quote:

Originally Posted by penguiniator (Post 4342230)
Code:

count=$(ls *.php 2>/dev/null | wc -l)
This should not be a problem. Wc will count 0 lines before EOF, and redirecting error output should not affect the variable assignment. What shell are you using? I'm using bash here. BTW, you don't need -l with ls if all you are doing is counting lines of output.

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
    echo "0 php files found"
fi


The first suggestion works well. Thanks for the replies.

tanasqui 04-30-2011 05:02 PM

Final code:
Code:

count=$(ls -l  *.php 2>/dev/null | wc -l)
  if [ "$count" -ne "0" ]; then
    mv  *.php ~/Desktop/PHP
    echo -e "moved $count php files"
  else
    echo -e "$count php files found"
  fi

Again, Thanks for the help.


All times are GMT -5. The time now is 11:41 AM.