Solved

Stuck with ls

Posted on 1998-07-03
2
454 Views
Last Modified: 2010-04-21
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
Comment
Question by:datavalue
2 Comments
 

Accepted Solution

by:
singlep2 earned 80 total points
ID: 2008434
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
 
LVL 3

Expert Comment

by:braveheart
ID: 2008435
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question