LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-29-2009, 08:12 AM   #1
mike2008
LQ Newbie
 
Registered: Apr 2009
Posts: 7

Rep: Reputation: 0
Script cannot find directory path


Hi,

I wrote a simple script to find if a directory exists or not. I kept getting errors that the directory is not there, but I am certain it is there. Please help.

#!/bin/bash
DMPDIR=/home/oracle
# Make sure DMPDIR is a valid directory path
cd $DMPDIR
pwd
if [[ -d $DMPDIR ]]
then
#do something
fi


When I run the above script (name.sh), I got the folllowing errors:
:No such file or directory /home/oracle
:command not found 5: pwd
name.sh: line 0: syntax error: unexpected end of file

I tried to export DMPDIR, still won't work.


Thanks.
 
Old 04-29-2009, 08:21 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
You could use

set -xv

as the 2nd line to see what its actually doing.
If the dir /home/oracle really does exist, it doesn't always mean you can cd to it; think ownerships and permissions.
 
Old 04-29-2009, 08:24 AM   #3
repo
LQ 5k Club
 
Registered: May 2001
Location: Belgium
Distribution: Arch
Posts: 8,527

Rep: Reputation: 898Reputation: 898Reputation: 898Reputation: 898Reputation: 898Reputation: 898Reputation: 898
try

Code:
#!/bin/bash
DMPDIR=/home/oracle
# Make sure DMPDIR is a valid directory path

if [ -d $DMPDIR ];
then
   echo "$DMPDIR exists"
else
   echo "$DMPDIR does not exists"
fi
 
Old 04-29-2009, 09:41 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Can you post the output of the following command?
Code:
ls -ld /home
 
Old 04-29-2009, 10:37 AM   #5
mike2008
LQ Newbie
 
Registered: Apr 2009
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks chrism01 and repo. It works now. I think it was because I wrote the script in textpad then upload it to the server, thats why I was having so much problems with it. When I create a new script from scratch, it works. Thanks to both. I do have another question, maybe two more questions..

1) Why do I need to put ; after "if [ -d $DMPDIR ]"
2) I tried doing an if statement "if [ $count > 0]", it compiles fine. But somehow it created a file named 0, I have no clue how I did that, please see below:

#!/bin/bash
#*************************************************
# This script will delete files older than 60 days
#**************************************************
DMPDIR=/home/oracle
# Make sure DMPDIR is a valid directory path
if [ -d $DMPDIR ];
then
# Step 1: Create a list files older than 60 days.
find $DMPDIR/*.dmp.gz -mtime +60 -print >> /var/tmp/dmps_history.lst
# Step 2: Get a count of dump and log files to be deleted.
check_dmp_file=`cat /var/tmp/dmps_history.lst|wc -l`
dmp_num=`expr $check_dmp_file`
# If there are dump and log files to be removed, then delete them.
if [ $dmp_num > 0 ];
then
find $DMPDIR/*.dmp.gz -mtime +60 -exec rm {} \;
fi
fi
~
 
Old 04-29-2009, 10:56 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
1) The semi-colon is not mandatory. You have to use it if you put the if/then statement on a single line, but if you write the if/then block on multiple lines, it is not necessary.

2) The "> 0" is interpreted by the shell as redirection to file 0. If you want to do an integer comparison, the correct operator is -gt (greater than).
 
Old 04-29-2009, 11:11 AM   #7
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
mike2008, you don't need to put the ';' after the if statement since you are putting the then on a separate line. However, as far as style goes, I don't like the then on a separate line, and neither do a lot of others. Therefore, we typically write the if statement like:
Code:
if [ something ] ; then
        run_a_command_or_two
fi
This way just appears more balanced and by looking at it you can see which fi belongs to which if fairly easily. However, to get the 'then' on the same line, you need the command separator ';'. Nested if statements really point out how this looks cleaner:
Code:
if [ something ] ; then
        if [ something_else ] ; then
                run_a_command_or_two
        fi
fi
instead of:
Code:
if [ something ]
then
        if [ something_else ]
        then
                run_a_command_or_two
        fi
fi
As far as the second question goes, '>' is the redirect operator in most shells. It means, "don't put the output of this command to the screen, but instead make a new file and put the output in it (overwriting any currently found file)." There are also '>>' which means append to the file instead of overwriting, creating the file if needed and 'tee' which really is a command which means display the output AND write it to the file. You specified '0' as the filename to write to, so that is what the script did.

What you want is 'if [ $dmp_num -gt 0 ] ; then'.

Also, the '[' is actually a command. You can find in in '/usr/bin/['.
 
Old 04-29-2009, 01:12 PM   #8
mike2008
LQ Newbie
 
Registered: Apr 2009
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks forrest. It works now!!!
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Unexpected directory in path, cannot find source alanhr Linux - General 3 03-20-2009 06:39 AM
Find a file path and directory path ak.lokesh Linux - Newbie 3 02-19-2009 01:37 PM
Shell script help: How to pass a directory path to the script? dv502 Programming 13 09-10-2008 04:07 AM
Discovering Script Directory Path WayneK Programming 9 07-14-2008 10:18 AM
how to find a relative path to another directory bahadur Programming 9 04-04-2005 08:52 PM


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