LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 04-30-2011, 02:35 PM   #1
tanasqui
LQ Newbie
 
Registered: Apr 2011
Location: Johnson City, TN
Distribution: BTr2, Ubuntu 10 & 11
Posts: 5

Rep: Reputation: 0
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.
 
Old 04-30-2011, 04:24 PM   #2
penguiniator
Member
 
Registered: Feb 2004
Location: Olympia, WA
Distribution: SolydK
Posts: 442
Blog Entries: 3

Rep: Reputation: 60
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
 
Old 04-30-2011, 04:24 PM   #3
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: SLED, openSUSE
Posts: 1,009

Rep: Reputation: 171Reputation: 171
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)
 
Old 04-30-2011, 04:31 PM   #4
penguiniator
Member
 
Registered: Feb 2004
Location: Olympia, WA
Distribution: SolydK
Posts: 442
Blog Entries: 3

Rep: Reputation: 60
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.
 
Old 04-30-2011, 04:58 PM   #5
tanasqui
LQ Newbie
 
Registered: Apr 2011
Location: Johnson City, TN
Distribution: BTr2, Ubuntu 10 & 11
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by penguiniator View Post
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.
 
Old 04-30-2011, 05:02 PM   #6
tanasqui
LQ Newbie
 
Registered: Apr 2011
Location: Johnson City, TN
Distribution: BTr2, Ubuntu 10 & 11
Posts: 5

Original Poster
Rep: Reputation: 0
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Sed: assigning its output to a variable. stf92 Linux - Newbie 3 02-04-2011 12:38 AM
[SOLVED] Assigning Output of a Command to an Array in Perl devUnix Programming 10 11-27-2010 05:43 PM
Odd problem with making a variable the output of a command in a shell script linux=future Programming 3 12-13-2005 09:45 PM
in C, Assigning output of system() to a variable Miaire Programming 4 01-30-2005 12:40 PM
Assigning the output of one command to a variable (shell) guru_stew Programming 5 08-03-2003 06:12 PM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration