Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 467
  • Last Modified:

Stuck with ls

I need to list all the files in a directory so my call in a C program is system(syscmd)
where syscmd is "ls -l /tmp/receive/*.* > /tmp/receivelist"
however if there are no files in the directory receive my command fails as ls /tmp/receive/*.* returns no files found.
Any suggestions how to stop the error message and determine a list of files in a directory using a wildcard pattern?
0
datavalue
Asked:
datavalue
1 Solution
 
singlep2Commented:
As you are using ls -l I shall assume you are running your C program in a UNIX environment.

I shall also assume that you are specifically using *.* because you don't want to include the receivelist file (or any other file containing a ".") in your list of files.

To stop the ls command returning the std err message use:

ls -l /tmp/receive/*.* >/tmp/receivelist 2>/dev/null

The 2 is for std err output which in this case is redirected to the UNIX bit bucket in the sky -> /dev/null.

Hope this is what you want.

Paul Singleton
0
 
braveheartCommented:
If you are executing the command in the C-shell, i.e. if your normal login shell is the C-shell (or tcsh) you should set the shell (not envt.) variable "nonomatch" either in ~/.cshrc or as part of the syscmd. This should suppress the error message and status code so that the generated output will be empty.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now