LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-02-2005, 02:27 PM   #1
Rotwang
Member
 
Registered: Jan 2004
Distribution: CentOS
Posts: 281

Rep: Reputation: 30
shell script that checks for existence of files


I'm not very good with shell scripts, what I need to do is:

if (a file exists in directory x) {
echo 'warning: ' and file names;
}

or, maybe even better:

run perl script a;
if (perl script a's results are not '0') {
echo 'warning: ' and the script's results;
}


If you guys want just point me at a good shell tutorial that's cool too...

I have bash, btw.

thnx
 
Old 12-02-2005, 02:34 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
that's not a shell script, it's a one liner... if [ -e filename ]; then echo file exists; fi.
 
Old 12-02-2005, 02:35 PM   #3
Rotwang
Member
 
Registered: Jan 2004
Distribution: CentOS
Posts: 281

Original Poster
Rep: Reputation: 30
well, except that I want to check if any file exists in the directory. not a specific file.

my phrasing was ambiguous, i admit.
 
Old 12-02-2005, 03:11 PM   #4
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 31
Just loop over all the files the directory

for f in $dir/*; do echo "warning: $f exists"; done

(you need the nullglob option set or you'll get a nomatch error (in bash: set -o nullglob, in zsh: setopt nullglob).

or

for f in $(find $dir -type f); do echo "warning: $f exists"; done

(There are lots of way to do this....)

This concept applies just as well to perl as to sh or indeed any other language.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script to remove backup ~ files hallamigo Linux - General 3 09-13-2010 04:47 PM
Script that checks mapsize objorkum Linux - Software 1 07-06-2005 12:42 PM
How can I write a shell script to transfer files and keep permissions? LancerNZ Linux - Networking 1 02-24-2005 04:49 AM
Need help with shell script - renaming multiple files NiallC Linux - Newbie 25 07-04-2004 11:45 AM
Shell Script: Reading Pdf files funkymunky Programming 8 06-18-2004 04:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:55 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration