LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-17-2010, 10:04 AM   #1
jsteel
Member
 
Registered: Mar 2007
Location: England
Distribution: Arch
Posts: 392

Rep: Reputation: 34
Bash if and?


Hi,

I'm learning Bash and can't find how to say "if file1 exists and file2 exits then...".

I thought this may work:

Code:
file1="/root/file1"
file2="/root/file2"
if [ -e $file1 ] | [ -e $file2 ]
then
... but it doesn't. Where have I gone wrong?

Thanks
 
Old 08-17-2010, 10:11 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

You almost had it:

if [ -e $file1 ] || [ -e $file2 ]

|| instead of |

Hope this helps.

Hold on.... I read it wrong.

|| is an OR, what you need is an AND, which is &&

So this should be the correct answer: if [ -e $file1 ] && [ -e $file2 ]

Last edited by druuna; 08-17-2010 at 10:14 AM. Reason: Corrected answer
 
Old 08-17-2010, 10:33 AM   #3
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
In bash it's always better to use [[ than [:
Code:
if [[ -e $file1 && -e $file2 ]]; then
...
fi
With [ you are obliged to put variables inside double quotes (") or else the contents of the variables will be in danger of getting split due to IFS.

Last edited by konsolebox; 08-17-2010 at 10:34 AM. Reason: 1 and 2
 
Old 08-17-2010, 10:59 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
or even:

Code:
[ -e file1 -a -e file2 ]
 
Old 08-17-2010, 03:53 PM   #5
jsteel
Member
 
Registered: Mar 2007
Location: England
Distribution: Arch
Posts: 392

Original Poster
Rep: Reputation: 34
Thanks, that info helped a lot.
 
Old 08-17-2010, 08:42 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
you can also use test
Code:
if test -e "$file" || test -e "$another" ;then
..
fi
 
  


Reply



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] Finding bugs in bash scripts, Analyis tool for bash traene Programming 2 10-31-2009 11:42 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM
BASH -copy stdin to stdout (replace cat) (bash browser) gnashley Programming 4 07-21-2008 01:14 PM
Bash: Print usage statement & exit; otherwise continue using Bash shorthand operators stefanlasiewski Programming 9 02-07-2006 05:20 PM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 06:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:02 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
Open Source Consulting | Domain Registration