DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 21st August 2008
graudeejs's Avatar
graudeejs graudeejs is offline
Real Name: Aldis Berjoza
formerly killasmurf86
 
Join Date: Jul 2008
Location: Riga, Latvia
Posts: 588
Thanked 29 Times in 26 Posts
Default sh, checknig if file exists

How to check in sh if file/directory exists (using if)?
Couldn't find in net..

Also can someone suggest a Good sh tutorial... i came across few, but they are very basic
Reply With Quote
  #2   (View Single Post)  
Old 21st August 2008
vermaden's Avatar
vermaden vermaden is offline
Administrator
 
Join Date: Apr 2008
Location: pl_PL.lodz
Posts: 1,052
Thanked 118 Times in 93 Posts
Default

Code:
if [ -f file ]; then
  echo "file exists"
fi

if [ -d dir ]; then
  echo "dir exists"
fi
__________________
religions, worst damnation of mankind
"If 386BSD had been available when I started on Linux, Linux would probably never had happened." Linus Torvalds

Linux is not UNIX! Face it! It is not an insult. It is fact: GNU is a recursive acronym for “GNU's Not UNIX”.
vermaden's: links resources deviantart spreadbsd
Reply With Quote
  #3   (View Single Post)  
Old 21st August 2008
graudeejs's Avatar
graudeejs graudeejs is offline
Real Name: Aldis Berjoza
formerly killasmurf86
 
Join Date: Jul 2008
Location: Riga, Latvia
Posts: 588
Thanked 29 Times in 26 Posts
Default

Quote:
Originally Posted by vermaden View Post
Code:
if [ -f file ]; then
  echo "file exists"
fi

if [ -d dir ]; then
  echo "dir exists"
fi
I know already that.
and i can use else to do things i want if file doesn't exist...
Code:
if [ -d dir ]; then
  echo "dir exists"
else
  echo "dir doesn't exist"
fi
but is there a way to do it straight
cause i need action only if it doesn't exist (If doesn't exist, then do this and that)
Reply With Quote
  #4   (View Single Post)  
Old 21st August 2008
vermaden's Avatar
vermaden vermaden is offline
Administrator
 
Join Date: Apr 2008
Location: pl_PL.lodz
Posts: 1,052
Thanked 118 Times in 93 Posts
Default

Quote:
Originally Posted by killasmurf
I know already that.
O RLY?

So why do you ask:
Quote:
Originally Posted by killasmurf
How to check in sh if file/directory exists (using if)?
Code:
if [ ! -f file ]; then
  echo "file does not exist"
fi

if [ ! -d dir ]; then
  echo "dir does not exist"
fi
even shorter:

Code:
[ ! -f file ] && echo "file does not exist"
[ ! -d dir ] && echo "dir does not exist"
Quote:
Originally Posted by killasmurf
Also can someone suggest a Good sh tutorial... i came across few, but they are very basic
Here:
http://developer.apple.com/documenta...ShellScripting
http://developer.apple.com/documenta...lScripting.pdf
http://tldp.org/LDP/abs/html/
__________________
religions, worst damnation of mankind
"If 386BSD had been available when I started on Linux, Linux would probably never had happened." Linus Torvalds

Linux is not UNIX! Face it! It is not an insult. It is fact: GNU is a recursive acronym for “GNU's Not UNIX”.
vermaden's: links resources deviantart spreadbsd
Reply With Quote
  #5   (View Single Post)  
Old 21st August 2008
graudeejs's Avatar
graudeejs graudeejs is offline
Real Name: Aldis Berjoza
formerly killasmurf86
 
Join Date: Jul 2008
Location: Riga, Latvia
Posts: 588
Thanked 29 Times in 26 Posts
Default

Quote:
Originally Posted by vermaden View Post
O RLY?
Code:
if [ ! -f file ]; then
  echo "file does not exist"
fi

if [ ! -d dir ]; then
  echo "dir does not exist"
fi
even shorter:

Code:
[ ! -f file ] && echo "file does not exist"
[ ! -d dir ] && echo "dir does not exist"

Here:
http://developer.apple.com/documenta...ShellScripting
http://developer.apple.com/documenta...lScripting.pdf
http://tldp.org/LDP/abs/html/
Thanks
I did tried -!f -f!, didn't thought about trying ! -f
Reply With Quote
  #6   (View Single Post)  
Old 21st August 2008
arch arch is offline
Port Guard
 
Join Date: Jun 2008
Posts: 38
Thanked 2 Times in 2 Posts
Default

to just check if any file with whatever type exist use -e option in test then do what you have/want to do with the file
__________________
Verbose mode can also be turned on for SSH2 with the (surprise!) VerboseMode keyword.
Reply With Quote
  #7   (View Single Post)  
Old 21st August 2008
vermaden's Avatar
vermaden vermaden is offline
Administrator
 
Join Date: Apr 2008
Location: pl_PL.lodz
Posts: 1,052
Thanked 118 Times in 93 Posts
Default

@killasmurf
You are welcome.
__________________
religions, worst damnation of mankind
"If 386BSD had been available when I started on Linux, Linux would probably never had happened." Linus Torvalds

Linux is not UNIX! Face it! It is not an insult. It is fact: GNU is a recursive acronym for “GNU's Not UNIX”.
vermaden's: links resources deviantart spreadbsd
Reply With Quote
  #8   (View Single Post)  
Old 21st August 2008
Carpetsmoker's Avatar
Carpetsmoker Carpetsmoker is offline
Real Name: Martin
Old man from scene 24
 
Join Date: Apr 2008
Location: Eindhoven, Netherlands
Posts: 2,080
Thanked 198 Times in 156 Posts
Default

Also take a look at test(1).
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things.
Reply With Quote
Reply

Thread Tools
Display Modes

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP read file contents - Maximum file size cksraj Programming 1 21st September 2009 11:38 AM
ifconfig: SIOCAIFADDR: File exists zomo OpenBSD General 7 23rd December 2008 10:20 PM
File system at more than 100% michaelrmgreen FreeBSD General 4 28th July 2008 01:52 PM
file:/// mfaridi FreeBSD Security 3 27th July 2008 02:18 PM
VSFTPd: pid file kostromin FreeBSD Ports and Packages 0 4th July 2008 08:35 AM


All times are GMT. The time now is 03:10 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick