LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 03-18-2005, 08:58 PM   #1
obelxi
LQ Newbie
 
Registered: Feb 2005
Posts: 28

Rep: Reputation: 15
Bash scripting and trying to determine whether a directory exists?


Hi All,

I was wondering if someone could help me figure out how to write a bash script that will determine whether or not a directory exists.

#!/bin/bash

DIR=/home/optio/test/

if [ "`ls $DIR > /dev/null 2>&1 | wc -l | awk '{print $1}']; then
echo "do not create $DIR"

else
echo "create $DIR"
fi

My friend gave me that as an example a while ago, but it just doesn't make any sense to me and when I run it, the command fails. I am wondering if someone could explain how I could fix this script, and more importantly how it works. I know that all shell commands can be written to stdout but the art of processing this information is lost to me. Any help would be greatly appreciated.
 
Old 03-18-2005, 09:21 PM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
How about something like this...
Code:
#!/bin/bash

dir=/home/optio/test/

if [ -d $dir ] ; then
   echo "do not create $dir"
else
   echo "create $dir"
fi
 
Old 03-18-2005, 09:35 PM   #3
obelxi
LQ Newbie
 
Registered: Feb 2005
Posts: 28

Original Poster
Rep: Reputation: 15
Yes that makes a lot more sense to me than the nasty awk and wc commands. I've got one more question. How do you nest an if/else statement within an if/elif chain....

Here's what this question is a larger part of. I want to nest that if/else within the elif but it keeps giving me the error:

unexpected EOF while looking for matching `"'
syntax error: unexpected end of file

And then the line number that corresponds to the line just after the end of the else statement. It doesn't matter if I put a fi in there or not it still produces the same result.

#!/bin/sh

filename=$1
MACHINE_TYPE=`uname -s`
if [ "$MACHINE_TYPE" == "Darwin" ]; then
echo "Yay, you're running a Macintosh"
elif [ "$MACHINE_TYPE" == "Linux" ]; then
echo "Yay, you're running a Linux box"
dir=/tmp/pov-Script

if [ -d $dir ] ; then
echo "do not create $dir"
else
echo "create $dir"
mkdir /tmp/pov-script
chmod 777 /tmp/pov-script
fi

cd /tmp/pov-script
pwd
curl -O http://obelix.ca/xgrid/$filename".pov"
sleep 1
/opt/local/bin/povray $filename".pov" +W640 +H480
ls -la
curl -T $filename".png" -u userass -O \
ftp://obelix.ca/public_html/xgrid/$filename".png"
echo "Finished rendering scripts, they are available on the server"
fi
 
Old 03-18-2005, 10:13 PM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
That's strange. I put an echo statement just before curl like this...
Code:
echo "you made it this far"
#curl -O http://obelix.ca/xgrid/$filename".pov"
#sleep 1
#/opt/local/bin/povray $filename".pov" +W640 +H480
#ls -la
#curl -T $filename".png" -u user:pass -O \
#ftp://obelix.ca/public_html/xgrid/$filename".png"
#echo "Finished rendering scripts, they are available on the server"
fi
and here is the output....
Code:
# ./test file.txt
Yay, you're running a Linux box
do not create /tmp/pov-script
/tmp/pov-script
you made it this far
Edit: Maybe you should change the filename parts to this....
"$filename.pov"
and
"$filename.png"


Last edited by homey; 03-18-2005 at 10:24 PM.
 
Old 03-18-2005, 10:20 PM   #5
obelxi
LQ Newbie
 
Registered: Feb 2005
Posts: 28

Original Poster
Rep: Reputation: 15
so there isn't anything inherently wrong in my code then?
 
Old 03-18-2005, 10:24 PM   #6
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
On second thought, try it without quotes at all...
$filename.pov
and
$filename.png

Last edited by homey; 03-18-2005 at 10:29 PM.
 
Old 03-19-2005, 12:42 AM   #7
obelxi
LQ Newbie
 
Registered: Feb 2005
Posts: 28

Original Poster
Rep: Reputation: 15
I don't know what I did but I added some quotes onto a line and it works flawlessly now. Excellent. Thanks for your help.
 
Old 03-19-2005, 05:37 PM   #8
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 215

Rep: Reputation: 31
Quote:
Originally posted by obelxi
I don't know what I did but I added some quotes onto a line and it works flawlessly now. Excellent. Thanks for your help.
Can you post a list of the files you were trying to read and we'll see why you got an error and why quotation marks solved your problem.
 
Old 04-15-2005, 05:59 AM   #9
izza_azhar
LQ Newbie
 
Registered: Jan 2005
Location: malaysia
Posts: 18

Rep: Reputation: 0
its work

its work..thanks
 
Old 04-18-2005, 11:22 PM   #10
purefan
Member
 
Registered: Aug 2003
Location: Sweden
Distribution: Ubuntu 10.04
Posts: 99

Rep: Reputation: Disabled
hello
where can I find a tutorial on scripts creation?
I understand that I must write the commands I would execute in bash but I don't know with what extension the file should be saved, or if it needs to be run in some program.
I read about the script program but Im not sure how to use it...
 
  


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
How to determine # files in a directory ElricM Linux - Newbie 9 01-24-2012 02:37 PM
Determine os install directory Jebrew Linux - Software 2 08-12-2005 10:48 PM
where does 'su' look to determine if a user exists? MisterESauce Linux - Software 5 04-13-2005 09:18 AM
Does anyone know of a bash script that will determine if RPM exists on a system? jimwelc Linux - Software 3 12-28-2004 03:01 PM
trying to determine the size of all files in a directory riddlebox80 Programming 7 06-11-2003 07:27 PM

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

All times are GMT -5. The time now is 08:34 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