LinuxQuestions.org
Visit the LQ Articles and Editorials section
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-25-2010, 02:14 PM   #1
Mike_V
Member
 
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 6.2 x86_64 GNU/Linux
Posts: 59

Rep: Reputation: 19
line that checks if dir "TEST" exists. If so, nothing, of not mkdir


Hi there,

I have a line of code from a csh script:

Code:
if ( ! -e TEST ) mkdir TEST
it checks if the directory "TEST" exists and if it DOES exist then nothing happens, if it DOES NOT exist then directory TEST will be made.


Now I want to use this in a sh (or bash) script.

Does anybody know the equivalent of the line above for sh?

Thanks!
 
Old 08-25-2010, 02:21 PM   #2
bluebox
Member
 
Registered: Jun 2004
Posts: 71

Rep: Reputation: 6
Try
Quote:

if [ ! -d $TEST ]; then mkdir $TEST; fi
Edit: okay, better use
Quote:
if [ ! -e $TEST ]; then mkdir $TEST; fi
-d checks for directories only.

Last edited by bluebox; 08-25-2010 at 02:30 PM.
 
1 members found this post helpful.
Old 08-25-2010, 02:40 PM   #3
Mike_V
Member
 
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 6.2 x86_64 GNU/Linux
Posts: 59

Original Poster
Rep: Reputation: 19
solved

I did have to remove the $ signs... I don't know why.

So this works like a charm:

Code:
if [ ! -e TEST ]; then mkdir TEST; fi

Thanks a lot!
 
Old 08-25-2010, 02:54 PM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Alternatively, using the more robust [[ ]] instead of [ ] and the more concise but less legible || instead of if-then-else
Code:
[[ -d $TEST ]] || mkdir "$TEST"
EDIT: seeing your reply above, better
Code:
[[ -e TEST ]] || mkdir TEST
EDIT 2:

You had to change $TEST to TEST because $TEST means "the value of variable TEST" whereas you want the string TEST.

On reflection it might be worth handling the error condition of TEST existing but not being a directory.
Code:
if [[ ! -d TEST ]]; then
    if [[ -e TEST ]]; then
        echo 'Cannot create directory TEST; TEST already exists:' >&2
        /bin/ls -l TEST >&2
        exit 1
    fi
    mkdir TEST || exit 1
fi
Notes:
  1. >&2 means "redirect stdout to stderr". It is conventional and useful to write error messages to stderr. It allows error messages to be easily separated from informational messages. By default both stdout and stderr are displayed on the screen.
  2. /bin/ls is used instead of ls because ls is commonly aliased and might give unexpected results.
  3. If mkdir TEST fails it will write an error message to stderr and set its return code to non-zero. The shell, on detecting a non-zero return code, will execute the command after the || thus terminating the script.

Last edited by catkin; 08-25-2010 at 03:13 PM.
 
1 members found this post helpful.
Old 08-26-2010, 09:26 AM   #5
Mike_V
Member
 
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 6.2 x86_64 GNU/Linux
Posts: 59

Original Poster
Rep: Reputation: 19
This is why I love this forum... SO useful!
Bluebox came with a quick solution and then catkin's help teaches me things I can apply elsewhere.
Thanks to both bluebox and catkin!
 
Old 08-26-2010, 09:44 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
By the way, a quick and dirty technique is to simply use mkdir -p. If the directory (including any of its parents) doesn't exist, then it will be created, and if it does exist it will exit without error.

On the other hand, using a bash test first means you can avoid opening up an unnecessary external process, and so is probably better if efficiency is a goal.
 
  


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
[SOLVED] Silencing the line "echo test > test/test.txt" in a shell script Arenlor Linux - General 2 06-18-2010 02:37 PM
[SOLVED] how to simulate "mkdir -p /home/blah1/blah2/blah3" in "c" where only /home exist platinumedge Linux - Newbie 4 10-04-2009 07:28 PM
rkhunter warnings on "possible promiscuous interface" and file properties checks vinnie_vinodh Linux - Newbie 1 04-29-2009 03:44 AM
C/C++ functions similar to BASH's "cp", "mv", "mkdir", etc? kornerr Programming 10 04-23-2006 10:48 AM
"mkdir: cannot create directory `foo': Read-only file system" on FAT32 maddes Linux - Hardware 1 11-26-2003 07:19 PM


All times are GMT -5. The time now is 10:30 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration