LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 11-28-2005, 03:33 PM   #1
linmix
Senior Member
 
Registered: Jun 2004
Location: Spain
Distribution: FC5
Posts: 1,993
Blog Entries: 1

Rep: Reputation: 46
bash script 'test' question


I'm sure there's some incredibly stupid oversight in all of this, but I can't seem to find it.
Code:
#!/bin/bash

base_path="/home/linmix/"

case $1 in
        -pm)
	   test "$base_path/$2" != " " || mkdir "$base_path/$2"

	   cd $base_path/$2
	   for i in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 ; do
           touch ${i}.txt
	   done
	   exit
        ;;
(etc)
What's causing me trouble is that I cannot figure out how to get mkdir to work only if there is no dir already in place. The either works if there is no file and gives me a mkdir warning if there is one, or it exits saying it can't cd if there was no previous file and works fine if there is.

The stupidest thing is I had it working, then I made some changes and now I can't get it right again!

I've also tried
Code:
if test "$base_path/$2" = " " ; then
	   mkdir "$base_path/$2"
fi
But with the same result

command issued: ./script -pm 002
output: mkdir: cannot create directory `/home/linmix/002': File exists

I've also tried
[code]test "$base_path/$2" = " " && mkdir "$base_path/$2"/code]
sane problem...
 
Old 11-28-2005, 03:55 PM   #2
AdaHacker
Member
 
Registered: Oct 2001
Location: Brockport, NY
Distribution: Kubuntu
Posts: 384

Rep: Reputation: 31
Why are you comparing the path to the string " "? That doesn't make any sense. Why not just directly test if the directory exists?
Code:
test ! -d "$base_path/$2" && mkdir "$base_path/$2"

Last edited by AdaHacker; 11-28-2005 at 03:56 PM.
 
Old 11-29-2005, 12:53 AM   #3
linmix
Senior Member
 
Registered: Jun 2004
Location: Spain
Distribution: FC5
Posts: 1,993
Blog Entries: 1

Original Poster
Rep: Reputation: 46
My iea was that if the directory is " " (nonexistent) , then make it.

Thanks for the line!
 
Old 11-29-2005, 01:01 AM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Why even test for the directory?

mkdir -p dirname does not complain if dirname already exists.
 
Old 11-29-2005, 05:17 AM   #5
vivekr
Member
 
Registered: Nov 2005
Location: Coimbatore,India
Distribution: Fedora Core4
Posts: 68

Rep: Reputation: 15
Hai this will be more informative right
Code:
#!/bin/bash
base_path="/home/vivek/"
case $1 in
        -pm)
           if [ ! -d "$base_path/$2" ]; then
                  mkdir "$base_path/$2"
           fi
           cd $base_path/$2
           for i in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 ; do
                touch ${i}.txt
           done
           exit
        ;;
esac
Cheers

Last edited by vivekr; 11-29-2005 at 05:18 AM.
 
Old 11-29-2005, 11:49 AM   #6
linmix
Senior Member
 
Registered: Jun 2004
Location: Spain
Distribution: FC5
Posts: 1,993
Blog Entries: 1

Original Poster
Rep: Reputation: 46
I like the if statement, even if the && line works well. Looks like I'll have to read up on test.
Thanks guys!!
 
  


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
Bash Script test for empty dir uopjohnson Linux - Software 3 10-07-2005 06:45 PM
bash script question djgerbavore Linux - Newbie 3 07-08-2004 03:39 PM
Simple webserver test script (bash) somedudeppf Programming 3 01-14-2004 10:30 AM
bash script test file operators... bulliver Programming 3 10-17-2003 12:06 PM
benchmark test bash script PTBmilo Programming 3 02-14-2003 04:37 PM


All times are GMT -5. The time now is 06:16 PM.

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